refactoring InterviewRequest, Profile

This commit is contained in:
iironside
2022-03-21 14:56:21 +03:00
parent 55089accb5
commit e00f2b84ea
4 changed files with 139 additions and 128 deletions

View File

@ -0,0 +1,33 @@
<?php
namespace frontend\modules\api\controllers;
use common\models\InterviewRequest;
use common\services\InterviewRequestService;
use Yii;
use yii\base\InvalidConfigException;
use yii\web\ServerErrorHttpException;
class InterviewRequestController extends ApiController
{
public function verbs(): array
{
return [
'create-interview-request' => ['post']
];
}
/**
* @throws InvalidConfigException
* @throws ServerErrorHttpException
*/
public function actionCreateInterviewRequest(): InterviewRequest
{
$InterviewRequestModel = InterviewRequestService::createInterviewRequest(Yii::$app->getRequest()->getBodyParams());
if ($InterviewRequestModel->errors) {
throw new ServerErrorHttpException(json_encode($InterviewRequestModel->errors));
}
return $InterviewRequestModel;
}
}

View File

@ -2,117 +2,29 @@
namespace frontend\modules\api\controllers;
use common\behaviors\GsCors;
use common\classes\Debug;
use common\models\InterviewRequest;
use common\models\User;
use common\models\UserCard;
use common\services\ProfileService;
use frontend\modules\api\models\ProfileSearchForm;
use kavalar\BotNotificationTemplateProcessor;
use kavalar\TelegramBotService;
use Yii;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\auth\QueryParamAuth;
use yii\filters\ContentNegotiator;
use yii\helpers\ArrayHelper;
use yii\web\BadRequestHttpException;
use yii\web\Response;
class ProfileController extends ApiController
{
public function behaviors()
public function verbs(): array
{
$parent = parent::behaviors();
$b = [
[
'class' => ContentNegotiator::className(),
'formats' => [
'application/json' => Response::FORMAT_JSON,
],
],
'authenticator' => [
'class' => CompositeAuth::class,
'authMethods' => [
HttpBearerAuth::class,
],
]
return [
'' => ['get'],
'profile-with-report-permission' => ['post', 'patch']
];
return array_merge($parent, $b);
}
public function actionIndex($id = null)
{
$searchModel = new ProfileSearchForm();
$searchModel->attributes = \Yii::$app->request->get();
if ($id) {
return $searchModel->byId();
}
return $searchModel->byParams();
return ProfileService::getProfile($id, \Yii::$app->request->get());
}
public function actionProfileWithReportPermission($id)
/**
* @throws BadRequestHttpException
*/
public function actionProfileWithReportPermission($id): ?array
{
$searchModel = new ProfileSearchForm();
$searchModel->attributes = \Yii::$app->request->get();
$searcherUser = Yii::$app->user->getId();
$searcherProfileId = UserCard::findOne($searcherUser)->id;
if ($id && $searcherProfileId) {
if(!UserCard::find()->where(['id' => $id])->exists())
{
throw new BadRequestHttpException(json_encode('There is no user with this id'));
}
$profile = $searchModel->byId();
$profileService = new ProfileService($searcherProfileId, $id);
if($profileService->checkReportePermission()) {
$profile += ['report_permission' => '1'];
}
else {
$profile += ['report_permission' => '0'];
}
return $profile;
}
throw new BadRequestHttpException(json_encode('Missing required parameter'));
return ProfileService::getProfileWithReportPermission($id);
}
public function actionAddToInterview()
{
if (\Yii::$app->request->isPost) {
$attributes = \Yii::$app->request->post();
$model = new InterviewRequest();
$model->attributes = $attributes;
$model->created_at = time();
$model->user_id = \Yii::$app->user->id;
if ($model->save()) {
\Yii::$app->telegram_bot->sendRenderedMessage('interview_request', $attributes);
return ['status' => 'success'];
}
\Yii::$app->response->statusCode = 400;
return ['status' => 'error', 'errors' => $model->errors];
}
}
public function actionMe()
{
if(isset(\Yii::$app->user->id)){
$user = User::find()->with('userCard')->where(['id' => \Yii::$app->user->id])->one();
}
\Yii::$app->response->statusCode = 401;
return ['status' => 'error', 'errors' => 'No authorized'];
}
}
}