fix reports

This commit is contained in:
iIronside
2023-01-19 16:29:06 +03:00
parent b5a9d4be40
commit bb9959edf1
3 changed files with 40 additions and 163 deletions

View File

@ -7,66 +7,28 @@ 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();
}
/**
@ -88,9 +50,6 @@ class ReportsController extends ApiController
throw new BadRequestHttpException('Wrong date format');
}
// return $reportsModel->date;
// return $reportsModel->user_card_id;
if(!$reportsModel->validate()){
return $reportsModel->errors;
}
@ -145,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;
@ -161,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;
@ -197,7 +156,9 @@ class ReportsController extends ApiController
$reportsModel->attributes = $params;
$reportsModel->user_card_id = $userCard->id;
$reports = $reportsModel->findByDate();
// return $reportsModel;
$reports = $reportsModel->reportsByDate();
return ArrayHelper::toArray($reports , [
'common\models\Reports' => [
'date' => 'created_at',