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; namespace frontend\modules\api\controllers;
use common\services\UserResponseService; use frontend\modules\api\services\UserResponseService;
use Yii; use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\web\BadRequestHttpException; use yii\web\BadRequestHttpException;

View File

@ -98,5 +98,13 @@ namespace frontend\modules\api\models;
*/ */
class UserResponse extends \common\models\UserResponse 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 <?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;
use yii\web\BadRequestHttpException; use yii\web\BadRequestHttpException;
use yii\web\ServerErrorHttpException; use yii\web\ServerErrorHttpException;
@ -16,10 +17,16 @@ class UserResponseService
public static function createUserResponses($userResponsesParams): array public static function createUserResponses($userResponsesParams): array
{ {
$userResponseModels = array(); $userResponseModels = array();
foreach ($userResponsesParams['userResponses'] as $userResponseParams) { foreach ($userResponsesParams as $userResponse) {
$model = new UserResponse(); $model = new UserResponse();
$model->load($userResponseParams, ''); $model->load($userResponse, '');
(new UserResponseService)->validateResponseModel($model);
try {
self::validateResponseModel($model);
} catch (\Exception $ex) {
throw new BadRequestHttpException(json_encode('One of the parameters is empty!'));
}
array_push($userResponseModels, $model); array_push($userResponseModels, $model);
} }
@ -34,7 +41,7 @@ class UserResponseService
/** /**
* @throws BadRequestHttpException * @throws BadRequestHttpException
*/ */
protected function validateResponseModel($model) protected static function validateResponseModel($model)
{ {
if (!$model->validate()) { if (!$model->validate()) {
throw new BadRequestHttpException(json_encode($model->errors)); throw new BadRequestHttpException(json_encode($model->errors));