guild/frontend/modules/api/controllers/UserResponseController.php

50 lines
1.4 KiB
PHP
Raw Normal View History

2021-10-28 10:51:14 +03:00
<?php
namespace frontend\modules\api\controllers;
use common\models\UserResponse;
2022-03-15 16:54:34 +03:00
use common\services\UserResponseService;
2021-10-28 10:51:14 +03:00
use Yii;
use yii\base\InvalidConfigException;
2021-10-28 10:51:14 +03:00
use yii\web\BadRequestHttpException;
use yii\web\ServerErrorHttpException;
class UserResponseController extends ApiController
2021-10-28 10:51:14 +03:00
{
public function verbs(): array
{
return [
'set-response' => ['post'],
'set-responses' => ['post'],
2021-10-28 10:51:14 +03:00
];
}
/**
* @throws InvalidConfigException
2022-03-15 16:54:34 +03:00
* @throws ServerErrorHttpException|BadRequestHttpException
*/
2022-03-15 16:54:34 +03:00
public function actionSetResponse(): UserResponse
2021-10-28 10:51:14 +03:00
{
2022-03-15 16:54:34 +03:00
$userResponseModel = UserResponseService::createUserResponse(Yii::$app->getRequest()->getBodyParams());
if ($userResponseModel->errors) {
throw new ServerErrorHttpException(json_encode($userResponseModel->errors));
}
return $userResponseModel;
}
/**
* @throws InvalidConfigException
2022-03-15 16:54:34 +03:00
* @throws ServerErrorHttpException|BadRequestHttpException
*/
public function actionSetResponses(): array
{
2022-03-15 16:54:34 +03:00
$userResponseModels = UserResponseService::createUserResponses(Yii::$app->getRequest()->getBodyParams());
foreach ($userResponseModels as $model) {
if ($model->errors) {
throw new ServerErrorHttpException(json_encode($model->errors));
}
2021-10-28 10:51:14 +03:00
}
2022-03-15 16:54:34 +03:00
return $userResponseModels;
2021-10-28 10:51:14 +03:00
}
}