Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to create tasks that are not linked to contacts [wip] #2067

Merged
merged 18 commits into from
Nov 18, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add delete task service
  • Loading branch information
djaiss committed Nov 17, 2018
commit 5bcb8257c7e2637e487c920873e38f50ee14d3dc
14 changes: 8 additions & 6 deletions app/Http/Controllers/Api/ApiTaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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\Http\Resources\Task\Task as TaskResource;
Expand Down Expand Up @@ -108,16 +109,17 @@ public function update(Request $request, $taskId)
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
43 changes: 0 additions & 43 deletions app/Http/Controllers/Contacts/TasksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use App\Models\Contact\Contact;
use App\Services\Task\CreateTask;
use App\Http\Controllers\Controller;
use App\Http\Requests\People\TasksRequest;
use App\Http\Requests\People\TaskToggleRequest;

class TasksController extends Controller
{
Expand All @@ -34,47 +32,6 @@ public function index(Contact $contact)
return $tasks;
}

/**
* Store the task.
*/
public function store(TasksRequest $request, Contact $contact)
{
return (new CreateTask)->execute([
'account_id' => auth()->user()->account->id,
'contact_id' => $contact->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.
*
Expand Down
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.

42 changes: 42 additions & 0 deletions app/Services/Task/DestroyTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Services\Task;

use App\Services\BaseService;
use App\Models\Contact\Task;
use Illuminate\Support\Facades\Storage;

class DestroyTask extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'task_id' => 'required|integer',
];
}

/**
* Destroy a task.
*
* @param array $data
* @return bool
*/
public function execute(array $data) : bool
{
$this->validate($data);

$task = Task::where('account_id', $data['account_id'])
->findOrFail($data['task_id']);

// Delete the object in the DB
$task->delete();

return true;
}
}
54 changes: 25 additions & 29 deletions tests/Api/ApiTaskControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,38 +322,34 @@ public function test_updating_a_task_with_wrong_account_triggers_an_error()
$this->expectNotFound($response);
}

// public function test_tasks_delete()
// {
// $user = $this->signin();
// $contact = factory(Contact::class)->create([
// 'account_id' => $user->account->id,
// ]);
// $task = factory(Task::class)->create([
// 'account_id' => $user->account->id,
// 'contact_id' => $contact->id,
// ]);
// $this->assertDatabaseHas('tasks', [
// 'account_id' => $user->account->id,
// 'contact_id' => $contact->id,
// 'id' => $task->id,
// ]);
public function test_it_deletes_a_task()
{
$user = $this->signin();
$contact = factory(Contact::class)->create([
'account_id' => $user->account->id,
]);
$task = factory(Task::class)->create([
'account_id' => $user->account->id,
'contact_id' => $contact->id,
]);

// $response = $this->json('DELETE', '/api/tasks/'.$task->id);
$response = $this->json('DELETE', '/api/tasks/'.$task->id);

// $response->assertStatus(200);
// $this->assertDatabaseMissing('tasks', [
// 'account_id' => $user->account->id,
// 'contact_id' => $contact->id,
// 'id' => $task->id,
// ]);
// }
$response->assertStatus(200);

// public function test_tasks_delete_error()
// {
// $user = $this->signin();
$this->assertDatabaseMissing('tasks', [
'account_id' => $user->account->id,
'contact_id' => $contact->id,
'id' => $task->id,
]);
}

// $response = $this->json('DELETE', '/api/tasks/0');
public function test_it_cant_delete_a_task_if_wrong_task_id()
{
$user = $this->signin();

// $this->expectNotFound($response);
// }
$response = $this->json('DELETE', '/api/tasks/0');

$this->expectNotFound($response);
}
}
68 changes: 68 additions & 0 deletions tests/Unit/Services/Task/DestroyTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Tests\Unit\Services\Contact\Document;

use Tests\TestCase;
use App\Models\Account\Account;
use App\Models\Contact\Task;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use App\Exceptions\MissingParameterException;
use App\Services\Contact\Document\UploadDocument;
use App\Services\Task\DestroyTask;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class DestroyTaskTest extends TestCase
{
use DatabaseTransactions;

public function test_it_destroys_a_task()
{
$task = factory(Task::class)->create([]);

$this->assertDatabaseHas('tasks', [
'id' => $task->id,
]);

$request = [
'account_id' => $task->account->id,
'task_id' => $task->id,
];

$destroyTaskService = new DestroyTask;
$bool = $destroyTaskService->execute($request);

$this->assertDatabaseMissing('tasks', [
'id' => $task->id,
]);
}

public function test_it_fails_if_wrong_parameters_are_given()
{
$request = [
'task_id' => 2,
];

$this->expectException(MissingParameterException::class);

$destroyTaskService = new DestroyTask;
$result = $destroyTaskService->execute($request);
}

public function test_it_throws_a_task_doesnt_exist()
{
$task = factory(Task::class)->create([]);
$account = factory(Account::class)->create([]);

$request = [
'account_id' => $account->id,
'task_id' => $task->id,
];

$this->expectException(ModelNotFoundException::class);

$destroyTaskService = new DestroyTask;
$task = $destroyTaskService->execute($request);
}
}