2021-10-28 10:51:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace frontend\modules\api\controllers;
|
|
|
|
|
|
|
|
use common\models\UserResponse;
|
|
|
|
use Yii;
|
2021-11-12 14:30:01 +03:00
|
|
|
use yii\base\InvalidConfigException;
|
2021-10-28 10:51:14 +03:00
|
|
|
use yii\filters\auth\HttpBearerAuth;
|
|
|
|
use yii\rest\ActiveController;
|
|
|
|
use yii\web\BadRequestHttpException;
|
|
|
|
use yii\web\ServerErrorHttpException;
|
|
|
|
|
2021-11-12 14:30:01 +03:00
|
|
|
class UserResponseController extends ActiveController
|
2021-10-28 10:51:14 +03:00
|
|
|
{
|
|
|
|
public $modelClass = 'common\models\UserResponse';
|
|
|
|
|
|
|
|
public function behaviors(): array
|
|
|
|
{
|
|
|
|
$behaviors = parent::behaviors();
|
|
|
|
|
|
|
|
$behaviors['authenticator']['authMethods'] = [
|
|
|
|
HttpBearerAuth::className(),
|
|
|
|
];
|
|
|
|
|
|
|
|
return $behaviors;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function verbs(): array
|
|
|
|
{
|
|
|
|
return [
|
2021-11-12 14:30:01 +03:00
|
|
|
'set-response' => ['post'],
|
2021-10-28 10:51:14 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actions()
|
|
|
|
{
|
|
|
|
$actions = parent::actions();
|
|
|
|
unset($actions['create']);
|
|
|
|
return $actions;
|
|
|
|
}
|
|
|
|
|
2021-11-12 14:30:01 +03:00
|
|
|
/**
|
|
|
|
* @throws InvalidConfigException
|
|
|
|
* @throws BadRequestHttpException
|
|
|
|
* @throws ServerErrorHttpException
|
|
|
|
*/
|
|
|
|
public function actionSetResponse(): UserResponse
|
2021-10-28 10:51:14 +03:00
|
|
|
{
|
|
|
|
$model = new UserResponse();
|
|
|
|
|
|
|
|
$model->load(Yii::$app->getRequest()->getBodyParams(), '');
|
2021-11-12 14:30:01 +03:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2021-10-28 10:51:14 +03:00
|
|
|
if ($model->save()) {
|
|
|
|
$response = Yii::$app->getResponse();
|
|
|
|
$response->setStatusCode(201);
|
|
|
|
} elseif (!$model->hasErrors()) {
|
|
|
|
throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
}
|