refactor api/UserQuestionnaire

This commit is contained in:
iironside 2022-03-15 17:56:45 +03:00
parent a5ef1d5008
commit 18d2b4327d
2 changed files with 43 additions and 39 deletions

View File

@ -2,7 +2,36 @@
namespace common\services;
use common\models\UserQuestionnaire;
use yii\web\NotFoundHttpException;
class UserQuestionnaireService
{
public static function getQuestionnaireList($user_id): array
{
$userQuestionnaireModels = UserQuestionnaire::findActiveUserQuestionnaires($user_id);
array_walk($userQuestionnaireModels, function (&$arr) {
unset(
$arr['questionnaire_id'],
$arr['created_at'],
$arr['updated_at'],
$arr['id'],
);
});
return $userQuestionnaireModels;
}
/**
* @throws NotFoundHttpException
*/
public static function calculateScore($user_questionnaire_uuid)
{
$userQuestionnaireModel = UserQuestionnaire::findOne(['uuid' => $user_questionnaire_uuid]);
if(empty($userQuestionnaireModel)) {
throw new NotFoundHttpException('The questionnaire with this uuid does not exist');
}
ScoreCalculatorService::rateResponses($userQuestionnaireModel);
ScoreCalculatorService::calculateScore($userQuestionnaireModel);
return $userQuestionnaireModel;
}
}

View File

@ -2,11 +2,9 @@
namespace frontend\modules\api\controllers;
use common\services\ScoreCalculatorService;
use common\models\UserQuestionnaire;
use Yii;
use yii\filters\auth\HttpBearerAuth;
use common\services\UserQuestionnaireService;
use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException;
class UserQuestionnaireController extends ApiController
{
@ -21,51 +19,28 @@ class UserQuestionnaireController extends ApiController
/**
* @throws NotFoundHttpException
*/
public function actionQuestionnairesList()//: array
public function actionQuestionnairesList($user_id): array
{
$user_id = Yii::$app->request->get('user_id');
if(empty($user_id) or !is_numeric($user_id))
{
if (empty($user_id) or !is_numeric($user_id)) {
throw new NotFoundHttpException('Incorrect user ID');
}
$userQuestionnaireModels = UserQuestionnaire::findActiveUserQuestionnaires($user_id);
$userQuestionnaireModels = UserQuestionnaireService::getQuestionnaireList($user_id);
if(empty($userQuestionnaireModels)) {
throw new NotFoundHttpException('Active questionnaire not found');
}
array_walk( $userQuestionnaireModels, function(&$arr){
unset(
$arr['questionnaire_id'],
// $arr['created_at'],
// $arr['updated_at'],
$arr['id'],
);
});
return $userQuestionnaireModels;
return $userQuestionnaireModels;
}
public function actionQuestionnaireCompleted()
/**
* @throws NotFoundHttpException
* @throws ServerErrorHttpException
*/
public function actionQuestionnaireCompleted($user_questionnaire_uuid)
{
// return Yii::$app->request;
$user_questionnaire_uuid = Yii::$app->request->get('user_questionnaire_uuid');
if(empty($user_questionnaire_uuid))
{
throw new NotFoundHttpException('Incorrect user ID');
$userQuestionnaireModel = UserQuestionnaireService::calculateScore($user_questionnaire_uuid);
if ($userQuestionnaireModel->errors) {
throw new ServerErrorHttpException(json_encode($userQuestionnaireModel->errors));
}
$userQuestionnaireModel = UserQuestionnaire::findOne(['uuid' => $user_questionnaire_uuid]);
if(empty($userQuestionnaireModel)) {
throw new NotFoundHttpException('Active questionnaire not found');
}
ScoreCalculatorService::rateResponses($userQuestionnaireModel);
ScoreCalculatorService::calculateScore($userQuestionnaireModel);
return $userQuestionnaireModel;
}
}