files, dead line

This commit is contained in:
2023-06-30 18:13:40 +03:00
parent f861f3eea9
commit 567ab20361
7 changed files with 232 additions and 4 deletions

View File

@ -5,6 +5,7 @@ namespace frontend\modules\api\controllers;
use common\classes\Debug;
use common\models\File;
use frontend\modules\api\models\FileEntity;
use frontend\modules\api\models\Timer;
use yii\helpers\FileHelper;
use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException;
@ -17,6 +18,8 @@ class FileController extends ApiController
return [
'upload' => ['post'],
'attach' => ['post'],
'detach' => ['delete'],
'get-by-entity' => ['get'],
];
}
@ -151,7 +154,7 @@ class FileController extends ApiController
* @throws NotFoundHttpException
* @throws ServerErrorHttpException
*/
public function actionAttach()
public function actionAttach(): FileEntity
{
$request = \Yii::$app->request->post();
$file = File::findOne($request['file_id']);
@ -162,7 +165,7 @@ class FileController extends ApiController
$fileEntity = new FileEntity();
$fileEntity->load($request, '');
if(!$fileEntity->validate()){
if (!$fileEntity->validate()) {
throw new ServerErrorHttpException(json_encode($fileEntity->errors));
}
@ -171,4 +174,137 @@ class FileController extends ApiController
return $fileEntity;
}
/**
*
* @OA\Delete (path="/file/detach",
* summary="Открепить файл",
* description="Метод для открепления файлов",
* security={
* {"bearerAuth": {}}
* },
* tags={"File"},
*
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* required={"file_id", "entity_type", "entity_id"},
* @OA\Property(
* property="file_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\Property(
* property="status",
* type="intager",
* example=1,
* description="Статус",
* ),
* ),
* ),
* ),
* @OA\Response(
* response=200,
* description="Возвращает объект прикрепления",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(ref="#/components/schemas/FileEntity"),
* ),
* ),
* )
*
* @return FileEntity
* @throws NotFoundHttpException
* @throws ServerErrorHttpException
*/
public function actionDetach(): FileEntity
{
$request = \Yii::$app->request->post();
$file = File::findOne($request['file_id']);
if (!$file) {
throw new NotFoundHttpException('File bot found');
}
$fileEntity = new FileEntity();
$fileEntity->load($request, '');
$fileEntity->status = FileEntity::STATUS_DISABLE;
if (!$fileEntity->validate()) {
throw new ServerErrorHttpException(json_encode($fileEntity->errors));
}
$fileEntity->save();
return $fileEntity;
}
/**
*
* @OA\Get(path="/file/get-by-entity",
* summary="Получить файл по идентификатору сущности",
* description="Метод для получения файла по идентификатору сущности.",
* security={
* {"bearerAuth": {}}
* },
* tags={"File"},
* @OA\Parameter(
* name="entity_id",
* in="query",
* required=true,
* @OA\Schema(
* type="integer",
* default=null
* )
* ),
*
* @OA\Parameter(
* name="entity_type",
* in="query",
* required=true,
* @OA\Schema(
* type="integer",
* default=null
* )
* ),
*
* @OA\Response(
* response=200,
* description="Возвращает массив объектов Файлов",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(ref="#/components/schemas/FileEntity"),
* ),
* ),
* )
*
* @param int $entity_type
* @param int $entity_id
* @return array
* @throws NotFoundHttpException
*/
public function actionGetByEntity(int $entity_type, int $entity_id): array
{
$model = FileEntity::find()->where(['entity_type' => $entity_type, 'entity_id' => $entity_id, 'status' => FileEntity::STATUS_ACTIVE])->all();
if (!$model) {
throw new NotFoundHttpException('The file not found');
}
return $model;
}
}