edit request, skills list, position list

This commit is contained in:
2023-04-13 01:09:35 +03:00
parent 8bc601aa6a
commit 7f46dc1346
6 changed files with 247 additions and 11 deletions

View File

@ -5,6 +5,7 @@ namespace frontend\modules\api\controllers;
use common\classes\Debug;
use common\models\Request;
use common\services\RequestService;
use yii\base\InvalidConfigException;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
@ -18,7 +19,7 @@ class RequestController extends ApiController
'get-request' => ['get'],
'get-request-list' => ['get'],
'create-request' => ['post'],
// 'update-task' => ['put', 'patch'],
'update-request' => ['put', 'patch'],
];
}
@ -115,7 +116,7 @@ class RequestController extends ApiController
* @param int|null $user_id
* @return array|\yii\db\ActiveRecord[]
*/
public function actionGetList(int $user_id = null, int $search_depth = 3): array
public function actionGetRequestList(int $user_id = null, int $search_depth = 3): array
{
if (!$user_id) {
$user_id = \Yii::$app->user->id;
@ -133,8 +134,8 @@ class RequestController extends ApiController
/**
*
* @OA\Post(path="/request/create-request",
* summary="Получить список запросов",
* description="Метод для оздания запроса, если не передан параметр <b>user_id</b>, то будет получен список текущего пользователя",
* summary="Добавить запрос",
* description="Метод для создания запроса, если не передан параметр <b>user_id</b>, то будет получен список текущего пользователя",
* security={
* {"bearerAuth": {}}
* },
@ -177,7 +178,7 @@ class RequestController extends ApiController
* ),
* @OA\Property(
* property="descr",
* type=" string",
* type="string",
* description="Описание запроса",
* ),
* @OA\Property(
@ -204,10 +205,10 @@ class RequestController extends ApiController
* @return Request
* @throws BadRequestHttpException
*/
public function actionCreateRequest()
public function actionCreateRequest(): Request
{
$user_id = \Yii::$app->user->id;
if (!$user_id){
if (!$user_id) {
throw new BadRequestHttpException(json_encode(['Пользователь не найден']));
}
@ -216,7 +217,118 @@ class RequestController extends ApiController
->load(\Yii::$app->request->post(), '')
->save();
if (!$requestService->isSave){
if (!$requestService->isSave) {
throw new BadRequestHttpException(json_encode($requestService->errors));
}
return $requestService->getModel();
}
/**
*
* @OA\Put(path="/request/update-request",
* summary="Редактировать запрос",
* description="Метод для редактирования запроса, если не передан параметр <b>user_id</b>, то будет получен список текущего пользователя",
* security={
* {"bearerAuth": {}}
* },
* tags={"Requests"},
*
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="application/x-www-form-urlencoded",
* @OA\Schema(
* required={"request_id"},
* @OA\Property(
* property="user_id",
* type="integer",
* description="Идентификатор пользователя",
* nullable=false,
* ),
* @OA\Property(
* property="request_id",
* type="integer",
* description="Идентификатор запросв",
* ),
* @OA\Property(
* property="title",
* type="string",
* description="Заголовок запроса",
* ),
* @OA\Property(
* property="position_id",
* type="integer",
* description="Позиция",
* ),
* @OA\Property(
* property="knowledge_level_id",
* type="integer",
* description="Уровень",
* ),
* @OA\Property(
* property="specialist_count",
* type="integer",
* description="Количество специалистов",
* ),
* @OA\Property(
* property="status",
* type="integer",
* description="Статус запроса",
* ),
* @OA\Property(
* property="descr",
* type="string",
* description="Описание запроса",
* ),
* @OA\Property(
* property="skill_ids",
* type="array",
* description="Навыки",
* @OA\Items(
* type="integer",
* ),
* ),
* ),
* ),
* ),
* @OA\Response(
* response=200,
* description="Возвращает объект Запроса",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(ref="#/components/schemas/Request"),
* ),
* ),
* )
*
* @return Request|null
* @throws BadRequestHttpException
* @throws InvalidConfigException
*/
public function actionUpdateRequest(): Request
{
$user_id = \Yii::$app->user->id;
if (!$user_id) {
throw new BadRequestHttpException(json_encode(['User not found']));
}
$request_id = \Yii::$app->request->getBodyParam('request_id');
if (!$request_id) {
throw new BadRequestHttpException(json_encode(['Request not found']));
}
$requestService = RequestService::run($request_id);
if (!$requestService->validateUser($user_id)){
throw new BadRequestHttpException(json_encode(['The user does not have the right to edit the request'], true));
}
$put = array_diff(\Yii::$app->request->getBodyParams(), [null, '']);
$requestService->load($put, '')
->save();
if (!$requestService->isSave) {
throw new BadRequestHttpException(json_encode($requestService->errors));
}