Merge branch 'master' into update_projects_and_tasks

This commit is contained in:
iIronside 2023-01-25 10:38:17 +03:00
commit a353cbec6c
3 changed files with 52 additions and 92 deletions

View File

@ -119,7 +119,7 @@
</tr> </tr>
<tr> <tr>
<td> <td>
user_id user_card_id
</td> </td>
<td> <td>
Идентификатор карточки пользователя отчета. Идентификатор карточки пользователя отчета.
@ -130,7 +130,7 @@
Пример запроса: Пример запроса:
</p> </p>
`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}` `https://guild.craft-group.xyz/api/reports/{id}`
@ -488,7 +488,7 @@
</tr> </tr>
<tr> <tr>
<td> <td>
user_id user_card_id
</td> </td>
<td> <td>
Id пользователя. По умолчанию будет выведен список для текущего пользователя, Id пользователя. По умолчанию будет выведен список для текущего пользователя,
@ -503,7 +503,7 @@
Пример запроса: Пример запроса:
</p> </p>
`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`
<p> <p>
Возвращаемые параметры: id - идентификатор отчёта, date - дата отчёта, Возвращаемые параметры: id - идентификатор отчёта, date - дата отчёта,

View File

@ -7,72 +7,34 @@ use common\models\ReportsTask;
use common\models\UserCard; use common\models\UserCard;
use DateTime; use DateTime;
use frontend\modules\api\models\ReportSearchForm; use frontend\modules\api\models\ReportSearchForm;
use JsonException;
use Yii; use Yii;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\ContentNegotiator;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\web\BadRequestHttpException; use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\web\Response;
class ReportsController extends ApiController class ReportsController extends ApiController
{ {
public function init() public function actionIndex($user_card_id)
{
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(); $reportsModel = new ReportSearchForm();
$params = Yii::$app->request->get(); $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; $reportsModel->attributes = $params;
$reportsModel->limit = $params['limit'] ?? $reportsModel->limit;
$reportsModel->offset = $params['offset'] ?? $reportsModel->offset;
if(!$reportsModel->validate()){ if(!$reportsModel->validate()){
return $reportsModel->errors; return $reportsModel->errors;
} }
return $reportsModel->byParams();
}
public function actionView($id): array{ return $reportsModel->byParams();
$report = Reports::findOne($id);
return array_merge($report->toArray(), ['tasks' => $report->_task]);
} }
/** /**
* @throws NotFoundHttpException * @throws NotFoundHttpException
*/ */
public function actionFindByDate(): array public function actionFindByDate()//: array
{ {
$reportsModel = new ReportSearchForm(); $reportsModel = new ReportSearchForm();
@ -82,12 +44,17 @@ class ReportsController extends ApiController
} }
$reportsModel->attributes = $params; $reportsModel->attributes = $params;
$reportsModel->byDate = true; $reportsModel->date = $params['date'];
if (!$this->checkDate($reportsModel->date)) {
throw new BadRequestHttpException('Wrong date format');
}
if(!$reportsModel->validate()){ if(!$reportsModel->validate()){
return $reportsModel->errors; return $reportsModel->errors;
} }
return $reportsModel->byParams();
return $reportsModel->findByDate();
} }
public function actionCreate() public function actionCreate()
@ -137,7 +104,7 @@ class ReportsController extends ApiController
} }
if(false === ($report->delete())) { if(false === ($report->delete())) {
throw new JsonException('Report not deleted'); throw new \RuntimeException('Report not deleted');
} }
return true; return true;
@ -153,7 +120,7 @@ class ReportsController extends ApiController
} }
if(isset($params['user_card_id'])) { if(isset($params['user_card_id'])) {
throw new JsonException('constraint by user_card_id'); throw new \RuntimeException('constraint by user_card_id');
} }
$reportsModel->attributes = $params; $reportsModel->attributes = $params;
@ -170,26 +137,19 @@ class ReportsController extends ApiController
/** /**
* @throws NotFoundHttpException * @throws NotFoundHttpException
*/ */
public function actionReportsByDate($fromDate, $toDate, $user_id = null) public function actionReportsByDate($fromDate, $toDate, $user_card_id)
{ {
if (!$this->checkDate($fromDate) || !$this->checkDate($toDate)) { if (!$this->checkDate($fromDate) || !$this->checkDate($toDate)) {
throw new BadRequestHttpException('Wrong date format'); throw new BadRequestHttpException('Wrong date format');
} }
$params = Yii::$app->request->get(); $params = Yii::$app->request->get();
$userId = $user_id ?? Yii::$app->user->id;
/** @var UserCard $userCard */
$userCard = UserCard::find()->where(['id_user' => $userId])->one();
if (!$userCard) {
throw new NotFoundHttpException('User not found');
}
$reportsModel = new ReportSearchForm(); $reportsModel = new ReportSearchForm();
$reportsModel->attributes = $params; $reportsModel->attributes = $params;
$reportsModel->user_id = $userCard->id; $reportsModel->user_card_id = $user_card_id;
$reports = $reportsModel->findByDate(); $reports = $reportsModel->reportsByDate();
return ArrayHelper::toArray($reports , [ return ArrayHelper::toArray($reports , [
'common\models\Reports' => [ 'common\models\Reports' => [
'date' => 'created_at', 'date' => 'created_at',

View File

@ -4,32 +4,28 @@
namespace frontend\modules\api\models; namespace frontend\modules\api\models;
use common\models\Reports; use common\models\Reports;
use common\models\ReportsTask;
use yii\base\Model; use yii\base\Model;
/** */
class ReportSearchForm extends Model class ReportSearchForm extends Model
{ {
public $user_card_id;
public $limit; public $limit;
public $offset; public $offset;
/** @var string */
public $date;
public $fromDate; public $fromDate;
public $toDate; public $toDate;
public $user_id;
/**
* @var false
*/
public $byDate;
public $date;
public function __construct($config = []) public function __construct($config = [])
{ {
$this->limit = 10; $this->limit = 10;
$this->offset = 0; $this->offset = 0;
$this->user_id = null; $this->user_card_id = null;
$this->toDate = date('Y-m-d', time()); $this->toDate = date('Y-m-d');
$this->fromDate = date('Y-m-01', time()); $this->fromDate = date('Y-m-d');
$this->date = date('Y-m-d'); $this->date = date('Y-m-d');
$this->byDate = false;
parent::__construct($config); parent::__construct($config);
} }
@ -38,39 +34,43 @@ class ReportSearchForm extends Model
{ {
return [ return [
[['byDate'], 'safe'], [['byDate'], 'safe'],
[['fromDate', 'toDate', 'date'], 'date', 'format' => 'php:Y-m-d'], [['fromDate', 'toDate', 'date'], 'string'],
[['limit', 'offset', 'user_id'], 'integer', 'min' => 0], // [['fromDate', 'toDate', 'date'], 'date', 'format' => 'php:Y-m-d'],
[[ 'user_card_id'], 'integer', 'min' => 0],
]; ];
} }
public function byParams() public function byParams()
{ {
$queryBuilder = Reports::find() $queryBuilder = Reports::find()->with('task');
->with('task');
if ($this->byDate) { if ($this->fromDate && $this->toDate) {
$queryBuilder->andWhere(['reports.created_at' => $this->date]);
} else {
$queryBuilder->andWhere(['between', 'reports.created_at', $this->fromDate, $this->toDate]); $queryBuilder->andWhere(['between', 'reports.created_at', $this->fromDate, $this->toDate]);
} }
if (isset($this->user_card_id)) {
$queryBuilder->andWhere(['reports.user_card_id' => $this->user_card_id]);
}
$queryBuilder->limit($this->limit) $queryBuilder->limit($this->limit)
->offset($this->offset); ->offset($this->offset);
if (isset($this->user_id)) { return $queryBuilder->asArray()->all();
$queryBuilder->andWhere(['user_card_id' => $this->user_id]);
}
$data = $queryBuilder->asArray()->all();
return $data;
} }
public function findByDate(): array public function findByDate()
{ {
return Reports::find() return Reports::find()->with('task')
->where(['between', 'reports.created_at', $this->fromDate, $this->toDate]) ->where(['reports.user_card_id' => $this->user_card_id])
->andWhere(['user_card_id' => $this->user_id]) ->andWhere(['reports.created_at' => $this->date])
->all(); ->asArray()->all();
}
public function reportsByDate()
{
return Reports::find()->with('task')
->where(['reports.user_card_id' => $this->user_card_id])
->andWhere(['between', 'reports.created_at', $this->fromDate, $this->toDate])
->asArray()->all();
} }
} }