guild/frontend/modules/api/controllers/ReportsController.php
2022-12-16 18:54:13 +03:00

178 lines
4.9 KiB
PHP
Executable File

<?php
namespace frontend\modules\api\controllers;
use common\behaviors\GsCors;
use common\classes\Debug;
use common\models\Reports;
use common\models\ReportsTask;
use common\models\UserCard;
use frontend\modules\api\models\ReportSearchForm;
use JsonException;
use Yii;
use yii\db\Expression;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\ContentNegotiator;
use yii\helpers\ArrayHelper;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;
class ReportsController extends ApiController
{
public function init()
{
parent::init(); // TODO: Change the autogenerated stub
}
public function behaviors()
{
$parent = parent::behaviors();
$b = [
[
'class' => ContentNegotiator::className(),
'formats' => [
'application/json' => Response::FORMAT_JSON,
],
],
'authenticator' => [
'class' => CompositeAuth::class,
'authMethods' => [
HttpBearerAuth::class,
],
]
];
return array_merge($parent, $b);
}
public function actionIndex(): array
{
$reportsModel = new ReportSearchForm();
$params = Yii::$app->request->get();
if(!isset($params['user_card_id'])){
$userCard = UserCard::find()->where(['id_user' => Yii::$app->user->id])->one();
if($userCard){
$params['user_card_id'] = $userCard->id;
}
}
$reportsModel->attributes = $params;
if(!$reportsModel->validate()){
return $reportsModel->errors;
}
return $reportsModel->byParams();
}
public function actionView($id): array{
$report = Reports::findOne($id);
return array_merge($report->toArray(), ['tasks' => $report->_task]);
}
/**
* @throws NotFoundHttpException
*/
public function actionFindByDate(): array
{
$reportsModel = new ReportSearchForm();
$params = Yii::$app->request->get();
if(!isset($params['user_card_id']) or !isset($params['date'])){
throw new NotFoundHttpException('Required parameter are missing!');
}
$reportsModel->attributes = $params;
$reportsModel->byDate = true;
if(!$reportsModel->validate()){
return $reportsModel->errors;
}
return $reportsModel->byParams();
}
public function actionCreate()
{
$params = Yii::$app->request->post();
if (!isset($params['tasks'])){
throw new BadRequestHttpException('Нет параметра tasks');
}
if(!isset($params['user_card_id'])){
/** @var UserCard $userCard */
$userCard = UserCard::find()->where(['id_user' => Yii::$app->user->id])->one();
if($userCard){
$userCardId = $userCard->id;
} else {
throw new BadRequestHttpException('User not found!');
}
} else {
$userCardId = $params['user_card_id'];
}
$reports = [];
foreach ($params['tasks'] as $task) {
$report = new Reports();
$report->load($task);
$report->difficulties = $report->difficulties ?? $params['difficulties'];
$report->tomorrow = $report->difficulties ?? $params['tomorrow'];
$report->today = $report->today ?? $params['today'];
$report->user_card_id = $report->user_card_id ?? $userCardId;
$report->created_at = date('Y-m-d');
if ($report->validate() && !$report->save()) {
throw new BadRequestHttpException(json_encode($report->errors));
}
$reports[] = $report;
}
return array_merge($reports);
}
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();
return $reportsModel->toArray();
}
}