add attachments structure

This commit is contained in:
2023-02-24 12:53:26 +10:00
parent 4ee8fd2400
commit 6ebb915c68
6 changed files with 170 additions and 1 deletions

View File

@@ -0,0 +1,84 @@
<?php
namespace App\Http\Controllers\Api;
use App\Models\Attachment;
use Illuminate\Http\Request;
class AttachmentController extends ApiController
{
/**
* ApplicationController constructor.
*/
public function __construct()
{
$this->middleware('auth:sanctum')
->except(['store', 'destroyByEmail']);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param \App\Models\Attachment $attachment
* @return \Illuminate\Http\Response
*/
public function show(Attachment $attachment)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Attachment $attachment
* @return \Illuminate\Http\Response
*/
public function edit(Attachment $attachment)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Attachment $attachment
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Attachment $attachment)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Attachment $attachment
* @return \Illuminate\Http\Response
*/
public function destroy(Attachment $attachment)
{
//
}
}

28
app/Models/Attachment.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Attachment extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'media_id',
];
/**
* Get attachments attachable
*/
public function attachable()
{
return $this->morphTo();
}}

View File

@@ -29,4 +29,12 @@ class Event extends Model
'hero',
'content'
];
/**
* Get all of the post's attachments.
*/
public function attachments()
{
return $this->morphMany('App\Attachment', 'attachable');
}
}

View File

@@ -27,7 +27,7 @@ class Post extends Model
/**
* Get the file user
* Get the post user
*
* @return BelongsTo
*/
@@ -35,4 +35,12 @@ class Post extends Model
{
return $this->belongsTo(User::class);
}
/**
* Get all of the post's attachments.
*/
public function attachments()
{
return $this->morphMany('App\Attachment', 'attachable');
}
}

View File

@@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('attachments', function (Blueprint $table) {
$table->id();
$table->uuid('media_id');
$table->uuidMorphs('attachable');
$table->timestamps();
$table->foreign('media_id')->references('id')->on('media')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('media_attachments');
}
};

View File

@@ -36,8 +36,14 @@ Route::apiResource('media', MediaController::class);
Route::get('media/{medium}/download', [MediaController::class, 'download']);
Route::apiResource('posts', PostController::class);
Route::get('posts/{post}/attachments', [PostController::class, 'getAttachments']);
Route::post('posts/{post}/attachments', [PostController::class, 'storeAttachment']);
Route::delete('posts/{post}/attachments/{attachment}', [PostController::class, 'deleteAttachment']);
Route::apiResource('events', EventController::class);
Route::get('events/{event}/attachments', [PostController::class, 'getAttachments']);
Route::post('events/{event}/attachments', [PostController::class, 'storeAttachment']);
Route::delete('events/{event}/attachments/{attachment}', [PostController::class, 'deleteAttachment']);
Route::apiResource('subscriptions', SubscriptionController::class);
Route::delete('subscriptions', [SubscriptionController::class, 'destroyByEmail']);