2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
2015-09-07 11:07:55 +02:00
|
|
|
|
2017-03-08 17:03:35 +01:00
|
|
|
use App\Http\Requests\TaskRequest;
|
2016-11-14 10:43:10 +01:00
|
|
|
use App\Http\Requests\UpdateTaskRequest;
|
2015-09-07 11:07:55 +02:00
|
|
|
use App\Models\Task;
|
|
|
|
use App\Ninja\Repositories\TaskRepository;
|
2015-11-27 13:55:28 +01:00
|
|
|
use App\Ninja\Transformers\TaskTransformer;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Auth;
|
|
|
|
use Input;
|
|
|
|
use Response;
|
2015-09-07 11:07:55 +02:00
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
class TaskApiController extends BaseAPIController
|
2015-09-07 11:07:55 +02:00
|
|
|
{
|
|
|
|
protected $taskRepo;
|
|
|
|
|
2016-05-01 22:55:13 +02:00
|
|
|
protected $entityType = ENTITY_TASK;
|
|
|
|
|
2015-09-07 11:07:55 +02:00
|
|
|
public function __construct(TaskRepository $taskRepo)
|
|
|
|
{
|
2016-03-02 15:36:46 +01:00
|
|
|
parent::__construct();
|
2015-11-27 13:55:28 +01:00
|
|
|
|
2015-09-07 11:07:55 +02:00
|
|
|
$this->taskRepo = $taskRepo;
|
|
|
|
}
|
|
|
|
|
2015-11-08 22:53:13 +01:00
|
|
|
/**
|
|
|
|
* @SWG\Get(
|
|
|
|
* path="/tasks",
|
2017-03-09 17:22:37 +01:00
|
|
|
* summary="List tasks",
|
|
|
|
* operationId="listTasks",
|
2015-11-08 22:53:13 +01:00
|
|
|
* tags={"task"},
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
2017-03-08 17:03:35 +01:00
|
|
|
* description="A list of tasks",
|
2015-11-08 22:53:13 +01:00
|
|
|
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Task"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2015-11-27 13:55:28 +01:00
|
|
|
public function index()
|
2015-09-07 11:07:55 +02:00
|
|
|
{
|
2016-06-08 16:56:13 +02:00
|
|
|
$tasks = Task::scope()
|
2016-05-01 22:55:13 +02:00
|
|
|
->withTrashed()
|
2016-11-29 18:47:26 +01:00
|
|
|
->with('client', 'invoice', 'project')
|
2019-01-30 11:45:46 +01:00
|
|
|
->orderBy('updated_at', 'desc');
|
2015-09-07 11:07:55 +02:00
|
|
|
|
2016-06-08 16:56:13 +02:00
|
|
|
return $this->listResponse($tasks);
|
2015-09-07 11:07:55 +02:00
|
|
|
}
|
|
|
|
|
2017-03-08 17:03:35 +01:00
|
|
|
/**
|
|
|
|
* @SWG\Get(
|
|
|
|
* path="/tasks/{task_id}",
|
2017-03-09 17:22:37 +01:00
|
|
|
* summary="Retrieve a task",
|
|
|
|
* operationId="getTask",
|
2017-03-08 17:03:35 +01:00
|
|
|
* tags={"task"},
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="path",
|
|
|
|
* name="task_id",
|
|
|
|
* type="integer",
|
2017-03-09 11:11:45 +01:00
|
|
|
* required=true
|
2017-03-08 17:03:35 +01:00
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="A single task",
|
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Task"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function show(TaskRequest $request)
|
|
|
|
{
|
|
|
|
return $this->itemResponse($request->entity());
|
|
|
|
}
|
|
|
|
|
2015-11-08 22:53:13 +01:00
|
|
|
/**
|
|
|
|
* @SWG\Post(
|
|
|
|
* path="/tasks",
|
|
|
|
* summary="Create a task",
|
2017-03-09 17:22:37 +01:00
|
|
|
* operationId="createTask",
|
|
|
|
* tags={"task"},
|
2015-11-08 22:53:13 +01:00
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="body",
|
2017-03-08 17:03:35 +01:00
|
|
|
* name="task",
|
2015-11-08 22:53:13 +01:00
|
|
|
* @SWG\Schema(ref="#/definitions/Task")
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="New task",
|
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Task"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2015-09-07 11:07:55 +02:00
|
|
|
public function store()
|
|
|
|
{
|
|
|
|
$data = Input::all();
|
|
|
|
$taskId = isset($data['id']) ? $data['id'] : false;
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2015-09-07 11:07:55 +02:00
|
|
|
if (isset($data['client_id']) && $data['client_id']) {
|
|
|
|
$data['client'] = $data['client_id'];
|
|
|
|
}
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2018-05-06 21:45:47 +02:00
|
|
|
if (! empty($data['time_details'])) {
|
|
|
|
$timeLog = [];
|
|
|
|
foreach ($data['time_details'] as $detail) {
|
|
|
|
$startTime = strtotime($detail['start_datetime']);
|
|
|
|
$endTime = false;
|
|
|
|
if (! empty($detail['end_datetime'])) {
|
|
|
|
$endTime = strtotime($detail['end_datetime']);
|
2018-05-09 10:30:27 +02:00
|
|
|
} else {
|
|
|
|
$duration = 0;
|
|
|
|
if (! empty($detail['duration_seconds'])) {
|
|
|
|
$duration += $detail['duration_seconds'];
|
|
|
|
}
|
|
|
|
if (! empty($detail['duration_minutes'])) {
|
|
|
|
$duration += $detail['duration_minutes'] * 60;
|
|
|
|
}
|
|
|
|
if (! empty($detail['duration_hours'])) {
|
|
|
|
$duration += $detail['duration_hours'] * 60 * 60;
|
|
|
|
}
|
|
|
|
if ($duration) {
|
|
|
|
$endTime = $startTime + $duration;
|
|
|
|
}
|
2018-05-06 21:45:47 +02:00
|
|
|
}
|
|
|
|
$timeLog[] = [$startTime, $endTime];
|
|
|
|
if (! $endTime) {
|
|
|
|
$data['is_running'] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$data['time_log'] = json_encode($timeLog);
|
|
|
|
}
|
|
|
|
|
2015-09-07 11:07:55 +02:00
|
|
|
$task = $this->taskRepo->save($taskId, $data);
|
|
|
|
$task = Task::scope($task->public_id)->with('client')->first();
|
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
$transformer = new TaskTransformer(Auth::user()->account, Input::get('serializer'));
|
|
|
|
$data = $this->createItem($task, $transformer, 'task');
|
2015-09-07 11:07:55 +02:00
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
return $this->response($data);
|
2015-09-07 11:07:55 +02:00
|
|
|
}
|
|
|
|
|
2016-11-14 10:43:10 +01:00
|
|
|
/**
|
|
|
|
* @SWG\Put(
|
2017-03-08 17:03:35 +01:00
|
|
|
* path="/tasks/{task_id}",
|
2016-11-14 10:43:10 +01:00
|
|
|
* summary="Update a task",
|
2017-03-09 17:22:37 +01:00
|
|
|
* operationId="updateTask",
|
|
|
|
* tags={"task"},
|
2016-11-14 10:43:10 +01:00
|
|
|
* @SWG\Parameter(
|
2017-03-08 17:03:35 +01:00
|
|
|
* in="path",
|
|
|
|
* name="task_id",
|
|
|
|
* type="integer",
|
2017-03-09 11:11:45 +01:00
|
|
|
* required=true
|
2017-03-08 17:03:35 +01:00
|
|
|
* ),
|
|
|
|
* @SWG\Parameter(
|
2016-11-14 10:43:10 +01:00
|
|
|
* in="body",
|
|
|
|
* name="body",
|
|
|
|
* @SWG\Schema(ref="#/definitions/Task")
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Update task",
|
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Task"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function update(UpdateTaskRequest $request)
|
|
|
|
{
|
2018-04-21 13:54:05 +02:00
|
|
|
if ($request->action) {
|
|
|
|
return $this->handleAction($request);
|
|
|
|
}
|
2018-05-06 21:45:47 +02:00
|
|
|
|
2016-11-14 10:43:10 +01:00
|
|
|
$task = $request->entity();
|
2018-05-06 21:45:47 +02:00
|
|
|
|
2016-11-14 10:43:10 +01:00
|
|
|
$task = $this->taskRepo->save($task->public_id, \Illuminate\Support\Facades\Input::all());
|
|
|
|
|
|
|
|
return $this->itemResponse($task);
|
|
|
|
}
|
2017-03-08 17:03:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @SWG\Delete(
|
|
|
|
* path="/tasks/{task_id}",
|
|
|
|
* summary="Delete a task",
|
2017-03-09 17:22:37 +01:00
|
|
|
* operationId="deleteTask",
|
|
|
|
* tags={"task"},
|
2017-03-08 17:03:35 +01:00
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="path",
|
|
|
|
* name="task_id",
|
|
|
|
* type="integer",
|
2017-03-09 11:11:45 +01:00
|
|
|
* required=true
|
2017-03-08 17:03:35 +01:00
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Deleted task",
|
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Task"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2017-03-09 11:11:45 +01:00
|
|
|
public function destroy(UpdateTaskRequest $request)
|
2017-03-08 17:03:35 +01:00
|
|
|
{
|
|
|
|
$task = $request->entity();
|
|
|
|
|
|
|
|
$this->taskRepo->delete($task);
|
|
|
|
|
|
|
|
return $this->itemResponse($task);
|
|
|
|
}
|
2015-09-07 11:07:55 +02:00
|
|
|
}
|