From 18d2b4327d5d1df91b3ab33abed54624cb226397 Mon Sep 17 00:00:00 2001 From: iironside Date: Tue, 15 Mar 2022 17:56:45 +0300 Subject: [PATCH] refactor api/UserQuestionnaire --- common/services/UserQuestionnaireService.php | 29 ++++++++++ .../UserQuestionnaireController.php | 53 +++++-------------- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/common/services/UserQuestionnaireService.php b/common/services/UserQuestionnaireService.php index 9efaf69..9a8022d 100644 --- a/common/services/UserQuestionnaireService.php +++ b/common/services/UserQuestionnaireService.php @@ -2,7 +2,36 @@ namespace common\services; +use common\models\UserQuestionnaire; +use yii\web\NotFoundHttpException; + class UserQuestionnaireService { + public static function getQuestionnaireList($user_id): array + { + $userQuestionnaireModels = UserQuestionnaire::findActiveUserQuestionnaires($user_id); + array_walk($userQuestionnaireModels, function (&$arr) { + unset( + $arr['questionnaire_id'], + $arr['created_at'], + $arr['updated_at'], + $arr['id'], + ); + }); + return $userQuestionnaireModels; + } + /** + * @throws NotFoundHttpException + */ + public static function calculateScore($user_questionnaire_uuid) + { + $userQuestionnaireModel = UserQuestionnaire::findOne(['uuid' => $user_questionnaire_uuid]); + if(empty($userQuestionnaireModel)) { + throw new NotFoundHttpException('The questionnaire with this uuid does not exist'); + } + ScoreCalculatorService::rateResponses($userQuestionnaireModel); + ScoreCalculatorService::calculateScore($userQuestionnaireModel); + return $userQuestionnaireModel; + } } \ No newline at end of file diff --git a/frontend/modules/api/controllers/UserQuestionnaireController.php b/frontend/modules/api/controllers/UserQuestionnaireController.php index 79e450a..4dfe8cd 100644 --- a/frontend/modules/api/controllers/UserQuestionnaireController.php +++ b/frontend/modules/api/controllers/UserQuestionnaireController.php @@ -2,11 +2,9 @@ namespace frontend\modules\api\controllers; -use common\services\ScoreCalculatorService; -use common\models\UserQuestionnaire; -use Yii; -use yii\filters\auth\HttpBearerAuth; +use common\services\UserQuestionnaireService; use yii\web\NotFoundHttpException; +use yii\web\ServerErrorHttpException; class UserQuestionnaireController extends ApiController { @@ -21,51 +19,28 @@ class UserQuestionnaireController extends ApiController /** * @throws NotFoundHttpException */ - public function actionQuestionnairesList()//: array + public function actionQuestionnairesList($user_id): array { - $user_id = Yii::$app->request->get('user_id'); - - if(empty($user_id) or !is_numeric($user_id)) - { + if (empty($user_id) or !is_numeric($user_id)) { throw new NotFoundHttpException('Incorrect user ID'); } - - $userQuestionnaireModels = UserQuestionnaire::findActiveUserQuestionnaires($user_id); + $userQuestionnaireModels = UserQuestionnaireService::getQuestionnaireList($user_id); if(empty($userQuestionnaireModels)) { throw new NotFoundHttpException('Active questionnaire not found'); } - - array_walk( $userQuestionnaireModels, function(&$arr){ - unset( - $arr['questionnaire_id'], -// $arr['created_at'], -// $arr['updated_at'], - $arr['id'], - ); - }); - - return $userQuestionnaireModels; + return $userQuestionnaireModels; } - public function actionQuestionnaireCompleted() + /** + * @throws NotFoundHttpException + * @throws ServerErrorHttpException + */ + public function actionQuestionnaireCompleted($user_questionnaire_uuid) { -// return Yii::$app->request; - $user_questionnaire_uuid = Yii::$app->request->get('user_questionnaire_uuid'); - - if(empty($user_questionnaire_uuid)) - { - throw new NotFoundHttpException('Incorrect user ID'); + $userQuestionnaireModel = UserQuestionnaireService::calculateScore($user_questionnaire_uuid); + if ($userQuestionnaireModel->errors) { + throw new ServerErrorHttpException(json_encode($userQuestionnaireModel->errors)); } - - $userQuestionnaireModel = UserQuestionnaire::findOne(['uuid' => $user_questionnaire_uuid]); - - if(empty($userQuestionnaireModel)) { - throw new NotFoundHttpException('Active questionnaire not found'); - } - - ScoreCalculatorService::rateResponses($userQuestionnaireModel); - ScoreCalculatorService::calculateScore($userQuestionnaireModel); - return $userQuestionnaireModel; } }