refactor api/UserResponse
This commit is contained in:
parent
675d78b958
commit
a5ef1d5008
@ -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();
|
||||
|
8
common/services/UserQuestionnaireService.php
Normal file
8
common/services/UserQuestionnaireService.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace common\services;
|
||||
|
||||
class UserQuestionnaireService
|
||||
{
|
||||
|
||||
}
|
74
common/services/UserResponseService.php
Normal file
74
common/services/UserResponseService.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace common\services;
|
||||
|
||||
use common\models\UserResponse;
|
||||
use Yii;
|
||||
use yii\web\BadRequestHttpException;
|
||||
use yii\web\ServerErrorHttpException;
|
||||
|
||||
class UserResponseService
|
||||
{
|
||||
/**
|
||||
* @throws BadRequestHttpException
|
||||
* @throws ServerErrorHttpException
|
||||
*/
|
||||
public static function createUserResponse($userResponseParams): UserResponse
|
||||
{
|
||||
$userResponse = new UserResponse();
|
||||
$userResponse->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.');
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user