Skip to content

Commit

Permalink
feat: add ability to create tasks that are not linked to contacts (#2067
Browse files Browse the repository at this point in the history
)
  • Loading branch information
djaiss authored Nov 18, 2018
1 parent a743afe commit 4f3fbd9
Show file tree
Hide file tree
Showing 57 changed files with 1,529 additions and 1,149 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
UNRELEASED CHANGES:

*
* Add ability to create tasks that are not linked to any contacts

RELEASED VERSIONS:

Expand Down
94 changes: 31 additions & 63 deletions app/Http/Controllers/Api/ApiTaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
use App\Models\Contact\Task;
use Illuminate\Http\Request;
use App\Models\Contact\Contact;
use App\Services\Task\CreateTask;
use App\Services\Task\UpdateTask;
use App\Services\Task\DestroyTask;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Validator;
use App\Exceptions\MissingParameterException;
use App\Http\Resources\Task\Task as TaskResource;
use Illuminate\Database\Eloquent\ModelNotFoundException;

Expand Down Expand Up @@ -55,18 +58,17 @@ public function show(Request $request, $taskId)
*/
public function store(Request $request)
{
$isvalid = $this->validateUpdate($request);
if ($isvalid !== true) {
return $isvalid;
}

try {
$task = Task::create(
$request->all()
+ ['account_id' => auth()->user()->account_id]
);
} catch (QueryException $e) {
return $this->respondNotTheRightParameters();
$task = (new CreateTask)->execute([
'account_id' => auth()->user()->account->id,
'contact_id' => ($request->get('contact_id') == '' ? null : $request->get('contact_id')),
'title' => $request->get('title'),
'description' => ($request->get('description') == '' ? null : $request->get('description')),
]);
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
} catch (MissingParameterException $e) {
return $this->respondInvalidParameters($e->errors);
}

return new TaskResource($task);
Expand All @@ -81,58 +83,23 @@ public function store(Request $request)
public function update(Request $request, $taskId)
{
try {
$task = Task::where('account_id', auth()->user()->account_id)
->findOrFail($taskId);
$task = (new UpdateTask)->execute(
$request->all()
+
[
'task_id' => $taskId,
'account_id' => auth()->user()->account->id,
]
);
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}

$isvalid = $this->validateUpdate($request);
if ($isvalid !== true) {
return $isvalid;
}

try {
$task->update($request->all());
} catch (QueryException $e) {
return $this->respondNotTheRightParameters();
} catch (MissingParameterException $e) {
return $this->respondInvalidParameters($e->errors);
}

return new TaskResource($task);
}

/**
* Validate the request for update.
*
* @param Request $request
* @return mixed
*/
private function validateUpdate(Request $request)
{
// Validates basic fields to create the entry
$validator = Validator::make($request->all(), [
'title' => 'required|max:255',
'description' => 'string|max:1000000',
'completed_at' => 'date',
'completed' => 'boolean|required',
'contact_id' => 'required|integer',
]);

if ($validator->fails()) {
return $this->respondValidatorFailed($validator);
}

try {
Contact::where('account_id', auth()->user()->account_id)
->where('id', $request->input('contact_id'))
->firstOrFail();
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}

return true;
}

/**
* Delete a task.
* @param Request $request
Expand All @@ -141,16 +108,17 @@ private function validateUpdate(Request $request)
public function destroy(Request $request, $taskId)
{
try {
$task = Task::where('account_id', auth()->user()->account_id)
->where('id', $taskId)
->firstOrFail();
(new DestroyTask)->execute([
'task_id' => $taskId,
'account_id' => auth()->user()->account->id,
]);
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
} catch (MissingParameterException $e) {
return $this->respondInvalidParameters($e->errors);
}

$task->delete();

return $this->respondObjectDeleted($task->id);
return $this->respondObjectDeleted($taskId);
}

/**
Expand Down
55 changes: 0 additions & 55 deletions app/Http/Controllers/Contacts/TasksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
namespace App\Http\Controllers\Contacts;

use App\Helpers\DateHelper;
use App\Models\Contact\Task;
use App\Models\Contact\Contact;
use App\Http\Controllers\Controller;
use App\Http\Requests\People\TasksRequest;
use App\Http\Requests\People\TaskToggleRequest;

class TasksController extends Controller
{
Expand All @@ -32,56 +29,4 @@ public function index(Contact $contact)

return $tasks;
}

/**
* Store the task.
*/
public function store(TasksRequest $request, Contact $contact)
{
return $contact->tasks()->create([
'account_id' => auth()->user()->account_id,
'title' => $request->get('title'),
'description' => ($request->get('description') == '' ? null : $request->get('description')),
]);
}

/**
* Edit the task field.
*/
public function update(TasksRequest $request, Contact $contact, Task $task)
{
$task->update([
'title' => $request->get('title'),
'description' => ($request->get('description') == '' ? null : $request->get('description')),
'completed' => $request->get('completed'),
]);

return $task;
}

public function toggle(TaskToggleRequest $request, Contact $contact, Task $task)
{
// check if the state of the task has changed
if ($task->completed) {
$task->completed_at = null;
$task->completed = false;
} else {
$task->completed = true;
$task->completed_at = now();
}

$task->save();
}

/**
* Remove the specified resource from storage.
*
* @param Contact $contact
* @param Task $task
* @return \Illuminate\Http\Response
*/
public function destroy(Contact $contact, Task $task)
{
$task->delete();
}
}
18 changes: 0 additions & 18 deletions app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use App\Http\Resources\Debt\Debt as DebtResource;
use App\Http\Resources\Task\Task as TaskResource;

