guild/frontend/modules/api/controllers/TaskController.php

171 lines
4.9 KiB
PHP
Raw Normal View History

2021-11-25 12:33:08 +03:00
<?php
namespace frontend\modules\api\controllers;
use common\models\ProjectTask;
2022-01-16 23:54:13 +03:00
use common\services\TaskService;
2021-11-25 12:33:08 +03:00
use Yii;
use yii\base\InvalidConfigException;
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'],
];
}
/**
* @throws InvalidConfigException
* @throws ServerErrorHttpException
*/
public function actionCreateTask(): ProjectTask
2021-11-25 12:33:08 +03:00
{
2022-01-16 23:54:13 +03:00
$taskModel = TaskService::createTask(Yii::$app->getRequest()->getBodyParams());
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",
* )
* ),
* @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",
* )
* ),
* @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
/**
* @throws NotFoundHttpException
*/
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
/**
* @throws InvalidConfigException
* @throws ServerErrorHttpException
* @throws NotFoundHttpException
*/
public function actionUpdate(): ?ProjectTask
2021-11-25 12:33:08 +03:00
{
2022-01-16 23:54:13 +03:00
$params = Yii::$app->request->getBodyParams();
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())) {
throw new ServerErrorHttpException(json_encode('Bad params'));
}
return $modelTask;
2021-11-25 12:33:08 +03:00
}
}