From 4c7e6f3d2849a3d1da2770b6ef597fcdf9d5f557 Mon Sep 17 00:00:00 2001 From: iIronside Date: Tue, 7 Nov 2023 16:29:52 +0300 Subject: [PATCH] fix user response --- .../controllers/UserResponseController.php | 2 +- frontend/modules/api/models/UserResponse.php | 10 +++++++++- .../api}/services/UserResponseService.php | 19 +++++++++++++------ 3 files changed, 23 insertions(+), 8 deletions(-) rename {common => frontend/modules/api}/services/UserResponseService.php (73%) diff --git a/frontend/modules/api/controllers/UserResponseController.php b/frontend/modules/api/controllers/UserResponseController.php index 7646ed8..d9ffec5 100644 --- a/frontend/modules/api/controllers/UserResponseController.php +++ b/frontend/modules/api/controllers/UserResponseController.php @@ -2,7 +2,7 @@ namespace frontend\modules\api\controllers; -use common\services\UserResponseService; +use frontend\modules\api\services\UserResponseService; use Yii; use yii\base\InvalidConfigException; use yii\web\BadRequestHttpException; diff --git a/frontend/modules/api/models/UserResponse.php b/frontend/modules/api/models/UserResponse.php index 7212f2b..7eb5f1d 100644 --- a/frontend/modules/api/models/UserResponse.php +++ b/frontend/modules/api/models/UserResponse.php @@ -98,5 +98,13 @@ namespace frontend\modules\api\models; */ class UserResponse extends \common\models\UserResponse { - + public function fields(): array + { + return [ + 'user_id', + 'question_id', + 'response_body', + 'user_questionnaire_uuid', + ]; + } } \ No newline at end of file diff --git a/common/services/UserResponseService.php b/frontend/modules/api/services/UserResponseService.php similarity index 73% rename from common/services/UserResponseService.php rename to frontend/modules/api/services/UserResponseService.php index af68613..7b41bdb 100644 --- a/common/services/UserResponseService.php +++ b/frontend/modules/api/services/UserResponseService.php @@ -1,8 +1,9 @@ load($userResponseParams, ''); - (new UserResponseService)->validateResponseModel($model); + $model->load($userResponse, ''); + + try { + self::validateResponseModel($model); + } catch (\Exception $ex) { + throw new BadRequestHttpException(json_encode('One of the parameters is empty!')); + } + array_push($userResponseModels, $model); } @@ -34,7 +41,7 @@ class UserResponseService /** * @throws BadRequestHttpException */ - protected function validateResponseModel($model) + protected static function validateResponseModel($model) { if (!$model->validate()) { throw new BadRequestHttpException(json_encode($model->errors));