class DashboardController extends Controller
{
Expand Down Expand Up @@ -157,23 +156,6 @@ public function debts()
return $debtsCollection;
}

/**
* Get tasks for the dashboard.
*
* @return Collection
*/
public function tasks()
{
$tasksCollection = collect([]);
$tasks = auth()->user()->account->tasks()->where('completed', 0)->get();

foreach ($tasks as $task) {
$tasksCollection->push(new TaskResource($task));
}

return $tasksCollection;
}

/**
* Save the current active tab to the User table.
*/
Expand Down
74 changes: 74 additions & 0 deletions app/Http/Controllers/TasksController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace App\Http\Controllers;

use App\Models\Contact\Task;
use Illuminate\Http\Request;
use App\Models\Contact\Contact;
use App\Services\Task\CreateTask;
use App\Services\Task\UpdateTask;
use App\Services\Task\DestroyTask;
use App\Http\Resources\Task\Task as TaskResource;

class TasksController extends Controller
{
/**
* Get the list of tasks for the account.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return TaskResource::collection(auth()->user()->account->tasks);
}

/**
* Store a newly created resource in storage.
*
* @param $request
* @param Contact $contact
* @return Task
*/
public function store(Request $request) : Task
{
return (new CreateTask)->execute([
'account_id' => auth()->user()->account->id,
'contact_id' => ($request->get('contact_id') == '' ? null : $request->get('contact_id')),
'title' => $request->get('title'),
'description' => ($request->get('description') == '' ? null : $request->get('description')),
]);
}

/**
* Update a task.
*
* @param Request $request
* @return Task
*/
public function update(Request $request, $taskId) : Task
{
return (new UpdateTask)->execute([
'account_id' => auth()->user()->account->id,
'task_id' => $taskId,
'contact_id' => ($request->get('contact_id') == '' ? null : $request->get('contact_id')),
'title' => $request->get('title'),
'description' => ($request->get('description') == '' ? null : $request->get('description')),
'completed' => $request->get('completed'),
]);
}

/**
* Destroy the task.
*
* @param Contact $contact
* @param Task $task
* @return \Illuminate\Http\Response
*/
public function destroy(Contact $contact, $taskId)
{
(new DestroyTask)->execute([
'task_id' => $taskId,
'account_id' => auth()->user()->account->id,
]);
}
}
28 changes: 0 additions & 28 deletions app/Http/Requests/People/TaskToggleRequest.php

This file was deleted.

36 changes: 0 additions & 36 deletions app/Http/Requests/People/TasksRequest.php

This file was deleted.

Loading

0 comments on commit 4f3fbd9

Please sign in to comment.