diff --git a/common/services/TemplateService.php b/common/services/TemplateService.php index d9b928a..c3d2d11 100644 --- a/common/services/TemplateService.php +++ b/common/services/TemplateService.php @@ -19,11 +19,7 @@ class TemplateService public static function getTemplateWithFields($template_id): array { return Template::find() -// ->select('title') ->joinWith('templateDocumentFields.field') -// ->with([ -// 'fields' => function ($query) { $query->select(['id', 'title', 'field_template']); } -// ]) ->where(['template.id' => $template_id]) ->asArray() ->one(); diff --git a/common/services/UserQuestionnaireService.php b/common/services/UserQuestionnaireService.php new file mode 100644 index 0000000..9efaf69 --- /dev/null +++ b/common/services/UserQuestionnaireService.php @@ -0,0 +1,8 @@ +load($userResponseParams, ''); + (new UserResponseService)->validateResponseModel($userResponse); + (new UserResponseService)->saveModel($userResponse); + return $userResponse; + } + + /** + * @throws BadRequestHttpException + * @throws ServerErrorHttpException + */ + public static function createUserResponses($userResponsesParams): array + { + $userResponseModels = array(); + foreach ($userResponsesParams['userResponses'] as $userResponseParams) { + $model = new UserResponse(); + $model->load($userResponseParams, ''); + (new UserResponseService)->validateResponseModel($model); + + array_push($userResponseModels, $model); + } + + foreach ($userResponseModels as $responseModel) { + (new UserResponseService)->saveModel($responseModel); + } + + return $userResponseModels; + } + + /** + * @throws BadRequestHttpException + */ + protected function validateResponseModel($model) + { + if (!$model->validate()) { + throw new BadRequestHttpException(json_encode($model->errors)); + } + + if (empty($model->user_id) or empty($model->question_id) or empty($model->user_questionnaire_uuid)) { + throw new BadRequestHttpException(json_encode('One of the parameters is empty!')); + } + } + + /** + * @throws ServerErrorHttpException + */ + protected function saveModel($model) + { + if ($model->save()) { + ScoreCalculatorService::rateOneResponse($model); + $response = Yii::$app->getResponse(); + $response->setStatusCode(201); + } elseif (!$model->hasErrors()) { + throw new ServerErrorHttpException('Failed to create the object for unknown reason.'); + } + } +} \ No newline at end of file diff --git a/frontend/modules/api/controllers/UserQuestionnaireController.php b/frontend/modules/api/controllers/UserQuestionnaireController.php index 1d77ccf..79e450a 100644 --- a/frontend/modules/api/controllers/UserQuestionnaireController.php +++ b/frontend/modules/api/controllers/UserQuestionnaireController.php @@ -2,26 +2,14 @@ namespace frontend\modules\api\controllers; -use common\helpers\ScoreCalculatorHelper; +use common\services\ScoreCalculatorService; use common\models\UserQuestionnaire; use Yii; use yii\filters\auth\HttpBearerAuth; -use yii\rest\Controller; use yii\web\NotFoundHttpException; class UserQuestionnaireController extends ApiController { - public function behaviors() - { - $behaviors = parent::behaviors(); - - $behaviors['authenticator']['authMethods'] = [ - HttpBearerAuth::className(), - ]; - - return $behaviors; - } - public function verbs() { return [ @@ -75,8 +63,8 @@ class UserQuestionnaireController extends ApiController throw new NotFoundHttpException('Active questionnaire not found'); } - ScoreCalculatorHelper::rateResponses($userQuestionnaireModel); - ScoreCalculatorHelper::calculateScore($userQuestionnaireModel); + ScoreCalculatorService::rateResponses($userQuestionnaireModel); + ScoreCalculatorService::calculateScore($userQuestionnaireModel); return $userQuestionnaireModel; } diff --git a/frontend/modules/api/controllers/UserResponseController.php b/frontend/modules/api/controllers/UserResponseController.php index 15533fc..694cd31 100644 --- a/frontend/modules/api/controllers/UserResponseController.php +++ b/frontend/modules/api/controllers/UserResponseController.php @@ -2,8 +2,8 @@ namespace frontend\modules\api\controllers; -use common\services\ScoreCalculatorService; use common\models\UserResponse; +use common\services\UserResponseService; use Yii; use yii\base\InvalidConfigException; use yii\web\BadRequestHttpException; @@ -11,8 +11,6 @@ use yii\web\ServerErrorHttpException; class UserResponseController extends ApiController { - public $modelClass = 'common\models\UserResponse'; - public function verbs(): array { return [ @@ -21,76 +19,31 @@ class UserResponseController extends ApiController ]; } - /** * @throws InvalidConfigException - * @throws BadRequestHttpException - * @throws ServerErrorHttpException + * @throws ServerErrorHttpException|BadRequestHttpException */ - public function actionSetResponse() + public function actionSetResponse(): UserResponse { - $request = Yii::$app->getRequest()->getBodyParams(); - - $model = new UserResponse(); - $model->load($request, ''); - - $this->validateResponseModel($model); - $this->saveModel($model); - - return $model; + $userResponseModel = UserResponseService::createUserResponse(Yii::$app->getRequest()->getBodyParams()); + if ($userResponseModel->errors) { + throw new ServerErrorHttpException(json_encode($userResponseModel->errors)); + } + return $userResponseModel; } /** * @throws InvalidConfigException - * @throws ServerErrorHttpException - * @throws BadRequestHttpException + * @throws ServerErrorHttpException|BadRequestHttpException */ public function actionSetResponses(): array { - $requests = Yii::$app->getRequest()->getBodyParams(); - - $responseModels = array(); - - foreach ($requests['userResponses'] as $request) { - $model = new UserResponse(); - $model->load($request, ''); - $this->validateResponseModel($model); - - array_push($responseModels, $model); - } - - foreach ($responseModels as $responseModel) { - $this->saveModel($responseModel); - } - - return $responseModels; - } - - /** - * @throws BadRequestHttpException - */ - protected function validateResponseModel($model) - { - if(!$model->validate()) { - throw new BadRequestHttpException(json_encode($model->errors)); - } - - if (empty($model->user_id) or empty($model->question_id) or empty($model->user_questionnaire_uuid)) { - throw new BadRequestHttpException(json_encode($model->errors)); - } - } - - /** - * @throws ServerErrorHttpException - */ - protected function saveModel($model) - { - if ($model->save()) { - ScoreCalculatorService::rateOneResponse($model); - $response = Yii::$app->getResponse(); - $response->setStatusCode(201); - } elseif (!$model->hasErrors()) { - throw new ServerErrorHttpException('Failed to create the object for unknown reason.'); + $userResponseModels = UserResponseService::createUserResponses(Yii::$app->getRequest()->getBodyParams()); + foreach ($userResponseModels as $model) { + if ($model->errors) { + throw new ServerErrorHttpException(json_encode($model->errors)); + } } + return $userResponseModels; } }