fix add report

This commit is contained in:
iIronside 2022-12-16 20:18:25 +03:00
parent ac03d43a40
commit b6d958bdc0
2 changed files with 20 additions and 29 deletions

View File

@ -251,7 +251,7 @@
`https://guild.craft-group.xyz/api/reports/create` `https://guild.craft-group.xyz/api/reports/create`
<p> <p>
Для того, отправить приглашение профилю на собеседование, необходимо сделать Для создания отчёта, необходимо сделать
<b>POST</b> запрос на URL https://guild.craft-group.xyz/api/reports/create <b>POST</b> запрос на URL https://guild.craft-group.xyz/api/reports/create
</p> </p>
<p> <p>

View File

@ -2,19 +2,15 @@
namespace frontend\modules\api\controllers; namespace frontend\modules\api\controllers;
use common\behaviors\GsCors;
use common\classes\Debug;
use common\models\Reports; use common\models\Reports;
use common\models\ReportsTask; use common\models\ReportsTask;
use common\models\UserCard; use common\models\UserCard;
use frontend\modules\api\models\ReportSearchForm; use frontend\modules\api\models\ReportSearchForm;
use JsonException; use JsonException;
use Yii; use Yii;
use yii\db\Expression;
use yii\filters\auth\CompositeAuth; use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth; use yii\filters\auth\HttpBearerAuth;
use yii\filters\ContentNegotiator; use yii\filters\ContentNegotiator;
use yii\helpers\ArrayHelper;
use yii\web\BadRequestHttpException; use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\web\Response; use yii\web\Response;
@ -100,37 +96,32 @@ class ReportsController extends ApiController
} }
if(!isset($params['user_card_id'])){ if(!isset($params['user_card_id'])){
/** @var UserCard $userCard */
$userCard = UserCard::find()->where(['id_user' => Yii::$app->user->id])->one(); $userCard = UserCard::find()->where(['id_user' => Yii::$app->user->id])->one();
if($userCard){ if($userCard){
$userCardId = $userCard->id; $params['user_card_id'] = $userCard->id;
} else {
throw new BadRequestHttpException('User not found!');
} }
} else {
$userCardId = $params['user_card_id'];
} }
$reportsModel = new Reports();
$reports = []; $reportsModel->attributes = $params;
foreach ($params['tasks'] as $task) { if(!$reportsModel->validate() || !$reportsModel->save()){
$report = new Reports(); throw new BadRequestHttpException(json_encode($reportsModel->errors));
$report->load($task);
$report->difficulties = $report->difficulties ?? $params['difficulties'];
$report->tomorrow = $report->difficulties ?? $params['tomorrow'];
$report->today = $report->today ?? $params['today'];
$report->user_card_id = $report->user_card_id ?? $userCardId;
$report->created_at = date('Y-m-d');
if ($report->validate() && !$report->save()) {
throw new BadRequestHttpException(json_encode($report->errors));
} }
$reports[] = $report; $tasks = $params['tasks'];
foreach ($tasks as $task) {
$reportsTask = new ReportsTask();
$reportsTask->attributes = $task;
$reportsTask->report_id = $reportsModel->id;
$reportsTask->created_at = $reportsTask->created_at ?? strtotime($reportsModel->created_at);
$reportsTask->status = $reportsTask->status ?? 1;
if(!$reportsTask->validate() || !$reportsTask->save()){
throw new BadRequestHttpException(json_encode($reportsTask->errors));
}
} }
return array_merge($reports); return array_merge($reportsModel->toArray());
} }
public function actionDelete() public function actionDelete()