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

View File

@ -7,72 +7,34 @@ use common\models\ReportsTask;
use common\models\UserCard;
use DateTime;
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\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
public function actionIndex($user_card_id)
{
$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;
$reportsModel->limit = $params['limit'] ?? $reportsModel->limit;
$reportsModel->offset = $params['offset'] ?? $reportsModel->offset;
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]);
return $reportsModel->byParams();
}
/**
* @throws NotFoundHttpException
*/
public function actionFindByDate(): array
public function actionFindByDate()//: array
{
$reportsModel = new ReportSearchForm();
@ -82,12 +44,17 @@ 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');
}
if(!$reportsModel->validate()){
return $reportsModel->errors;
}
return $reportsModel->byParams();
return $reportsModel->findByDate();
}
public function actionCreate()
@ -137,7 +104,7 @@ class ReportsController extends ApiController
}
if(false === ($report->delete())) {
throw new JsonException('Report not deleted');
throw new \RuntimeException('Report not deleted');
}
return true;
@ -153,7 +120,7 @@ class ReportsController extends ApiController
}
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;
@ -170,26 +137,19 @@ class ReportsController extends ApiController
/**
* @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)) {
throw new BadRequestHttpException('Wrong date format');
}
$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->attributes = $params;
$reportsModel->user_id = $userCard->id;
$reportsModel->user_card_id = $user_card_id;
$reports = $reportsModel->findByDate();
$reports = $reportsModel->reportsByDate();
return ArrayHelper::toArray($reports , [
'common\models\Reports' => [
'date' => 'created_at',

View File

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