From 097ecfe0875c7e3d44c1fbb05987f7c3ef016cbe Mon Sep 17 00:00:00 2001 From: iIronside Date: Thu, 19 Jan 2023 14:30:49 +0300 Subject: [PATCH] fix actionFindByDate in ReportsController --- docs/api/reports.md | 8 ++++---- .../api/controllers/ReportsController.php | 18 +++++++++++++----- .../modules/api/models/ReportSearchForm.php | 18 +++++++++--------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/docs/api/reports.md b/docs/api/reports.md index 32feef2..1d71804 100644 --- a/docs/api/reports.md +++ b/docs/api/reports.md @@ -119,7 +119,7 @@ - user_id + user_card_id Идентификатор карточки пользователя отчета. @@ -130,7 +130,7 @@ Пример запроса:

-`https://guild.craft-group.xyz/api/reports/index?fromDate=2021-08-01&toDate=2021-08-31&user_id=2&limit=3&offset=2` +`https://guild.craft-group.xyz/api/reports/index?fromDate=2021-08-01&toDate=2021-08-31&user_card_id=2&limit=3&offset=2` ### Один отчет `https://guild.craft-group.xyz/api/reports/{id}` @@ -488,7 +488,7 @@ - user_id + user_card_id Id пользователя. По умолчанию будет выведен список для текущего пользователя, @@ -503,7 +503,7 @@ Пример запроса:

-`http://guild.loc/api/reports/reports-by-date?fromDate=2022-12-1&toDate=2022-12-31&user_id=1 task&status=1` +`http://guild.loc/api/reports/reports-by-date?fromDate=2022-12-1&toDate=2022-12-31&user_card_id=1 task&status=1`

Возвращаемые параметры: id - идентификатор отчёта, date - дата отчёта, diff --git a/frontend/modules/api/controllers/ReportsController.php b/frontend/modules/api/controllers/ReportsController.php index 0e683df..37411e4 100755 --- a/frontend/modules/api/controllers/ReportsController.php +++ b/frontend/modules/api/controllers/ReportsController.php @@ -72,7 +72,7 @@ class ReportsController extends ApiController /** * @throws NotFoundHttpException */ - public function actionFindByDate(): array + public function actionFindByDate()//: array { $reportsModel = new ReportSearchForm(); @@ -82,11 +82,19 @@ class ReportsController extends ApiController } $reportsModel->attributes = $params; - $reportsModel->byDate = true; + $reportsModel->date = $params['date']; + + if (!$this->checkDate($reportsModel->date)) { + throw new BadRequestHttpException('Wrong date format'); + } + +// return $reportsModel->date; +// return $reportsModel->user_card_id; if(!$reportsModel->validate()){ return $reportsModel->errors; } + return $reportsModel->findByDate(); } @@ -170,14 +178,14 @@ class ReportsController extends ApiController /** * @throws NotFoundHttpException */ - public function actionReportsByDate($fromDate, $toDate, $user_id = null) + public function actionReportsByDate($fromDate, $toDate, $user_card_id = null) { if (!$this->checkDate($fromDate) || !$this->checkDate($toDate)) { throw new BadRequestHttpException('Wrong date format'); } $params = Yii::$app->request->get(); - $userId = $user_id ?? Yii::$app->user->id; + $userId = $user_card_id ?? Yii::$app->user->id; /** @var UserCard $userCard */ $userCard = UserCard::find()->where(['id_user' => $userId])->one(); @@ -187,7 +195,7 @@ class ReportsController extends ApiController $reportsModel = new ReportSearchForm(); $reportsModel->attributes = $params; - $reportsModel->user_id = $userCard->id; + $reportsModel->user_card_id = $userCard->id; $reports = $reportsModel->findByDate(); return ArrayHelper::toArray($reports , [ diff --git a/frontend/modules/api/models/ReportSearchForm.php b/frontend/modules/api/models/ReportSearchForm.php index 5359698..cb83a4a 100755 --- a/frontend/modules/api/models/ReportSearchForm.php +++ b/frontend/modules/api/models/ReportSearchForm.php @@ -13,7 +13,7 @@ class ReportSearchForm extends Model public $offset; public $fromDate; public $toDate; - public $user_id; + public $user_card_id; /** * @var false */ @@ -24,10 +24,10 @@ class ReportSearchForm extends Model { $this->limit = 10; $this->offset = 0; - $this->user_id = null; + $this->user_card_id = null; $this->toDate = date('Y-m-d', time()); - $this->fromDate = date('Y-m-01', time()); + $this->fromDate = date('Y-m-d', time()); $this->date = date('Y-m-d'); $this->byDate = false; @@ -38,8 +38,8 @@ class ReportSearchForm extends Model { return [ [['byDate'], 'safe'], - [['fromDate', 'toDate', 'date'], 'date', 'format' => 'php:Y-m-d'], - [['limit', 'offset', 'user_id'], 'integer', 'min' => 0], +// [['fromDate', 'toDate', 'date'], 'date', 'format' => 'Y-m-d'], + [['limit', 'offset', 'user_card_id'], 'integer', 'min' => 0], ]; } @@ -57,8 +57,8 @@ class ReportSearchForm extends Model $queryBuilder->limit($this->limit) ->offset($this->offset); - if (isset($this->user_id)) { - $queryBuilder->andWhere(['user_card_id' => $this->user_id]); + if (isset($this->user_card_id)) { + $queryBuilder->andWhere(['user_card_id' => $this->user_card_id]); } $data = $queryBuilder->asArray()->all(); @@ -69,8 +69,8 @@ class ReportSearchForm extends Model public function findByDate(): array { return Reports::find() - ->where(['between', 'reports.created_at', $this->fromDate, $this->toDate]) - ->andWhere(['user_card_id' => $this->user_id]) + ->where(['user_card_id' => $this->user_card_id]) + ->andWhere(['created_at' => $this->date]) ->all(); } } \ No newline at end of file