fix detach fail, marks

This commit is contained in:
Kavalar 2023-10-06 00:37:46 +03:00
parent 5cdd516696
commit 21b0135c68
11 changed files with 643 additions and 2 deletions

22
common/models/Entity.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace common\models;
class Entity
{
const ENTITY_TYPE_PROJECT = 1;
const ENTITY_TYPE_TASK = 2;
/**
* @return string[]
*/
public static function getEntityTypeList(): array
{
return [
self::ENTITY_TYPE_PROJECT => "Проект",
self::ENTITY_TYPE_TASK => "Задача",
];
}
}

View File

@ -9,6 +9,9 @@ use Yii;
*
* @property int $id
* @property string $title
* @property string $slug
* @property string $color
* @property int $status
*
* @property ProjectMark[] $projectMarks
*/
@ -28,7 +31,8 @@ class Mark extends \yii\db\ActiveRecord
public function rules()
{
return [
[['title'], 'string', 'max' => 255],
[['title', 'slug', 'color'], 'string', 'max' => 255],
[['status'], 'integer'],
];
}
@ -40,6 +44,9 @@ class Mark extends \yii\db\ActiveRecord
return [
'id' => 'ID',
'title' => 'Название',
'slug' => 'Ключ',
'color' => 'Цвет',
'status' => 'Статус',
];
}

View File

@ -0,0 +1,70 @@
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "mark_entity".
*
* @property int $id
* @property int $mark_id
* @property int $entity_type
* @property int $entity_id
*/
class MarkEntity extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'mark_entity';
}
/**
* @return string[]
*/
public function fields(): array
{
return [
'id',
'mark_id',
'mark',
'entity_type',
'entity_id',
];
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['mark_id', 'entity_type', 'entity_id'], 'required'],
[['mark_id', 'entity_type', 'entity_id'], 'integer'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'mark_id' => 'Mark ID',
'entity_type' => 'Entity Type',
'entity_id' => 'Entity ID',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getMark(): \yii\db\ActiveQuery
{
return $this->hasOne(Mark::class, ['id' => 'mark_id']);
}
}

View File

@ -102,6 +102,7 @@ class ProjectTask extends ActiveRecord
return [
'id',
'project_id',
'project.name',
'title',
'created_at',
'updated_at',

View File

@ -29,7 +29,7 @@ class TaskService
public static function getTaskListByProject($project_id): array
{
return ProjectTask::find()->where(['project_id' => $project_id])->asArray()->all();
return ProjectTask::find()->where(['project_id' => $project_id])->all();
}
public static function getTaskListByUser($user_id): array

View File

@ -0,0 +1,29 @@
<?php
use yii\db\Migration;
/**
* Handles adding columns to table `{{%mark}}`.
*/
class m231004_211051_add_columns_to_mark_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('mark', 'slug', $this->string(255));
$this->addColumn('mark', 'color', $this->string(255));
$this->addColumn('mark', 'status', $this->integer(1)->defaultValue(1));
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('mark', 'slug');
$this->dropColumn('mark', 'color');
$this->dropColumn('mark', 'status');
}
}

View File

@ -0,0 +1,30 @@
<?php
use yii\db\Migration;
/**
* Handles the creation of table `{{%mark_entity}}`.
*/
class m231004_212828_create_mark_entity_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->createTable('{{%mark_entity}}', [
'id' => $this->primaryKey(),
'mark_id' => $this->integer(11)->notNull(),
'entity_type' => $this->integer(1)->notNull(),
'entity_id' => $this->integer(11)->notNull(),
]);
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropTable('{{%mark_entity}}');
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace frontend\modules\api\controllers;
use common\models\Entity;
use frontend\modules\api\models\Comment;
class EntityController extends ApiController
{
/**
* @return array[]
*/
public function verbs(): array
{
return [
'get-list' => ['get'],
];
}
/**
*
* @OA\Get(path="/entity/get-list",
* summary="Список типов сущностей",
* description="Получить список всех возможных типов сущностей",
* tags={"Entity"},
* security={
* {"bearerAuth": {}}
* },
* @OA\Response(
* response=200,
* description="Возвращает массив",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="array",
* @OA\Items(
* @OA\Property(
* property="id",
* type="integer",
* example="1",
* ),
* @OA\Property(
* property="name",
* type="string",
* example="Проект",
* ),
* )
* ),
* ),
*
* ),
* )
*
* @return array
*/
public function actionGetList(): array
{
$arr = [];
foreach (Entity::getEntityTypeList() as $key => $value) {
$arr[] = ["id" => $key, "name" => $value];
}
return $arr;
}
}

