From 4ae43ff2daaf33ae46eb17ecb8190bc83a0d5f87 Mon Sep 17 00:00:00 2001 From: Kavalar Date: Wed, 17 May 2023 01:14:54 +0300 Subject: [PATCH] comments entity --- common/models/ProjectTask.php | 3 + .../m230511_205501_create_comment_table.php | 2 +- .../api/controllers/CommentController.php | 114 +++++++++++++++++- frontend/modules/api/models/Comment.php | 8 ++ frontend/modules/api/models/ProjectColumn.php | 8 +- frontend/modules/api/models/ProjectTask.php | 6 + 6 files changed, 135 insertions(+), 6 deletions(-) diff --git a/common/models/ProjectTask.php b/common/models/ProjectTask.php index 9ce8abe..2010e12 100644 --- a/common/models/ProjectTask.php +++ b/common/models/ProjectTask.php @@ -125,6 +125,9 @@ class ProjectTask extends ActiveRecord return null; }, + 'comment_count' => function(){ + return Comment::find()->where(['entity_id' => $this->id, 'entity_type' => 2, 'status' => Comment::STATUS_ACTIVE])->count(); + }, 'taskUsers', ]; } diff --git a/console/migrations/m230511_205501_create_comment_table.php b/console/migrations/m230511_205501_create_comment_table.php index 7ca937e..a1e6b68 100644 --- a/console/migrations/m230511_205501_create_comment_table.php +++ b/console/migrations/m230511_205501_create_comment_table.php @@ -20,7 +20,7 @@ class m230511_205501_create_comment_table extends Migration 'parent_id' => $this->integer(11), 'entity_type' => $this->integer(2), 'entity_id' => $this->integer(11), - 'status' => $this->integer(1), + 'status' => $this->integer(1)->defaultValue(1), 'text' => $this->text() ]); } diff --git a/frontend/modules/api/controllers/CommentController.php b/frontend/modules/api/controllers/CommentController.php index bee22e3..ae0febb 100644 --- a/frontend/modules/api/controllers/CommentController.php +++ b/frontend/modules/api/controllers/CommentController.php @@ -12,6 +12,7 @@ class CommentController extends ApiController return [ 'get-entity-type-list' => ['get'], 'create' => ['post'], + 'update' => ['put', 'patch'], ]; } @@ -126,11 +127,122 @@ class CommentController extends ApiController $model->load($request, ''); - if (!$model->save()){ + if (!$model->save()) { return $model->errors; } return $model; } + /** + * + * @OA\Put(path="/comment/update", + * summary="Редактировать комментария", + * description="Метод для редактирования комментария", + * security={ + * {"bearerAuth": {}} + * }, + * tags={"Comment"}, + * + * @OA\RequestBody( + * @OA\MediaType( + * mediaType="application/x-www-form-urlencoded", + * @OA\Schema( + * required={"comment_id", "text"}, + * @OA\Property( + * property="comment_id", + * type="integer", + * description="Идентификатор комментария", + * ), + * @OA\Property( + * property="text", + * type="string", + * description="Текст комментария", + * ), + * @OA\Property( + * property="status", + * type="integer", + * description="статус", + * ), + * ), + * ), + * ), + * @OA\Response( + * response=200, + * description="Возвращает объект Комментария", + * @OA\MediaType( + * mediaType="application/json", + * @OA\Schema(ref="#/components/schemas/Comment"), + * ), + * ), + * ) + * + * @return Comment + * @throws BadRequestHttpException + * @throws \yii\base\InvalidConfigException + */ + public function actionUpdate(): Comment + { + $user_id = \Yii::$app->user->id; + if (!$user_id) { + throw new BadRequestHttpException(json_encode(['User not found'])); + } + + $comment_id = \Yii::$app->request->getBodyParam('comment_id'); + $model = Comment::findOne($comment_id); + if (!$model) { + throw new BadRequestHttpException(json_encode(['Comment not found'])); + } + + $put = array_diff(\Yii::$app->request->getBodyParams(), [null, '']); + $model->load($put, ''); + + + if(!$model->validate()){ + throw new BadRequestHttpException($model->errors); + } + $model->save(); + + return $model; + } + + /** + * + * @OA\Get(path="/comment/get-by-entity", + * summary="Получить комментарии по идентификатору сущности", + * description="Метод для получения комментариев по идентификатору сущности.", + * security={ + * {"bearerAuth": {}} + * }, + * tags={"Comment"}, + * @OA\Parameter( + * name="entity_id", + * in="query", + * required=true, + * @OA\Schema( + * type="integer", + * default=null + * ) + * ), + * + * @OA\Response( + * response=200, + * description="Возвращает массив объектов Комментариев", + * @OA\MediaType( + * mediaType="application/json", + * @OA\Schema(ref="#/components/schemas/CommentExample"), + * ), + * ), + * ) + * + * @param int $entity_id + * @return array|\yii\db\ActiveRecord[] + */ + public function actionGetByEntity(int $entity_id): array + { + $model = Comment::find()->where(['entity_id' => $entity_id, 'status' => Comment::STATUS_ACTIVE])->all(); + + return $model; + } + } \ No newline at end of file diff --git a/frontend/modules/api/models/Comment.php b/frontend/modules/api/models/Comment.php index 0d9e2d5..d9a4e70 100644 --- a/frontend/modules/api/models/Comment.php +++ b/frontend/modules/api/models/Comment.php @@ -56,6 +56,14 @@ namespace frontend\modules\api\models; * ), *) * + * @OA\Schema( + * schema="CommentExample", + * type="array", + * @OA\Items( + * ref="#/components/schemas/Comment", + * ), + *) + * */ class Comment extends \common\models\Comment { diff --git a/frontend/modules/api/models/ProjectColumn.php b/frontend/modules/api/models/ProjectColumn.php index 8407cb1..9f03e26 100644 --- a/frontend/modules/api/models/ProjectColumn.php +++ b/frontend/modules/api/models/ProjectColumn.php @@ -48,13 +48,13 @@ namespace frontend\modules\api\models; * example={ * {"id": 1, "title": "Задачи на проверку", "project_id": 95, "status": 1, "priority": 1, * "tasks": { - * {"id": 95, "title": "Сложная задача", "project_id": 44, "column_id": 1, "user_id": 19, "description": "Описание задачи", "status": 1, + * {"id": 95, "title": "Сложная задача", "project_id": 44, "column_id": 1, "user_id": 19, "description": "Описание задачи", "status": 1, "comment_count": 3, * "taskUsers": { * {"id": 2, "task_id": 95, "user_id": 2, "fio": "Сапронов Антон Викторович", "avatar": "/profileava/m8.png"}, * {"id": 3, "task_id": 95, "user_id": 3, "fio": "Иванов Иван Иванович", "avatar": "/profileava/m2.png"}, * } * }, - * {"id": 96, "title": "Простая задача", "project_id": 44, "column_id": 1, "user_id": 19, "description": "Описание простой задачи", "status": 1, + * {"id": 96, "title": "Простая задача", "project_id": 44, "column_id": 1, "user_id": 19, "description": "Описание простой задачи", "status": 1, "comment_count": 4, * "taskUsers": { * {"id": 3, "task_id": 96, "user_id": 3, "fio": "Иванов Иван Иванович", "avatar": "/profileava/m2.png"}, * {"id": 4, "task_id": 96, "user_id": 4, "fio": "Петров Петр Петрович", "avatar": "/profileava/m7.png"}, @@ -64,8 +64,8 @@ namespace frontend\modules\api\models; * }, * {"id": 2, "title": "Новые задачи", "project_id": 95, "status": 1, "priority": 2, * "tasks": { - * {"id": 97, "title": "Очень Сложная задача", "project_id": 44, "column_id": 2, "user_id": 19, "description": "Описание простой задачи", "status": 1}, - * {"id": 98, "title": "Очень Простая задача", "project_id": 44, "column_id": 2, "user_id": 19, "description": "Описание очень простой задачи", "status": 1} + * {"id": 97, "title": "Очень Сложная задача", "project_id": 44, "column_id": 2, "user_id": 19, "description": "Описание простой задачи", "status": 1, "comment_count": 2}, + * {"id": 98, "title": "Очень Простая задача", "project_id": 44, "column_id": 2, "user_id": 19, "description": "Описание очень простой задачи", "status": 1, "comment_count": 3} * } * } * }, diff --git a/frontend/modules/api/models/ProjectTask.php b/frontend/modules/api/models/ProjectTask.php index b382027..c142dfe 100644 --- a/frontend/modules/api/models/ProjectTask.php +++ b/frontend/modules/api/models/ProjectTask.php @@ -71,6 +71,12 @@ namespace frontend\modules\api\models; * description="Статус задачи" * ), * @OA\Property( + * property="comment_count", + * type="int", + * example="5", + * description="Кол-во комментариев" + * ), + * @OA\Property( * property="taskUsers", * ref="#/components/schemas/ProjectTaskUsersExample", * ),