From 6ebb915c6898fef7edae87eb9c8a79416adc0717 Mon Sep 17 00:00:00 2001 From: James Collins Date: Fri, 24 Feb 2023 12:53:26 +1000 Subject: [PATCH] add attachments structure --- .../Controllers/Api/AttachmentController.php | 84 +++++++++++++++++++ app/Models/Attachment.php | 28 +++++++ app/Models/Event.php | 8 ++ app/Models/Post.php | 10 ++- ..._02_24_023054_create_attachments_table.php | 35 ++++++++ routes/api.php | 6 ++ 6 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/Api/AttachmentController.php create mode 100644 app/Models/Attachment.php create mode 100644 database/migrations/2023_02_24_023054_create_attachments_table.php diff --git a/app/Http/Controllers/Api/AttachmentController.php b/app/Http/Controllers/Api/AttachmentController.php new file mode 100644 index 0000000..36a5acc --- /dev/null +++ b/app/Http/Controllers/Api/AttachmentController.php @@ -0,0 +1,84 @@ +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) + { + // + } +} diff --git a/app/Models/Attachment.php b/app/Models/Attachment.php new file mode 100644 index 0000000..b1bc6f2 --- /dev/null +++ b/app/Models/Attachment.php @@ -0,0 +1,28 @@ + + */ + protected $fillable = [ + 'media_id', + ]; + + + /** + * Get attachments attachable + */ + public function attachable() + { + return $this->morphTo(); + }} diff --git a/app/Models/Event.php b/app/Models/Event.php index 1e87ebe..eac9a9b 100644 --- a/app/Models/Event.php +++ b/app/Models/Event.php @@ -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'); + } } diff --git a/app/Models/Post.php b/app/Models/Post.php index 9584def..2fb5b69 100644 --- a/app/Models/Post.php +++ b/app/Models/Post.php @@ -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'); + } } diff --git a/database/migrations/2023_02_24_023054_create_attachments_table.php b/database/migrations/2023_02_24_023054_create_attachments_table.php new file mode 100644 index 0000000..2b86154 --- /dev/null +++ b/database/migrations/2023_02_24_023054_create_attachments_table.php @@ -0,0 +1,35 @@ +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'); + } +}; diff --git a/routes/api.php b/routes/api.php index 3a71170..528c656 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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']);