From 525dbda65717798c887688ebe4723af22630fa7a Mon Sep 17 00:00:00 2001 From: Kavalar Date: Thu, 27 Apr 2023 01:53:21 +0300 Subject: [PATCH] project task fix --- common/models/ProjectTask.php | 35 ++++++++++- ..._add_executor_id_to_project_task_table.php | 42 +++++++++++++ .../api/controllers/ProjectController.php | 63 ++++++++++++++++++- frontend/modules/api/models/ProjectTask.php | 30 +++++++++ 4 files changed, 164 insertions(+), 6 deletions(-) create mode 100644 console/migrations/m230426_221513_add_executor_id_to_project_task_table.php diff --git a/common/models/ProjectTask.php b/common/models/ProjectTask.php index 8bf9115..475cabf 100644 --- a/common/models/ProjectTask.php +++ b/common/models/ProjectTask.php @@ -19,6 +19,7 @@ use yii\helpers\ArrayHelper; * @property string $updated_at * @property int $column_id * @property int $user_id + * @property int $executor_id * @property string $description * * @property Project $project @@ -30,6 +31,7 @@ class ProjectTask extends ActiveRecord { const STATUS_ACTIVE = 1; const STATUS_DISABLE = 0; + /** * {@inheritdoc} */ @@ -57,13 +59,14 @@ class ProjectTask extends ActiveRecord { return [ [['project_id', 'status', 'title', 'description',], 'required'], - [['project_id', 'status', 'column_id', 'user_id'], 'integer'], + [['project_id', 'status', 'column_id', 'user_id', 'executor_id'], 'integer'], [['created_at', 'updated_at'], 'safe'], ['title', 'unique', 'targetAttribute' => ['title', 'project_id'], 'message' => 'Такая задача уже создана'], [['title'], 'string', 'max' => 255], [['description'], 'string', 'max' => 500], [['project_id'], 'exist', 'skipOnError' => true, 'targetClass' => Project::className(), 'targetAttribute' => ['project_id' => 'id']], [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']], + [['executor_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['executor_id' => 'id']], [['column_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProjectColumn::className(), 'targetAttribute' => ['column_id' => 'id']], ]; } @@ -83,6 +86,7 @@ class ProjectTask extends ActiveRecord 'description' => 'Описание', 'user_id' => 'Создатель задачи', 'column_id' => 'Колонка', + 'executor_id' => 'Исполнитель', ]; } @@ -101,6 +105,23 @@ class ProjectTask extends ActiveRecord 'status', 'column_id', 'user_id', + 'user' => function () { + return [ + "fio" => $this->user->userCard->fio ?? $this->user->username, + "avatar" => $this->user->userCard->photo ?? '', + ]; + }, + 'executor_id', + 'executor' => function () { + if ($this->executor){ + return [ + "fio" => $this->executor->userCard->fio ?? $this->executor->username, + "avatar" => $this->executor->userCard->photo ?? '', + ]; + } + + return null; + }, 'taskUsers', ]; } @@ -127,7 +148,7 @@ class ProjectTask extends ActiveRecord /** * @return ActiveQuery */ - public function getProject() + public function getProject(): ActiveQuery { return $this->hasOne(Project::className(), ['id' => 'project_id']); } @@ -135,11 +156,19 @@ class ProjectTask extends ActiveRecord /** * @return ActiveQuery */ - public function getUser() + public function getUser(): ActiveQuery { return $this->hasOne(User::className(), ['id' => 'user_id']); } + /** + * @return ActiveQuery + */ + public function getExecutor(): ActiveQuery + { + return $this->hasOne(User::class, ['id' => 'executor_id']); + } + /** * @return ActiveQuery */ diff --git a/console/migrations/m230426_221513_add_executor_id_to_project_task_table.php b/console/migrations/m230426_221513_add_executor_id_to_project_task_table.php new file mode 100644 index 0000000..1b86de7 --- /dev/null +++ b/console/migrations/m230426_221513_add_executor_id_to_project_task_table.php @@ -0,0 +1,42 @@ +addColumn('project_task', 'executor_id', $this->integer(11)); + $this->addForeignKey('fk_project_task_user_executor', 'project_task', 'executor_id', 'user', 'id'); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->dropForeignKey('fk_project_task_user_executor', 'project_task'); + $this->dropColumn('project_task', 'executor_id'); + } + + /* + // Use up()/down() to run migration code without a transaction. + public function up() + { + + } + + public function down() + { + echo "m230426_221513_add_executor_id_to_project_task_table cannot be reverted.\n"; + + return false; + } + */ +} diff --git a/frontend/modules/api/controllers/ProjectController.php b/frontend/modules/api/controllers/ProjectController.php index e317d62..efed193 100644 --- a/frontend/modules/api/controllers/ProjectController.php +++ b/frontend/modules/api/controllers/ProjectController.php @@ -34,7 +34,7 @@ class ProjectController extends ApiController 'status-list' => ['GET', 'OPTIONS'], 'project-task-category-list' => ['GET', 'OPTIONS'], 'create' => ['POST', 'OPTIONS'], - 'update' => ['POST', 'OPTIONS'] + 'update' => ['PUT', 'OPTIONS'] ], ] ]); @@ -254,6 +254,58 @@ class ProjectController extends ApiController } /** + * + * @OA\PUT(path="/project/update", + * summary="Редактировать проект", + * description="Метод для редактирования проекта", + * security={ + * {"bearerAuth": {}} + * }, + * tags={"TaskManager"}, + * + * @OA\RequestBody( + * @OA\MediaType( + * mediaType="application/x-www-form-urlencoded", + * @OA\Schema( + * required={"project_id"}, + * @OA\Property( + * property="project_id", + * type="integer", + * description="Идентификатор проекта", + * ), + * @OA\Property( + * property="name", + * type="string", + * description="Название проекта", + * ), + * @OA\Property( + * property="description", + * type="string", + * description="Описание проекта", + * ), + * @OA\Property( + * property="status", + * type="integer", + * description="статус", + * ), + * @OA\Property( + * property="company_id", + * type="integer", + * description="Компания к которой относится проект", + * ), + * ), + * ), + * ), + * @OA\Response( + * response=200, + * description="Возвращает объект Проекта", + * @OA\MediaType( + * mediaType="application/json", + * @OA\Schema(ref="#/components/schemas/Project"), + * ), + * ), + * ) + * * @throws \Throwable * @throws \yii\base\InvalidConfigException * @throws \yii\db\StaleObjectException @@ -261,12 +313,17 @@ class ProjectController extends ApiController */ public function actionUpdate() { - $project = Project::findOne(Yii::$app->request->post('project_id')); + $request = Yii::$app->request->getBodyParams(); + if (!isset($request['project_id']) || $request['project_id'] == null){ + throw new BadRequestHttpException(json_encode(['The project ID not found'])); + } + $project = Project::findOne($request['project_id']); if(empty($project)) { throw new NotFoundHttpException('The project not found'); } - $project->load(Yii::$app->request->getBodyParams(), ''); + $put = array_diff($request, [null, '']); + $project->load($put, ''); if (!$project->update()) { return $project->errors; } diff --git a/frontend/modules/api/models/ProjectTask.php b/frontend/modules/api/models/ProjectTask.php index 1bc6034..53a9387 100644 --- a/frontend/modules/api/models/ProjectTask.php +++ b/frontend/modules/api/models/ProjectTask.php @@ -34,9 +34,25 @@ namespace frontend\modules\api\models; * property="user_id", * type="int", * example="19", + * description="Идентификатор пользователя создавшего задачу" + * ), + * @OA\Property( + * property="user", + * ref="#/components/schemas/ProjectTaskUsersShortExample", * description="Пользователь создавший задачу" * ), * @OA\Property( + * property="executor_id", + * type="int", + * example="2", + * description="Идентификатор исполнителя задачи" + * ), + * @OA\Property( + * property="executor", + * ref="#/components/schemas/ProjectTaskUsersShortExample", + * description="Исполнитель задачи" + * ), + * @OA\Property( * property="description", * type="string", * example="Описание задачи", @@ -97,6 +113,20 @@ namespace frontend\modules\api\models; * ), *) * + * @OA\Schema( + * schema="ProjectTaskUsersShortExample", + * @OA\Property( + * property="fio", + * type="string", + * example="Сапронов Антон Викторович" + * ), + * @OA\Property( + * property="avatar", + * type="string", + * example="/profileava/m8.png" + * ), + *) + * */ class ProjectTask extends \common\models\ProjectTask {