asset edit functionality

master
Константин 2023-12-08 15:36:01 +03:00
parent 7804991c4e
commit 9911f60b85
5 changed files with 53 additions and 45 deletions

View File

@ -0,0 +1,31 @@
<?php
namespace App\Http\Controllers\Api\Assets;
use App\Http\Controllers\Controller;
use App\Models\Asset;
use App\Transformers\Assets\AssetTransformer;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class AssetsController extends Controller {
protected Asset $model;
public function __construct(Asset $model) {
$this->model = $model;
}
public function update(Request $request, $uuid): JsonResponse {
$model = $this->model->byUuid($uuid)->firstOrFail();
$model->update($request->only('name', 'description'));
return fractal($model, new AssetTransformer())->respond();
}
public function destroy(Request $request, $uuid): JsonResponse {
$model = $this->model->byUuid($uuid)->firstOrFail();
$model->delete();
return response()->json(null, 204);
}
}

View File

@ -8,64 +8,38 @@ use App\Support\RelationValuesTrait;
use App\Support\UuidScopeTrait; use App\Support\UuidScopeTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* Class Asset.
*/
class Asset extends Model { class Asset extends Model {
use UuidScopeTrait, HasFactory, RelationValuesTrait; use UuidScopeTrait, HasFactory, RelationValuesTrait;
/**
* @var array
*/
protected $guarded = ['id']; protected $guarded = ['id'];
public function user() public function user(): BelongsTo {
{
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
} }
public function links()
{ public function getIsImageAttribute(): bool {
$result = [ return $this->type === 'image';
}
public function links(): array {
return [
'open' => asset("/api/assets/{$this->uuid}"), 'open' => asset("/api/assets/{$this->uuid}"),
'download' => asset("/api/assets/{$this->uuid}/download"), 'download' => asset("/api/assets/{$this->uuid}/download"),
'direct' => asset("storage/{$this->path}") 'direct' => asset("storage/{$this->path}"),
]; 'full' => $this->isImage ? asset("/api/assets/{$this->uuid}/render") : null,
if ($this->type == 'image') { 'thumb' => $this->isImage ? asset("/api/assets/{$this->uuid}/render?width=300") : null
$result['full'] = asset("/api/assets/{$this->uuid}/render");
$result['thumb'] = asset("/api/assets/{$this->uuid}/render?width=300");
}
return $result;
}
public function coordinates()
{
return [
'latitude' => $this->latitude,
'longitude' => $this->longitude,
'accuracy' => $this->accuracy
]; ];
} }
public function output() public function coordinates(): array {
{ return ['latitude' => $this->latitude, 'longitude' => $this->longitude, 'accuracy' => $this->accuracy];
return [
'id' => $this->uuid,
'type' => $this->type,
'mime' => $this->mime,
'filename' => $this->filename,
'extension' => $this->extension,
'links' => $this->links(),
'user' => $this->user->output(),
'coordinates' => $this->coordinates(),
'created_at' => $this->created_at->toIso8601String(),
'created_by' => $this->user->name,
];
} }
public function saveValue($fieldName, $object) public function saveValue($fieldName, $object) {
{
$field = Field::byUuidOrName($fieldName)->first(); $field = Field::byUuidOrName($fieldName)->first();
$relationValue = RelationValue::create(); $relationValue = RelationValue::create();
$relationValue->set($this); $relationValue->set($this);

View File

@ -23,6 +23,7 @@ class AssetTransformer extends TransformerAbstract {
'path' => $model->path, 'path' => $model->path,
'mime' => $model->mime, 'mime' => $model->mime,
'name' => $model->name, 'name' => $model->name,
'description' => $model->description,
'filename' => $model->filename, 'filename' => $model->filename,
'extension' => $model->extension, 'extension' => $model->extension,
'links' => $model->links(), 'links' => $model->links(),

View File

@ -11,13 +11,13 @@ class CreateAssetsTable extends Migration
* *
* @return void * @return void
*/ */
public function up() public function up() {
{
Schema::create('assets', function (Blueprint $table) { Schema::create('assets', function (Blueprint $table) {
$table->id(); $table->id();
$table->char('uuid', 36)->index()->unique(); $table->char('uuid', 36)->index()->unique();
$table->bigInteger('user_id')->unsigned()->index()->nullable(); $table->bigInteger('user_id')->unsigned()->index()->nullable();
$table->string('name')->index()->nullable(); $table->string('name')->index()->nullable();
$table->text('description')->nullable();
$table->string('type', 45)->index()->nullable(); $table->string('type', 45)->index()->nullable();
$table->string('path')->nullable(); $table->string('path')->nullable();
$table->string('mime')->nullable(); $table->string('mime')->nullable();
@ -28,7 +28,7 @@ class CreateAssetsTable extends Migration
$table->float('accuracy')->nullable(); $table->float('accuracy')->nullable();
$table->timestamps(); $table->timestamps();
$table->softDeletes(); $table->softDeletes();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); //$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
}); });
} }

View File

@ -15,6 +15,8 @@ Route::group(['prefix' => 'assets'], function() {
Route::get('/{uuid}/download', 'Api\Assets\RenderFileController@download'); Route::get('/{uuid}/download', 'Api\Assets\RenderFileController@download');
Route::group(['middleware' => ['auth:api']], function() { Route::group(['middleware' => ['auth:api']], function() {
Route::post('/', 'Api\Assets\UploadFileController@store'); Route::post('/', 'Api\Assets\UploadFileController@store');
Route::put('/{uuid}', 'Api\Assets\AssetsController@update');
Route::delete('/uuid', 'Api\Assets\AssetsController@destroy');
}); });
}); });