From 50b9722e82e115faa8778ec955d40c9f806cf55e Mon Sep 17 00:00:00 2001 From: Kavalar Date: Wed, 26 Apr 2023 01:22:02 +0300 Subject: [PATCH] swagger tasks --- common/models/Project.php | 2 +- common/models/ProjectColumn.php | 2 +- common/models/ProjectTask.php | 13 ++ common/services/TaskService.php | 6 +- .../controllers/ProjectColumnController.php | 72 +++++++- .../api/controllers/TaskController.php | 155 +++++++++++++++++- 6 files changed, 242 insertions(+), 8 deletions(-) diff --git a/common/models/Project.php b/common/models/Project.php index dced535..0292bd7 100755 --- a/common/models/Project.php +++ b/common/models/Project.php @@ -106,7 +106,7 @@ class Project extends \yii\db\ActiveRecord */ public function getColumns() { - return $this->hasMany(ProjectColumn::class, ['project_id' => 'id'])->with('tasks'); + return $this->hasMany(ProjectColumn::class, ['project_id' => 'id'])->with('tasks')->where(['status' => ProjectColumn::STATUS_ACTIVE]); } /** diff --git a/common/models/ProjectColumn.php b/common/models/ProjectColumn.php index 167884d..b1539d0 100644 --- a/common/models/ProjectColumn.php +++ b/common/models/ProjectColumn.php @@ -123,6 +123,6 @@ class ProjectColumn extends \yii\db\ActiveRecord */ public function getTasks() { - return $this->hasMany(ProjectTask::class, ['column_id' => 'id'])->with('taskUsers'); + return $this->hasMany(ProjectTask::class, ['column_id' => 'id'])->with('taskUsers')->where(['status' => ProjectTask::STATUS_ACTIVE]); } } diff --git a/common/models/ProjectTask.php b/common/models/ProjectTask.php index 4cb140a..8bf9115 100644 --- a/common/models/ProjectTask.php +++ b/common/models/ProjectTask.php @@ -28,6 +28,8 @@ use yii\helpers\ArrayHelper; */ class ProjectTask extends ActiveRecord { + const STATUS_ACTIVE = 1; + const STATUS_DISABLE = 0; /** * {@inheritdoc} */ @@ -103,6 +105,17 @@ class ProjectTask extends ActiveRecord ]; } + /** + * @return string[] + */ + public static function getStatus(): array + { + return [ + self::STATUS_ACTIVE => 'Активен', + self::STATUS_DISABLE => 'Выключен' + ]; + } + public function beforeDelete() { foreach ($this->taskUsers as $taskUser) { diff --git a/common/services/TaskService.php b/common/services/TaskService.php index 2086697..88f0d33 100644 --- a/common/services/TaskService.php +++ b/common/services/TaskService.php @@ -2,7 +2,10 @@ namespace common\services; +use common\classes\Debug; use common\models\ProjectTask; +use common\models\ProjectTaskUser; +use common\models\ProjectUser; class TaskService { @@ -31,7 +34,8 @@ class TaskService public static function getTaskListByUser($user_id): array { - return ProjectTask::find()->where(['user_id' => $user_id])->all(); + $taskIdList = ProjectTaskUser::find()->where(['user_id' => $user_id])->select('task_id')->column(); + return ProjectTask::find()->where([ 'IN', 'id', $taskIdList])->orWhere(['user_id' => $user_id])->all(); } public static function updateTask($task_params): ?ProjectTask diff --git a/frontend/modules/api/controllers/ProjectColumnController.php b/frontend/modules/api/controllers/ProjectColumnController.php index de61792..047fa5a 100644 --- a/frontend/modules/api/controllers/ProjectColumnController.php +++ b/frontend/modules/api/controllers/ProjectColumnController.php @@ -62,7 +62,7 @@ class ProjectColumnController extends ApiController throw new BadRequestHttpException(json_encode(['Проект не найден'])); } - $columns = \frontend\modules\api\models\ProjectColumn::find()->where(['project_id' => $project_id])->all(); + $columns = \frontend\modules\api\models\ProjectColumn::find()->where(['project_id' => $project_id, 'status' => ProjectColumn::STATUS_ACTIVE])->all(); return $columns; @@ -121,9 +121,79 @@ class ProjectColumnController extends ApiController } + /** + * + * @OA\Put(path="/project-column/update-column", + * summary="Редактировать колонку", + * description="Метод для редактирования колонки", + * security={ + * {"bearerAuth": {}} + * }, + * tags={"TaskManager"}, + * + * @OA\RequestBody( + * @OA\MediaType( + * mediaType="application/x-www-form-urlencoded", + * @OA\Schema( + * required={"column_id"}, + * @OA\Property( + * property="column_id", + * type="integer", + * description="Идентификатор колонки", + * nullable=false, + * ), + * @OA\Property( + * property="title", + * type="string", + * description="Название колонки", + * ), + * @OA\Property( + * property="project_id", + * type="integer", + * description="Идентификатор проекта", + * ), + * @OA\Property( + * property="status", + * type="integer", + * description="Статус колонки", + * ), + * ), + * ), + * ), + * @OA\Response( + * response=200, + * description="Возвращает объект Колонки", + * @OA\MediaType( + * mediaType="application/json", + * @OA\Schema(ref="#/components/schemas/ProjectColumn"), + * ), + * ), + * ) + * + * @return ProjectColumn|null + * @throws BadRequestHttpException + * @throws \yii\base\InvalidConfigException + */ public function actionUpdateColumn() { + $column_id = \Yii::$app->request->getBodyParam('column_id'); + if (!$column_id) { + throw new BadRequestHttpException(json_encode(['Column not found'])); + } + $column = ProjectColumn::find()->where(['id' => $column_id, 'status' => ProjectColumn::STATUS_ACTIVE]); + + $put = array_diff(\Yii::$app->request->getBodyParams(), [null, '']); + + $column->load($put, ''); + + if (!$column->validate()){ + throw new BadRequestHttpException(json_encode($column->errors)); + } + + $column->save(false); + + return $column; } } \ No newline at end of file diff --git a/frontend/modules/api/controllers/TaskController.php b/frontend/modules/api/controllers/TaskController.php index 5ad05dd..2d80c55 100644 --- a/frontend/modules/api/controllers/TaskController.php +++ b/frontend/modules/api/controllers/TaskController.php @@ -2,6 +2,7 @@ namespace frontend\modules\api\controllers; +use common\classes\Debug; use common\models\ProjectTask; use common\services\TaskService; use Yii; @@ -23,12 +24,74 @@ class TaskController extends ApiController } /** + * + * @OA\Post(path="/task/create-task", + * summary="Добавить задачу", + * description="Метод для создания задачи, если не передан параметр user_id, то будет получен текущий пользователь", + * 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( + * property="column_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"), + * ), + * ), + * ) + * * @throws InvalidConfigException * @throws ServerErrorHttpException */ public function actionCreateTask(): ProjectTask { - $taskModel = TaskService::createTask(Yii::$app->getRequest()->getBodyParams()); + $request = Yii::$app->getRequest()->getBodyParams(); + if(!isset($request['user_id']) or $request['user_id'] == null){ + $request['user_id'] = Yii::$app->user->id; + } + + $taskModel = TaskService::createTask($request); if ($taskModel->errors) { throw new ServerErrorHttpException(json_encode($taskModel->errors)); } @@ -133,6 +196,32 @@ class TaskController extends ApiController } /** + * + * @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", + * ) + * ), + * @OA\Response( + * response=200, + * description="Возвращает объект Задачи", + * @OA\MediaType( + * mediaType="application/json", + * @OA\Schema(ref="#/components/schemas/ProjectTask"), + * ), + * ), + * ) + * * @throws NotFoundHttpException */ public function actionGetTask($task_id): ProjectTask @@ -150,20 +239,78 @@ class TaskController extends ApiController } /** + * + * @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( + * property="task_id", + * type="integer", + * description="Идентификатор задачи", + * ), + * @OA\Property( + * property="title", + * type="string", + * description="Заголовок задачи", + * ), + * @OA\Property( + * property="column_id", + * type="integer", + * description="Идентификатор колонки", + * ), + * @OA\Property( + * property="status", + * type="integer", + * description="Статус запроса", + * ), + * @OA\Property( + * property="description", + * type="string", + * description="Описание запроса", + * ), + * ), + * ), + * ), + * @OA\Response( + * response=200, + * description="Возвращает объект Задачи", + * @OA\MediaType( + * mediaType="application/json", + * @OA\Schema(ref="#/components/schemas/ProjectTask"), + * ), + * ), + * ) + * * @throws InvalidConfigException * @throws ServerErrorHttpException * @throws NotFoundHttpException */ - public function actionUpdate(): ?ProjectTask + public function actionUpdateTask(): ?ProjectTask { - $params = Yii::$app->request->getBodyParams(); + $params = array_diff(\Yii::$app->request->getBodyParams(), [null, '']); if (empty ($params['task_id']) or !TaskService::taskExists($params['task_id'])) { throw new NotFoundHttpException('The task does not exist'); } $modelTask = TaskService::updateTask($params); if (!empty($modelTask->hasErrors())) { - throw new ServerErrorHttpException(json_encode('Bad params')); + throw new ServerErrorHttpException(json_encode($modelTask->errors)); } return $modelTask;