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

127 lines
3.2 KiB
PHP
Raw Normal View History

2021-07-05 15:30:35 +03:00
<?php
namespace frontend\modules\api\controllers;
use common\behaviors\GsCors;
2021-07-28 17:08:08 +03:00
use common\models\Reports;
use frontend\modules\api\models\ReportSearchForm;
use JsonException;
use Yii;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\ContentNegotiator;
use yii\rest\Controller;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;
2021-07-05 15:30:35 +03:00
2021-07-28 17:08:08 +03:00
class ReportsController extends Controller
2021-07-05 15:30:35 +03:00
{
2021-07-28 17:08:08 +03:00
public function init()
{
parent::init(); // TODO: Change the autogenerated stub
}
2021-07-05 15:30:35 +03:00
public function behaviors()
{
return [
[
2021-07-28 17:08:08 +03:00
'class' => ContentNegotiator::className(),
2021-07-05 15:30:35 +03:00
'formats' => [
2021-07-28 17:08:08 +03:00
'application/json' => Response::FORMAT_JSON,
2021-07-05 15:30:35 +03:00
],
],
'corsFilter' => [
'class' => GsCors::class,
'cors' => [
'Origin' => ['*'],
//'Access-Control-Allow-Credentials' => true,
'Access-Control-Allow-Headers' => [
'Content-Type',
'Access-Control-Allow-Headers',
'Authorization',
'X-Requested-With'
],
]
2021-07-28 17:08:08 +03:00
],
'authenticator' => [
'class' => CompositeAuth::class,
'authMethods' => [
HttpBearerAuth::class,
],
2021-07-05 15:30:35 +03:00
]
];
}
2021-07-28 17:08:08 +03:00
public function actionIndex(): array
2021-07-05 15:30:35 +03:00
{
2021-07-28 17:08:08 +03:00
$reportsModel = new ReportSearchForm();
$params = Yii::$app->request->get();
$reportsModel->attributes = $params;
if(!$reportsModel->validate()){
return $reportsModel->errors;
}
return $reportsModel->byParams();
}
public function actionCreate()
{
$reportsModel = new Reports();
$params = Yii::$app->request->get();
$reportsModel->attributes = $params;
if(!$reportsModel->validate()){
throw new BadRequestHttpException(json_encode($reportsModel->errors));
}
$reportsModel->save();
return $reportsModel->toArray();
}
public function actionDelete()
{
$id = Yii::$app->request->get('id');
$report = Reports::findOne($id);
if(null === $report) {
throw new NotFoundHttpException('Report not found');
}
if(false === ($report->delete())) {
throw new JsonException('Report not deleted');
}
return true;
}
public function actionUpdate(): array
{
$params = Yii::$app->request->get();
$reportsModel = Reports::findone($params['id']);
if(!isset($reportsModel)) {
throw new NotFoundHttpException('report not found');
}
if(isset($params['user_card_id'])) {
throw new JsonException('constraint by user_card_id');
}
$reportsModel->attributes = $params;
if(!$reportsModel->validate()){
throw new BadRequestHttpException(json_encode($reportsModel->errors));
}
$reportsModel->save();
2021-07-05 15:30:35 +03:00
2021-07-28 17:08:08 +03:00
return $reportsModel->toArray();
2021-07-05 15:30:35 +03:00
}
}