2021-11-25 12:33:08 +03:00
< ? php
namespace frontend\modules\api\controllers ;
2023-04-26 01:22:02 +03:00
use common\classes\Debug ;
2023-01-18 15:30:38 +03:00
use common\models\ProjectTask ;
2023-05-23 02:11:44 +03:00
use common\models\ProjectTaskUser ;
use common\models\User ;
2022-01-16 23:54:13 +03:00
use common\services\TaskService ;
2023-05-23 02:11:44 +03:00
use frontend\modules\api\models\ProjectColumn ;
2021-11-25 12:33:08 +03:00
use Yii ;
use yii\base\InvalidConfigException ;
2023-05-23 02:11:44 +03:00
use yii\web\BadRequestHttpException ;
2021-11-25 12:33:08 +03:00
use yii\web\NotFoundHttpException ;
use yii\web\ServerErrorHttpException ;
2022-01-16 23:54:13 +03:00
class TaskController extends ApiController
2021-11-25 12:33:08 +03:00
{
public function verbs () : array
{
return [
'get-task' => [ 'get' ],
'get-task-list' => [ 'get' ],
2023-04-25 01:32:15 +03:00
'get-user-tasks' => [ 'get' ],
2021-11-25 12:33:08 +03:00
'create-task' => [ 'post' ],
'update-task' => [ 'put' , 'patch' ],
2023-05-23 02:11:44 +03:00
'add-user-to-task' => [ 'post' ],
'del-user' => [ 'delete' ],
2021-11-25 12:33:08 +03:00
];
}
/**
2023-04-26 01:22:02 +03:00
*
* @ OA\Post ( path = " /task/create-task " ,
* summary = " Добавить задачу " ,
* description = " Метод для создания задачи, если не передан параметр <b>user_id</b>, то будет получен текущий пользователь " ,
* security = {
* { " bearerAuth " : {}}
* },
* tags = { " TaskManager " },
*
* @ OA\RequestBody (
* @ OA\MediaType (
* mediaType = " multipart/form-data " ,
* @ OA\Schema (
* required = { " project_id " , " status " , " title " , " description " },
* @ OA\Property (
* property = " project_id " ,
* type = " string " ,
* description = " Идентификатор проекта " ,
* ),
* @ OA\Property (
* property = " title " ,
* type = " string " ,
* description = " Заголовок задачи " ,
* ),
* @ OA\Property (
* property = " description " ,
* type = " string " ,
* description = " Описание задачи " ,
* ),
* @ OA\Property (
* property = " status " ,
* type = " integer " ,
* description = " статус " ,
* ),
* @ OA\Property (
2023-06-30 18:13:40 +03:00
* property = " dead_line " ,
* type = " string " ,
* description = " Срок выполнения задачи " ,
* ),
* @ OA\Property (
2023-05-12 02:12:43 +03:00
* property = " priority " ,
* type = " integer " ,
* description = " Приоритет задачи " ,
* ),
* @ OA\Property (
2023-04-26 01:22:02 +03:00
* property = " column_id " ,
* type = " integer " ,
* description = " Колонка к которой относится задача " ,
* ),
* @ OA\Property (
* property = " user_id " ,
* type = " integer " ,
* description = " Идентификатор создателя задачи " ,
* ),
2023-05-23 14:05:09 +03:00
* @ OA\Property (
* property = " executor_id " ,
* type = " integer " ,
* description = " Идентификатор исполнителя задачи " ,
* ),
2023-04-26 01:22:02 +03:00
* ),
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " Возвращает объект задачи " ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/ProjectTask " ),
* ),
* ),
* )
*
2021-11-25 12:33:08 +03:00
* @ throws InvalidConfigException
* @ throws ServerErrorHttpException
*/
2023-01-18 15:30:38 +03:00
public function actionCreateTask () : ProjectTask
2021-11-25 12:33:08 +03:00
{
2023-04-26 01:22:02 +03:00
$request = Yii :: $app -> getRequest () -> getBodyParams ();
2023-05-23 02:11:44 +03:00
if ( ! isset ( $request [ 'user_id' ]) or $request [ 'user_id' ] == null ) {
2023-04-26 01:22:02 +03:00
$request [ 'user_id' ] = Yii :: $app -> user -> id ;
}
$taskModel = TaskService :: createTask ( $request );
2022-01-16 23:54:13 +03:00
if ( $taskModel -> errors ) {
throw new ServerErrorHttpException ( json_encode ( $taskModel -> errors ));
}
return $taskModel ;
2021-11-25 12:33:08 +03:00
}
/**
2023-04-25 01:32:15 +03:00
*
* @ OA\Get ( path = " /task/get-task-list " ,
* summary = " Получить список задач по проекту " ,
* description = " Метод для получения задач по проекту " ,
* security = {
* { " bearerAuth " : {}}
* },
* tags = { " TaskManager " },
* @ OA\Parameter (
* name = " project_id " ,
* in = " query " ,
* required = true ,
* @ OA\Schema (
* type = " integer " ,
* )
* ),
2023-10-09 23:57:01 +03:00
* @ OA\Parameter (
* name = " expand " ,
* in = " query " ,
2023-10-12 11:34:47 +03:00
* example = " column,timers,mark " ,
* description = " В этом параметре по необходимости передаются поля, которые нужно добавить в ответ сервера, сейчас доступно только поля <b>column</b>, <b>timers</b> и <b>mark</b>" ,
2023-10-09 23:57:01 +03:00
* @ OA\Schema (
* type = " string " ,
* )
* ),
2023-04-25 01:32:15 +03:00
* @ OA\Response (
* response = 200 ,
* description = " Возвращает массив объектов Задач " ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/ProjectTaskExample " ),
* ),
* ),
* )
*
2022-01-16 23:54:13 +03:00
* @ throws NotFoundHttpException
2021-11-25 12:33:08 +03:00
*/
2023-04-25 01:32:15 +03:00
public function actionGetTaskList ( $project_id ) : array
2021-11-25 12:33:08 +03:00
{
2022-01-16 23:54:13 +03:00
$tasks = array ();
2022-03-21 14:57:15 +03:00
if ( $project_id ) {
if ( empty ( $project_id ) or ! is_numeric ( $project_id )) {
2022-01-16 23:54:13 +03:00
throw new NotFoundHttpException ( 'Incorrect project ID' );
}
$tasks = TaskService :: getTaskListByProject ( $project_id );
2022-03-21 14:57:15 +03:00
} else {
2022-01-16 23:54:13 +03:00
$tasks = TaskService :: getTaskList ( $project_id );
2021-11-25 12:33:08 +03:00
}
2022-03-21 14:57:15 +03:00
if ( empty ( $tasks )) {
2021-11-25 12:33:08 +03:00
throw new NotFoundHttpException ( 'The project does not exist or there are no tasks for it' );
}
return $tasks ;
}
2023-04-25 01:32:15 +03:00
/**
*
* @ OA\Get ( path = " /task/get-user-tasks " ,
* summary = " Получить список задач по пользователю " ,
* description = " Метод для получения задач по пользователю " ,
* security = {
* { " bearerAuth " : {}}
* },
* tags = { " TaskManager " },
* @ OA\Parameter (
* name = " user_id " ,
* in = " query " ,
* required = true ,
* @ OA\Schema (
* type = " integer " ,
* )
* ),
2023-10-09 23:57:01 +03:00
* @ OA\Parameter (
* name = " expand " ,
* in = " query " ,
2023-10-12 11:34:47 +03:00
* example = " column,timers,mark " ,
* description = " В этом параметре по необходимости передаются поля, которые нужно добавить в ответ сервера, сейчас доступно только поля <b>column</b>, <b>timers</b> и <b>mark</b>" ,
2023-10-09 23:57:01 +03:00
* @ OA\Schema (
* type = " string " ,
* )
* ),
2023-04-25 01:32:15 +03:00
* @ OA\Response (
* response = 200 ,
* description = " Возвращает массив объектов Задач " ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/ProjectTaskExample " ),
* ),
* ),
* )
*
* @ param $user_id
* @ return array
* @ throws NotFoundHttpException
*/
public function actionGetUserTasks ( $user_id ) : array
{
$tasks = array ();
if ( $user_id ) {
if ( empty ( $user_id ) or ! is_numeric ( $user_id )) {
throw new NotFoundHttpException ( 'Incorrect project ID' );
}
$tasks = TaskService :: getTaskListByUser ( $user_id );
} else {
$tasks = TaskService :: getTaskList ( $user_id );
}
if ( empty ( $tasks )) {
throw new NotFoundHttpException ( 'The project does not exist or there are no tasks for it' );
}
return $tasks ;
}
2022-01-16 23:54:13 +03:00
/**
2023-04-26 01:22:02 +03:00
*
* @ OA\Get ( path = " /task/get-task " ,
* summary = " Получить информацию по задаче " ,
* description = " Метод для получения данных по задаче " ,
* security = {
* { " bearerAuth " : {}}
* },
* tags = { " TaskManager " },
* @ OA\Parameter (
* name = " task_id " ,
* in = " query " ,
* required = true ,
* @ OA\Schema (
* type = " integer " ,
* )
* ),
2023-10-09 23:57:01 +03:00
* @ OA\Parameter (
* name = " expand " ,
* in = " query " ,
2023-10-12 11:34:47 +03:00
* example = " column,timers,mark " ,
* description = " В этом параметре по необходимости передаются поля, которые нужно добавить в ответ сервера, сейчас доступно только поля <b>column</b>, <b>timers</b> и <b>mark</b>" ,
2023-10-09 23:57:01 +03:00
* @ OA\Schema (
* type = " string " ,
* )
* ),
2023-04-26 01:22:02 +03:00
* @ OA\Response (
* response = 200 ,
* description = " Возвращает объект Задачи " ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/ProjectTask " ),
* ),
* ),
* )
*
2022-01-16 23:54:13 +03:00
* @ throws NotFoundHttpException
*/
2023-01-18 15:30:38 +03:00
public function actionGetTask ( $task_id ) : ProjectTask
2021-11-25 12:33:08 +03:00
{
2022-03-21 14:57:15 +03:00
if ( empty ( $task_id ) or ! is_numeric ( $task_id )) {
2021-11-25 12:33:08 +03:00
throw new NotFoundHttpException ( 'Incorrect task ID' );
}
2022-01-16 23:54:13 +03:00
$task = TaskService :: getTask ( $task_id );
2022-03-21 14:57:15 +03:00
if ( empty ( $task )) {
2021-11-25 12:33:08 +03:00
throw new NotFoundHttpException ( 'The task does not exist' );
}
return $task ;
}
2022-01-16 23:54:13 +03:00
/**
2023-04-26 01:22:02 +03:00
*
* @ OA\Put ( path = " /task/update-task " ,
* summary = " Редактировать задачу " ,
* description = " Метод для редактирования задачи " ,
* security = {
* { " bearerAuth " : {}}
* },
* tags = { " TaskManager " },
*
* @ OA\RequestBody (
* @ OA\MediaType (
* mediaType = " application/x-www-form-urlencoded " ,
* @ OA\Schema (
* required = { " task_id " },
* @ OA\Property (
* property = " user_id " ,
* type = " integer " ,
* description = " Идентификатор пользователя " ,
* nullable = false ,
* ),
* @ OA\Property (
2023-05-23 14:05:09 +03:00
* property = " executor_id " ,
* type = " integer " ,
* description = " Идентификатор исполнителя задачи " ,
* ),
* @ OA\Property (
2023-04-26 01:22:02 +03:00
* property = " task_id " ,
* type = " integer " ,
* description = " Идентификатор задачи " ,
* ),
* @ OA\Property (
* property = " title " ,
* type = " string " ,
* description = " Заголовок задачи " ,
* ),
* @ OA\Property (
* property = " column_id " ,
* type = " integer " ,
* description = " Идентификатор колонки " ,
* ),
* @ OA\Property (
2023-05-12 02:12:43 +03:00
* property = " priority " ,
* type = " integer " ,
* description = " Приоритет задачи " ,
* ),
* @ OA\Property (
2023-04-26 01:22:02 +03:00
* property = " status " ,
* type = " integer " ,
2023-05-12 02:12:43 +03:00
* description = " Статус задачи " ,
2023-04-26 01:22:02 +03:00
* ),
* @ OA\Property (
2023-06-30 18:13:40 +03:00
* property = " dead_line " ,
* type = " string " ,
* description = " Срок выполнения задачи " ,
* ),
* @ OA\Property (
2023-04-26 01:22:02 +03:00
* property = " description " ,
* type = " string " ,
* description = " Описание запроса " ,
* ),
* ),
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " Возвращает объект Задачи " ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/ProjectTask " ),
* ),
* ),
* )
*
2022-01-16 23:54:13 +03:00
* @ throws InvalidConfigException
* @ throws ServerErrorHttpException
* @ throws NotFoundHttpException
*/
2023-04-26 01:22:02 +03:00
public function actionUpdateTask () : ? ProjectTask
2021-11-25 12:33:08 +03:00
{
2023-04-26 01:22:02 +03:00
$params = array_diff ( \Yii :: $app -> request -> getBodyParams (), [ null , '' ]);
2022-03-21 14:57:15 +03:00
if ( empty ( $params [ 'task_id' ]) or ! TaskService :: taskExists ( $params [ 'task_id' ])) {
2022-01-16 23:54:13 +03:00
throw new NotFoundHttpException ( 'The task does not exist' );
}
2021-11-25 12:33:08 +03:00
2022-01-16 23:54:13 +03:00
$modelTask = TaskService :: updateTask ( $params );
if ( ! empty ( $modelTask -> hasErrors ())) {
2023-04-26 01:22:02 +03:00
throw new ServerErrorHttpException ( json_encode ( $modelTask -> errors ));
2022-01-16 23:54:13 +03:00
}
return $modelTask ;
2021-11-25 12:33:08 +03:00
}
2023-05-23 02:11:44 +03:00
/**
*
* @ OA\Post ( path = " /task/add-user-to-task " ,
* summary = " Добавить пользователя в задачу " ,
* description = " Метод для добавления пользователя в задачу " ,
* security = {
* { " bearerAuth " : {}}
* },
* tags = { " TaskManager " },
*
* @ OA\RequestBody (
* @ OA\MediaType (
* mediaType = " multipart/form-data " ,
* @ OA\Schema (
* required = { " user_id " , " task_id " },
* @ OA\Property (
* property = " user_id " ,
* type = " integer " ,
* description = " Идентификатор пользователя " ,
* ),
* @ OA\Property (
* property = " task_id " ,
* type = " integer " ,
* description = " Идентификатор задачи " ,
* ),
* ),
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " Возвращает объект связи задачи и пользователя " ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/ProjectTaskUser " ),
* ),
* ),
* )
*
* @ return ProjectTaskUser
* @ throws NotFoundHttpException
* @ throws ServerErrorHttpException
*/
public function actionAddUserToTask () : ProjectTaskUser
{
$request = \Yii :: $app -> request -> post ();
$user = User :: findOne ( $request [ 'user_id' ]);
if ( ! $user ) {
throw new NotFoundHttpException ( 'User not found' );
}
if ( empty ( $request [ 'task_id' ]) or ! TaskService :: taskExists ( $request [ 'task_id' ])) {
throw new NotFoundHttpException ( 'The task does not exist' );
}
if ( ProjectTaskUser :: find () -> where ([ 'user_id' => $request [ 'user_id' ], 'task_id' => $request [ 'task_id' ]]) -> exists ()) {
throw new ServerErrorHttpException ( 'The user has already been added' );
}
$model = new ProjectTaskUser ();
$model -> load ( $request , '' );
if ( ! $model -> validate ()) {
throw new ServerErrorHttpException ( $model -> errors );
}
$model -> save ();
return $model ;
}
/**
*
* @ OA\Delete ( path = " /task/del-user " ,
* summary = " Удаление пользователя из задачи " ,
* description = " Метод для Удаления пользователя из задачи " ,
* security = {
* { " bearerAuth " : {}}
* },
* tags = { " TaskManager " },
*
* @ OA\RequestBody (
* @ OA\MediaType (
* mediaType = " application/x-www-form-urlencoded " ,
* @ OA\Schema (
* required = { " task_id " , " user_id " },
* @ OA\Property (
* property = " task_id " ,
* type = " integer " ,
* description = " Идентификатор задачи " ,
* ),
* @ OA\Property (
* property = " user_id " ,
* type = " integer " ,
* description = " Идентификатор пользователя " ,
* ),
* ),
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " Возвращает объект задачи " ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/ProjectTask " ),
* ),
* ),
* )
*
* @ return ProjectTask | null
* @ throws InvalidConfigException
* @ throws NotFoundHttpException
*/
public function actionDelUser () : ? ProjectTask
{
$request = Yii :: $app -> request -> getBodyParams ();
$user = User :: findOne ( $request [ 'user_id' ]);
if ( ! $user ) {
throw new NotFoundHttpException ( 'User not found' );
}
if ( empty ( $request [ 'task_id' ]) or ! TaskService :: taskExists ( $request [ 'task_id' ])) {
throw new NotFoundHttpException ( 'The task does not exist' );
}
ProjectTaskUser :: deleteAll ([ 'task_id' => $request [ 'task_id' ], 'user_id' => $request [ 'user_id' ]]);
return TaskService :: getTask ( $request [ 'task_id' ]);
}
/**
*
* @ OA\Post ( path = " /task/set-priority " ,
* summary = " Установить приоритет задач " ,
* description = " Метод для установления приоритета задач в колонке " ,
* security = {
* { " bearerAuth " : {}}
* },
* tags = { " TaskManager " },
*
* @ OA\RequestBody (
* @ OA\MediaType (
* mediaType = " multipart/form-data " ,
* @ OA\Schema (
* required = { " column_id " , " data " },
* @ OA\Property (
* property = " column_id " ,
* type = " integer " ,
* description = " Идентификатор проекта " ,
* ),
* @ OA\Property (
* property = " data " ,
* type = " string " ,
* description = " Данные для обновления приоритета. Пример: [ { "task_id":3,"priority":2}, { "task_id":4,"priority":3}] " ,
* ),
* ),
* ),
* ),
* @ OA\Response (
* response = 200 ,
* description = " Возвращает объект колонки " ,
* @ OA\MediaType (
* mediaType = " application/json " ,
* @ OA\Schema ( ref = " #/components/schemas/ProjectColumn " ),
* ),
* ),
* )
*
* @ return ProjectColumn
* @ throws BadRequestHttpException
* @ throws NotFoundHttpException
*/
public function actionSetPriority () : ProjectColumn
{
$request = \Yii :: $app -> request -> post ();
$data = $request [ 'data' ];
if ( ! $data = json_decode ( $data , true )) {
throw new BadRequestHttpException ( 'No valid JSON' );
}
$column = ProjectColumn :: findOne ( $request [ 'column_id' ]);
if ( empty ( $column )) {
throw new NotFoundHttpException ( 'The column not found' );
}
foreach ( $data as $datum ) {
$model = ProjectTask :: findOne ( $datum [ 'task_id' ]);
$model -> priority = $datum [ 'priority' ];
if ( ! $model -> validate ()){
throw new BadRequestHttpException ( $model -> errors );
}
$model -> save ();
}
return $column ;
}
2021-11-25 12:33:08 +03:00
}