Merge pull request #74 from q6q9/api-reports

Api reports
This commit is contained in:
kavalar 2021-11-29 18:21:04 +03:00 committed by GitHub
commit 402e1863e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 5 deletions

View File

@ -19,6 +19,15 @@ use Yii;
*/ */
class ReportsTask extends \yii\db\ActiveRecord class ReportsTask extends \yii\db\ActiveRecord
{ {
const SCENARIO_WITHOUT_REPORT_ID = 'withoutReportID';
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios[self::SCENARIO_WITHOUT_REPORT_ID] = self::attributes();
return $scenarios;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -33,7 +42,7 @@ class ReportsTask extends \yii\db\ActiveRecord
public function rules() public function rules()
{ {
return [ return [
[['report_id'], 'required'], [['report_id'], 'required', 'on' => self::SCENARIO_DEFAULT],
[['report_id', 'created_at', 'status', 'minutes_spent'], 'integer'], [['report_id', 'created_at', 'status', 'minutes_spent'], 'integer'],
[['hours_spent'], 'number'], [['hours_spent'], 'number'],
['minutes_spent', 'compare', 'compareValue' => 60, 'operator' => '<'], ['minutes_spent', 'compare', 'compareValue' => 60, 'operator' => '<'],

View File

@ -84,6 +84,7 @@ return [
'rules' => [ 'rules' => [
'site/index' => 'card/user-card/index', 'site/index' => 'card/user-card/index',
'api/profile/<id:\d+>' => 'api/profile/index', 'api/profile/<id:\d+>' => 'api/profile/index',
'api/reports/<id:\d+>' => 'api/reports/view',
'' => 'card/user-card/index', '' => 'card/user-card/index',
['class' => 'yii\rest\UrlRule', 'controller' => 'skills'], ['class' => 'yii\rest\UrlRule', 'controller' => 'skills'],
], ],

View File

@ -5,6 +5,7 @@ namespace frontend\modules\api\controllers;
use common\behaviors\GsCors; use common\behaviors\GsCors;
use common\classes\Debug; use common\classes\Debug;
use common\models\Reports; use common\models\Reports;
use common\models\ReportsTask;
use frontend\modules\api\models\ReportSearchForm; use frontend\modules\api\models\ReportSearchForm;
use JsonException; use JsonException;
use Yii; use Yii;
@ -67,20 +68,43 @@ class ReportsController extends Controller
return $reportsModel->byParams(); return $reportsModel->byParams();
} }
public function actionView($id): array{
$report = Reports::findOne($id);
return array_merge($report->toArray(), ['tasks' => $report->_task]);
}
public function actionCreate() public function actionCreate()
{ {
$reportsModel = new Reports();
$params = Yii::$app->request->post(); $params = Yii::$app->request->post();
if (!isset($params['tasks'])){
throw new BadRequestHttpException('Нет параметра tasks');
}
$reportsModel = new Reports();
$reportsModel->attributes = $params; $reportsModel->attributes = $params;
if(!$reportsModel->validate()){ if(!$reportsModel->validate()){
throw new BadRequestHttpException(json_encode($reportsModel->errors)); throw new BadRequestHttpException(json_encode($reportsModel->errors));
} }
$reportsModel->save(); $tasks = [];
foreach (json_decode($params['tasks'], true) as $jsonTask){
$task = new ReportsTask();
$task->scenario = ReportsTask::SCENARIO_WITHOUT_REPORT_ID;
$task->attributes = $jsonTask;
if (!$task->validate()) {
throw new BadRequestHttpException(json_encode($task->errors));
}
$tasks []= $task->attributes;
}
$attributes = $task->attributes();
return $reportsModel->toArray(); $reportsModel->save();
$tasks = array_map(function ($task)use($reportsModel){$task['report_id'] = $reportsModel->id; return $task;}, $tasks);
Yii::$app->db->createCommand()->batchInsert(ReportsTask::tableName(), $attributes, $tasks)->execute();
return array_merge($reportsModel->toArray(), ['tasks' => $tasks]);
} }
public function actionDelete() public function actionDelete()