fix user response

This commit is contained in:
iIronside 2023-11-07 16:29:52 +03:00
parent 3d0ee63eae
commit 4c7e6f3d28
3 changed files with 23 additions and 8 deletions

View File

@ -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;

View File

@ -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',
];
}
}

View File

@ -1,8 +1,9 @@
<?php
namespace common\services;
namespace frontend\modules\api\services;
use common\models\UserResponse;
use common\services\ScoreCalculatorService;
use frontend\modules\api\models\UserResponse;
use Yii;
use yii\web\BadRequestHttpException;
use yii\web\ServerErrorHttpException;
@ -16,10 +17,16 @@ class UserResponseService
public static function createUserResponses($userResponsesParams): array
{
$userResponseModels = array();
foreach ($userResponsesParams['userResponses'] as $userResponseParams) {
foreach ($userResponsesParams as $userResponse) {
$model = new UserResponse();
$model->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));