View File

@ -0,0 +1,316 @@
<?php
namespace frontend\modules\api\controllers;
use common\models\File;
use frontend\modules\api\models\Comment;
use frontend\modules\api\models\FileEntity;
use frontend\modules\api\models\Mark;
use frontend\modules\api\models\MarkEntity;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException;
class MarkController extends ApiController
{
public function verbs(): array
{
return [
'create' => ['post'],
'attach' => ['post'],
'detach' => ['delete'],
'update' => ['put', 'patch'],
];
}
/**
*
* @OA\Post(path="/mark/create",
* summary="Добавить метку",
* description="Метод для создания метки",
* security={
* {"bearerAuth": {}}
* },
* tags={"Mark"},
*
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* required={"color", "title", "slug"},
* @OA\Property(
* property="title",
* type="string",
* description="Описание метки",
* ),
* @OA\Property(
* property="slug",
* type="string",
* description="Ключ",
* ),
* @OA\Property(
* property="color",
* type="string",
* description="Цвет",
* ),
* @OA\Property(
* property="status",
* type="integer",
* description="Статус метки",
* ),
* ),
* ),
* ),
* @OA\Response(
* response=200,
* description="Возвращает объект метки",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(ref="#/components/schemas/Mark"),
* ),
* ),
* )
*
* @return array|Mark
* @throws BadRequestHttpException
*/
public function actionCreate()
{
$model = new Mark();
$request = \Yii::$app->request->post();
$put = array_diff($request, [null, '']);
$model->load($put, '');
if (!$model->save()) {
return $model->errors;
}
return $model;
}
/**
*
* @OA\Put(path="/mark/update",
* summary="Редактировать метку",
* description="Метод для редактирования метки",
* security={
* {"bearerAuth": {}}
* },
* tags={"Mark"},
*
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="application/x-www-form-urlencoded",
* @OA\Schema(
* required={"mark_id"},
* @OA\Property(
* property="mark_id",
* type="integer",
* description="Идентификатор метки",
* ),
* @OA\Property(
* property="title",
* type="string",
* description="Описание метки",
* ),
* @OA\Property(
* property="slug",
* type="string",
* description="Ключ",
* ),
* @OA\Property(
* property="color",
* type="string",
* description="Цвет",
* ),
* @OA\Property(
* property="status",
* type="integer",
* description="Статус метки",
* ),
* ),
* ),
* ),
* @OA\Response(
* response=200,
* description="Возвращает объект Метки",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(ref="#/components/schemas/Mark"),
* ),
* ),
* )
*
* @return Mark
* @throws BadRequestHttpException
* @throws \yii\base\InvalidConfigException
*/
public function actionUpdate(): Mark
{
$mark_id = \Yii::$app->request->getBodyParam('mark_id');
$model = Mark::findOne($mark_id);
if (!$model) {
throw new BadRequestHttpException(json_encode(['Mark not found']));
}
$put = array_diff(\Yii::$app->request->getBodyParams(), [null, '']);
$model->load($put, '');
if(!$model->validate()){
throw new BadRequestHttpException($model->errors);
}
$model->save();
return $model;
}
/**
*
* @OA\Post(path="/mark/attach",
* summary="Прикрепить метку",
* description="Метод для прикрепления меток",
* security={
* {"bearerAuth": {}}
* },
* tags={"Mark"},
*
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* required={"mark_id", "entity_type", "entity_id"},
* @OA\Property(
* property="mark_id",
* type="intager",
* example=232,
* description="Идентификатор метки",
* ),
* @OA\Property(
* property="entity_type",
* type="intager",
* example=2,
* description="Идентификатор типа сущности",
* ),
* @OA\Property(
* property="entity_id",
* type="intager",
* example=234,
* description="Идентификатор сущности",
* ),
* ),
* ),
* ),
* @OA\Response(
* response=200,
* description="Возвращает объект прикрепления",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(ref="#/components/schemas/MarkEntity"),
* ),
* ),
* )
*
* @return MarkEntity
* @throws NotFoundHttpException
* @throws ServerErrorHttpException
*/
public function actionAttach(): MarkEntity
{
$request = \Yii::$app->request->post();
$mark = Mark::findOne($request['mark_id']);
if (!$mark) {
throw new NotFoundHttpException('Mark not found');
}
$markEntity = new MarkEntity();
$markEntity->load($request, '');
if (!$markEntity->validate()) {
throw new ServerErrorHttpException(json_encode($markEntity->errors));
}
$markEntity->save();
return $markEntity;
}
/**
*
* @OA\Delete (path="/mark/detach",
* summary="Открепить метку",
* description="Метод для открепления меток",
* security={
* {"bearerAuth": {}}
* },
* tags={"Mark"},
*
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="application/x-www-form-urlencoded",
* @OA\Schema(
* required={"mark_id", "entity_type", "entity_id"},
* @OA\Property(
* property="mark_id",
* type="intager",
* example=232,
* description="Идентификатор метки",
* ),
* @OA\Property(
* property="entity_type",
* type="intager",
* example=2,
* description="Идентификатор типа сущности",
* ),
* @OA\Property(
* property="entity_id",
* type="intager",
* example=234,
* description="Идентификатор сущности",
* ),
* ),
* ),
* ),
* @OA\Response(
* response=200,
* description="Возвращает объект прикрепления",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(ref="#/components/schemas/MarkEntity"),
* ),
* ),
* )
*
* @return int
* @throws NotFoundHttpException
* @throws ServerErrorHttpException
*/
public function actionDetach(): int
{
$request = \Yii::$app->request->getBodyParams();
$mark = Mark::findOne($request['mark_id']);
if (!$mark) {
throw new NotFoundHttpException('Mark not found');
}
$markEntity = MarkEntity::findOne(['mark_id' => $request['mark_id'], 'entity_type' => $request['entity_type'], 'entity_id' => $request['entity_id']]);
if (!$markEntity) {
throw new NotFoundHttpException('Mark attach not found');
}
//$fileEntity->load($request, '');
$del = $markEntity->delete();
if (!$del) {
throw new ServerErrorHttpException(json_encode($markEntity->errors));
}
return $del;
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace frontend\modules\api\models;
/**
*
* @OA\Schema(
* schema="Mark",
* @OA\Property(
* property="id",
* type="int",
* example=12,
* description="Идентификатор метки"
* ),
* @OA\Property(
* property="title",
* type="string",
* example="Срочная задача",
* description="Описание метки"
* ),
* @OA\Property(
* property="slug",
* type="string",
* example="urgent",
* description="Ключ метки"
* ),
* @OA\Property(
* property="color",
* type="string",
* example="RED",
* description="Цвет метки"
* ),
* @OA\Property(
* property="status",
* type="integer",
* example="1",
* description="Статус"
* ),
*)
*
* @OA\Schema(
* schema="MarkExample",
* type="array",
* @OA\Items(
* ref="#/components/schemas/Mark",
* ),
*)
*
*/
class Mark extends \common\models\Mark
{
}

View File

@ -0,0 +1,44 @@
<?php
namespace frontend\modules\api\models;
/**
*
* @OA\Schema(
* schema="MarkEntity",
* @OA\Property(
* property="id",
* type="int",
* example=1,
* description="Идентификатор метки"
* ),
* @OA\Property(
* property="mark_id",
* type="integer",
* example=1,
* description="Идентификатор метки"
* ),
* @OA\Property(
* property="mark",
* ref="#/components/schemas/MarkExample",
* description="Файл"
* ),
* @OA\Property(
* property="entity_type",
* type="integer",
* example=2,
* description="Идентификатор типа сущности"
* ),
* @OA\Property(
* property="entity_id",
* type="integer",
* example=24,
* description="Идентификатор сущности"
* ),
*)
*
*/
class MarkEntity extends \common\models\MarkEntity
{
}