From 6385787702352e67a36d24ec9c9e8a8d1b3fb8d1 Mon Sep 17 00:00:00 2001 From: iIronside Date: Wed, 19 Oct 2022 15:28:09 +0300 Subject: [PATCH 1/2] fix questionnaire and profile errors format --- common/services/ProfileService.php | 13 +++---- common/services/UserQuestionnaireService.php | 4 +-- .../api/controllers/ProfileController.php | 36 +++++++++++++++---- .../UserQuestionnaireController.php | 26 +++++++++----- .../modules/api/models/ProfileSearchForm.php | 2 +- 5 files changed, 56 insertions(+), 25 deletions(-) diff --git a/common/services/ProfileService.php b/common/services/ProfileService.php index bd90b60..909e07f 100644 --- a/common/services/ProfileService.php +++ b/common/services/ProfileService.php @@ -16,11 +16,11 @@ class ProfileService /** * @throws ServerErrorHttpException */ - public static function getMainData($user_id)//: array + public static function getMainData($user_id): array { $userCard = UserCard::findOne(['id_user' => $user_id]); if (empty($userCard)) { - throw new ServerErrorHttpException(json_encode('Profile not found!')); + throw new ServerErrorHttpException('Profile not found!'); } return array('fio' => $userCard->fio, 'photo' => $userCard->photo, @@ -31,10 +31,7 @@ class ProfileService 'position_name' => $userCard->position->name); } - /** - * @throws BadRequestHttpException - */ - public static function getProfile($id, $request)//: ?array + public static function getProfile($id, $request): ?array { $searchModel = new ProfileSearchForm(); $searchModel->attributes = $request; @@ -46,7 +43,7 @@ class ProfileService } /** - * @throws BadRequestHttpException + * @throws ServerErrorHttpException */ public static function getProfileWithReportPermission($user_card_id): ?array { @@ -59,7 +56,7 @@ class ProfileService self::addPermission($profile, $user_card_id); return $profile; } - throw new BadRequestHttpException(json_encode('There is no user with this id')); + throw new ServerErrorHttpException('There is no user with this id'); } private static function addPermission(&$profile, $user_card_id) diff --git a/common/services/UserQuestionnaireService.php b/common/services/UserQuestionnaireService.php index c5cfa3d..a21410b 100644 --- a/common/services/UserQuestionnaireService.php +++ b/common/services/UserQuestionnaireService.php @@ -51,7 +51,7 @@ class UserQuestionnaireService { $userQuestionnaireModel = UserQuestionnaire::findOne(['uuid' => $user_questionnaire_uuid]); if (empty($userQuestionnaireModel)) { - throw new ServerErrorHttpException(json_encode('Not found UserQuestionnaire')); + throw new ServerErrorHttpException('Not found UserQuestionnaire'); } $count = Question::find() ->where(['questionnaire_id' => $userQuestionnaireModel->questionnaire_id]) @@ -67,7 +67,7 @@ class UserQuestionnaireService { $userQuestionnaireModel = UserQuestionnaire::findOne(['uuid' => $user_questionnaire_uuid]); if (empty($userQuestionnaireModel)) { - throw new ServerErrorHttpException(json_encode('Not found UserQuestionnaire')); + throw new ServerErrorHttpException('Not found UserQuestionnaire'); } $pointSum = Question::find() ->where(['questionnaire_id' => $userQuestionnaireModel->questionnaire_id]) diff --git a/frontend/modules/api/controllers/ProfileController.php b/frontend/modules/api/controllers/ProfileController.php index 70c3a7c..c6e9a4f 100755 --- a/frontend/modules/api/controllers/ProfileController.php +++ b/frontend/modules/api/controllers/ProfileController.php @@ -3,22 +3,46 @@ namespace frontend\modules\api\controllers; use common\services\ProfileService; +use yii\helpers\ArrayHelper; use yii\web\BadRequestHttpException; +use yii\web\NotFoundHttpException; use yii\web\ServerErrorHttpException; class ProfileController extends ApiController { - public function verbs(): array +// public function verbs(): array +// { +// return [ +// '' => ['get'], +// 'profile-with-report-permission' => ['post', 'patch'] +// ]; +// } + + public function behaviors(): array { - return [ - '' => ['get'], - 'profile-with-report-permission' => ['post', 'patch'] - ]; + return ArrayHelper::merge(parent::behaviors(), [ + + 'verbs' => [ + 'class' => \yii\filters\VerbFilter::class, + 'actions' => [ + '' => ['get'], + 'profile-with-report-permission' => ['post', 'patch'], + 'get-main-data' => ['get'] + ], + ] + ]); } + /** + * @throws NotFoundHttpException + */ public function actionIndex($id = null): ?array { - return ProfileService::getProfile($id, \Yii::$app->request->get()); + $profiles = ProfileService::getProfile($id, \Yii::$app->request->get()); + if(empty($profiles)) { + throw new NotFoundHttpException('Profiles not found'); + } + return $profiles; } /** diff --git a/frontend/modules/api/controllers/UserQuestionnaireController.php b/frontend/modules/api/controllers/UserQuestionnaireController.php index 3a2c875..a434170 100644 --- a/frontend/modules/api/controllers/UserQuestionnaireController.php +++ b/frontend/modules/api/controllers/UserQuestionnaireController.php @@ -3,17 +3,27 @@ namespace frontend\modules\api\controllers; use common\services\UserQuestionnaireService; +use yii\helpers\ArrayHelper; use yii\web\NotFoundHttpException; use yii\web\ServerErrorHttpException; class UserQuestionnaireController extends ApiController { - public function verbs() + + public function behaviors(): array { - return [ - 'questionnaires-list' => ['get'], - 'questionnaire-completed' => ['get'], - ]; + return ArrayHelper::merge(parent::behaviors(), [ + + 'verbs' => [ + 'class' => \yii\filters\VerbFilter::class, + 'actions' => [ + 'questionnaires-list' => ['get'], + 'questionnaire-completed' => ['get'], + 'get-points-number' => ['get'], + 'get-question-number' => ['get'], + ], + ] + ]); } /** @@ -39,7 +49,7 @@ class UserQuestionnaireController extends ApiController { $userQuestionnaireModel = UserQuestionnaireService::calculateScore($user_questionnaire_uuid); if ($userQuestionnaireModel->errors) { - throw new ServerErrorHttpException(json_encode($userQuestionnaireModel->errors)); + throw new ServerErrorHttpException($userQuestionnaireModel->errors); } return $userQuestionnaireModel; } @@ -51,7 +61,7 @@ class UserQuestionnaireController extends ApiController { $questionPointsNumber = UserQuestionnaireService::getPointsNumber($user_questionnaire_uuid); if (empty($questionPointsNumber)) { - throw new ServerErrorHttpException(json_encode('Question points not found!')); + throw new ServerErrorHttpException('Question points not found!'); } return $questionPointsNumber; } @@ -63,7 +73,7 @@ class UserQuestionnaireController extends ApiController { $questionNumber = UserQuestionnaireService::getQuestionNumber($user_questionnaire_uuid); if (empty($questionNumber)) { - throw new ServerErrorHttpException(json_encode('Question number not found!')); + throw new ServerErrorHttpException('Question number not found!'); } return $questionNumber; } diff --git a/frontend/modules/api/models/ProfileSearchForm.php b/frontend/modules/api/models/ProfileSearchForm.php index e9d0f09..b7ca82f 100755 --- a/frontend/modules/api/models/ProfileSearchForm.php +++ b/frontend/modules/api/models/ProfileSearchForm.php @@ -86,7 +86,7 @@ class ProfileSearchForm extends Model $model->andWhere(['status' => [4, 12]]); $model->andWhere(['deleted_at' => null]); - $model->groupBy('card_skill.card_id'); + //$model->groupBy('card_skill.card_id'); $res = $model->limit($this->limit) ->offset($this->offset)->orderBy('updated_at DESC')->asArray()->all(); From f41b1615a58044e73eb8942091f43d03c8c80cbc Mon Sep 17 00:00:00 2001 From: iIronside Date: Wed, 19 Oct 2022 15:42:26 +0300 Subject: [PATCH 2/2] rollback actionIndex in ProfileController --- .../modules/api/controllers/ProfileController.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/frontend/modules/api/controllers/ProfileController.php b/frontend/modules/api/controllers/ProfileController.php index c6e9a4f..0369ecc 100755 --- a/frontend/modules/api/controllers/ProfileController.php +++ b/frontend/modules/api/controllers/ProfileController.php @@ -10,13 +10,6 @@ use yii\web\ServerErrorHttpException; class ProfileController extends ApiController { -// public function verbs(): array -// { -// return [ -// '' => ['get'], -// 'profile-with-report-permission' => ['post', 'patch'] -// ]; -// } public function behaviors(): array { @@ -38,11 +31,7 @@ class ProfileController extends ApiController */ public function actionIndex($id = null): ?array { - $profiles = ProfileService::getProfile($id, \Yii::$app->request->get()); - if(empty($profiles)) { - throw new NotFoundHttpException('Profiles not found'); - } - return $profiles; + return ProfileService::getProfile($id, \Yii::$app->request->get()); } /**