add api reports search

This commit is contained in:
kirill 2021-07-28 17:08:08 +03:00
parent b3979773b4
commit 1a2347ef99
3 changed files with 153 additions and 6 deletions

View File

@ -3,16 +3,32 @@
namespace frontend\modules\api\controllers;
use common\behaviors\GsCors;
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;
class ReportsController extends \yii\rest\Controller
class ReportsController extends Controller
{
public function init()
{
parent::init(); // TODO: Change the autogenerated stub
}
public function behaviors()
{
return [
[
'class' => \yii\filters\ContentNegotiator::className(),
'class' => ContentNegotiator::className(),
'formats' => [
'application/json' => \yii\web\Response::FORMAT_JSON,
'application/json' => Response::FORMAT_JSON,
],
],
'corsFilter' => [
@ -27,13 +43,84 @@ class ReportsController extends \yii\rest\Controller
'X-Requested-With'
],
]
],
'authenticator' => [
'class' => CompositeAuth::class,
'authMethods' => [
HttpBearerAuth::class,
],
]
];
}
public function actionIndex()
public function actionIndex(): array
{
$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();
return $reportsModel->toArray();
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace frontend\modules\api\models;
use common\models\Reports;
use frontend\modules\card\models\UserCard;
use yii\base\Model;
class ReportSearchForm extends Model
{
public $limit;
public $offset;
public $fromDate;
public $toDate;
public $user_id;
public function __construct($config = [])
{
$this->limit = 10;
$this->offset = 0;
$this->user_id = null;
$this->toDate = date('Y-m-d', time());
$this->fromDate = date('Y-m-01', time());
parent::__construct($config);
}
public function rules(): array
{
return [
[['fromDate', 'toDate'], 'date', 'format' => 'php:Y-m-d'],
[['limit', 'offset', 'user_id'], 'integer', 'min' => 0],
];
}
public function byParams()
{
$queryBuilder = Reports::find()
->andWhere(['between', 'created_at', $this->fromDate, $this->toDate, $this->user_id])
->limit($this->limit)
->offset($this->offset);
if(isset($this->user_id)) {
$userCardId = UserCard::findByUserId($this->user_id)->id;
$queryBuilder->andWhere(['user_card_id' => $userCardId]);
}
$data = $queryBuilder->all();
return $data;
}
}

View File

@ -3,6 +3,7 @@
namespace frontend\modules\card\models;
use common\models\CardSkill;
use Yii;
use yii\helpers\ArrayHelper;
class UserCard extends \common\models\UserCard
@ -15,7 +16,7 @@ class UserCard extends \common\models\UserCard
parent::init();
$skill = ArrayHelper::getColumn(
CardSkill::find()->where(['card_id' => \Yii::$app->request->get('id')])->all(),
CardSkill::find()->where(['card_id' => Yii::$app->request->get('id')])->all(),
'skill_id'
);
@ -26,7 +27,7 @@ class UserCard extends \common\models\UserCard
public function afterSave($insert, $changedAttributes)
{
$post = \Yii::$app->request->post('UserCard');
$post = Yii::$app->request->post('UserCard');
if ($post['skill']) {
CardSkill::deleteAll(['card_id' => $this->id]);
@ -42,4 +43,9 @@ class UserCard extends \common\models\UserCard
parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
}
public static function findByUserId($userId): ?UserCard
{
return self::findOne(['id_user' => $userId]);
}
}