1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Http/Controllers/TaskApiController.php

102 lines
2.8 KiB
PHP
Raw Normal View History

2015-09-07 11:07:55 +02:00
<?php namespace App\Http\Controllers;
2015-11-27 13:55:28 +01:00
use Auth;
2015-09-07 11:07:55 +02:00
use Utils;
use Response;
use Input;
use App\Models\Task;
use App\Ninja\Repositories\TaskRepository;
2015-11-27 13:55:28 +01:00
use App\Http\Controllers\BaseAPIController;
use App\Ninja\Transformers\TaskTransformer;
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;
public function __construct(TaskRepository $taskRepo)
{
2015-11-27 13:55:28 +01:00
parent::__construct();
2015-09-07 11:07:55 +02:00
$this->taskRepo = $taskRepo;
}
2015-11-08 22:53:13 +01:00
/**
* @SWG\Get(
* path="/tasks",
* tags={"task"},
* summary="List of tasks",
* @SWG\Response(
* response=200,
* description="A list with tasks",
* @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
{
2015-11-27 13:55:28 +01:00
$paginator = Task::scope();
$tasks = Task::scope()
->with($this->getIncluded());
2015-09-07 11:07:55 +02:00
2015-11-27 13:55:28 +01:00
if ($clientPublicId = Input::get('client_id')) {
$filter = function($query) use ($clientPublicId) {
2015-09-07 11:07:55 +02:00
$query->where('public_id', '=', $clientPublicId);
2015-11-27 13:55:28 +01:00
};
$tasks->whereHas('client', $filter);
$paginator->whereHas('client', $filter);
2015-09-07 11:07:55 +02:00
}
2015-11-27 13:55:28 +01:00
$tasks = $tasks->orderBy('created_at', 'desc')->paginate();
$paginator = $paginator->paginate();
$transformer = new TaskTransformer(\Auth::user()->account, Input::get('serializer'));
2015-09-07 11:07:55 +02:00
2015-11-27 13:55:28 +01:00
$data = $this->createCollection($tasks, $transformer, 'tasks', $paginator);
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
}
2015-11-08 22:53:13 +01:00
/**
* @SWG\Post(
* path="/tasks",
* tags={"task"},
* summary="Create a task",
* @SWG\Parameter(
* in="body",
* name="body",
* @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;
if (isset($data['client_id']) && $data['client_id']) {
$data['client'] = $data['client_id'];
}
$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
}
}