Skip to content

A self-hosted media server and file management system

tag backend ? no multi to multi yet

aminnausin3f3963dc04ca
diff --git a/app/Http/Controllers/Api/V1/TagController.php b/app/Http/Controllers/Api/V1/TagController.php
new file mode 100644
index 00000000..d875cae7
--- /dev/null
+++ b/app/Http/Controllers/Api/V1/TagController.php
@@ -0,0 +1,61 @@
+<?php
+
+namespace App\Http\Controllers\Api\V1;
+
+use App\Models\Series;
+use App\Http\Controllers\Controller;
+use App\Http\Requests\TagStoreRequest;
+use App\Http\Resources\TagResource;
+use App\Models\Tag;
+use App\Traits\HttpResponses;
+use Illuminate\Support\Facades\Auth;
+
+class TagController extends Controller
+{
+ use HttpResponses;
+ /* User can:
+ *
+ * - List all series (to search ? admin page ?)
+ * - Store (Create series if row for folder does not already exist)
+ * - Update (User updates details)
+ *
+ * User cannot:
+ * - Show (Series data is added to each folder. Theres no reason to get one row on its own)
+ * - Delete (User should not be able to delete series row | maybe admin can but thats later)
+ */
+
+ /**
+ * Display a listing of the resource.
+ */
+ public function index()
+ {
+ try {
+ return $this->success(
+ TagResource::collection(
+ Tag::all()
+ )
+ );
+ } catch (\Throwable $th) {
+ return $this->error(null, 'Unable to get list of tags. Error: ' . $th->getMessage(), 500);
+ }
+ }
+
+ /**
+ * Store a newly created resource in storage.
+ */
+ public function store(TagStoreRequest $request)
+ {
+ try {
+ $validated = $request->validated();
+
+ $existing = Tag::where('name', $request->name)->first();
+ if ($existing) return $this->error($existing, 'Tag already exists!', 500);
+
+ $validated['creator_id'] = Auth::user()->id;
+ $tag = Tag::create($validated);
+ return $this->success(new TagResource($tag));
+ } catch (\Throwable $th) {
+ return $this->error(null, 'Unable to create tag. Error: ' . $th->getMessage() . $request, 500);
+ }
+ }
+}
diff --git a/app/Http/Requests/TagStoreRequest.php b/app/Http/Requests/TagStoreRequest.php
new file mode 100644
index 00000000..3c794b36
--- /dev/null
+++ b/app/Http/Requests/TagStoreRequest.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+use Illuminate\Support\Facades\Auth;
+
+class TagStoreRequest extends FormRequest
+{
+ /**
+ * Determine if the user is authorized to make this request.
+ */
+ public function authorize(): bool
+ {
+ return Auth::check();
+ }
+
+ /**
+ * Get the validation rules that apply to the request.
+ *
+ * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
+ */
+ public function rules(): array
+ {
+ return [
+ 'name' => ['required', 'min:1']
+ ];
+ }
+}
diff --git a/app/Http/Resources/TagResource.php b/app/Http/Resources/TagResource.php
new file mode 100644
index 00000000..e8d71e0c
--- /dev/null
+++ b/app/Http/Resources/TagResource.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class TagResource extends JsonResource
+{
+ /**
+ * Transform the resource into an array.
+ *
+ * @return array<string, mixed>
+ */
+ public function toArray(Request $request): array
+ {
+ return [
+ 'id' => $this->id,
+ 'name' => $this->name,
+ 'relationships' => [
+ 'creator_id' => $this->creator ? $this->creator->id : null,
+ ]
+ ];
+ }
+}
diff --git a/app/Http/Resources/VideoResource.php b/app/Http/Resources/VideoResource.php
index ee81032f..6c4538ff 100755
--- a/app/Http/Resources/VideoResource.php
+++ b/app/Http/Resources/VideoResource.php
@@ -29,6 +29,7 @@ class VideoResource extends JsonResource
'season' => $metadata ? $this->metadata->season : $this->season,
'view_count' => $metadata ? $this->metadata->view_count : $this->view_count,
'tags' => $this->metadata->tags ?? '',
+ 'video_tags' => $this->metadata->tags() ?? [],
// ],
// 'relationships' => [
'folder_id' => (string)$this->folder->id,
diff --git a/app/Http/Resources/VideoTagResource.php b/app/Http/Resources/VideoTagResource.php
new file mode 100644
index 00000000..48ea208d
--- /dev/null
+++ b/app/Http/Resources/VideoTagResource.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class VideoTagResource extends JsonResource
+{
+ /**
+ * Transform the resource into an array.
+ *
+ * @return array<string, mixed>
+ */
+ public function toArray(Request $request): array
+ {
+ return [
+ 'id' => $this->id,
+ 'name' => $this->tag->name,
+ 'relationships' => [
+ 'metadata_id' => $this->metadata ? $this->metadata->id : null,
+ 'tag_id' => $this->tag ? $this->tag->id : null,
+ ]
+ ];
+ }
+}
diff --git a/app/Models/Metadata.php b/app/Models/Metadata.php
index 036a4609..389e0038 100644
--- a/app/Models/Metadata.php
+++ b/app/Models/Metadata.php
@@ -24,16 +24,24 @@ class Metadata extends Model
'date_released',
'tags',
];
-
- public function video(): BelongsTo {
+
+ public function video(): BelongsTo
+ {
return $this->belongsTo(Video::class);
}
- public function editor(): BelongsTo {
+ public function editor(): BelongsTo
+ {
return $this->belongsTo(User::class);
}
- public function playback(): HasMany{
+ public function playback(): HasMany
+ {
return $this->hasMany(Playback::class);
}
+
+ public function tags(): HasMany
+ {
+ return $this->hasMany(VideoTag::class);
+ }
}
diff --git a/app/Models/Tag.php b/app/Models/Tag.php
new file mode 100644
index 00000000..1d6378d3
--- /dev/null
+++ b/app/Models/Tag.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class Tag extends Model
+{
+ use HasFactory;
+
+ protected $fillable = [
+ 'name',
+ 'creator_id'
+ ];
+
+ public function creator()
+ {
+ return $this->belongsTo(User::class);
+ }
+}
diff --git a/app/Models/VideoTag.php b/app/Models/VideoTag.php
new file mode 100644
index 00000000..1c2b836f
--- /dev/null
+++ b/app/Models/VideoTag.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class VideoTag extends Model
+{
+ use HasFactory;
+
+ protected $fillable = [
+ 'metadata_id',
+ 'tag_id',
+ ];
+
+ public function metadata()
+ {
+ return $this->belongsTo(Metadata::class);
+ }
+
+ public function tag()
+ {
+ return $this->belongsTo(Tag::class);
+ }
+}
diff --git a/database/migrations/2024_11_09_221140_create_tags_table.php b/database/migrations/2024_11_09_221140_create_tags_table.php
new file mode 100644
index 00000000..f0024f10
--- /dev/null
+++ b/database/migrations/2024_11_09_221140_create_tags_table.php
@@ -0,0 +1,33 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+ /**
+ * Run the migrations.
+ */
+ public function up(): void
+ {
+ Schema::create('tags', function (Blueprint $table) {
+ $table->id();
+ $table->unsignedBigInteger('creator_id')->nullable();
+ $table->string('name')->unique();
+ $table->foreign('creator_id')
+ ->references('id')
+ ->on('users')
+ ->nullOnDelete();
+ $table->timestamps();
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void
+ {
+ Schema::dropIfExists('tags');
+ }
+};
diff --git a/database/migrations/2024_11_09_221201_create_video_tags_table.php b/database/migrations/2024_11_09_221201_create_video_tags_table.php
new file mode 100644
index 00000000..049c68c5
--- /dev/null
+++ b/database/migrations/2024_11_09_221201_create_video_tags_table.php
@@ -0,0 +1,37 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+ /**
+ * Run the migrations.
+ */
+ public function up(): void
+ {
+ Schema::create('video_tags', function (Blueprint $table) {
+ $table->id();
+ $table->unsignedBigInteger('tag_id');
+ $table->unsignedBigInteger('metadata_id');
+ $table->foreign('tag_id')
+ ->references('id')
+ ->on('tags')
+ ->cascadeOnDelete();
+ $table->foreign('metadata_id')
+ ->references('id')
+ ->on('metadata')
+ ->cascadeOnDelete();
+ $table->timestamps();
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void
+ {
+ Schema::dropIfExists('video_tags');
+ }
+};
diff --git a/routes/api.php b/routes/api.php
index 3974c1a8..12a5daf3 100755
--- a/routes/api.php
+++ b/routes/api.php
@@ -9,6 +9,7 @@ use App\Http\Controllers\Api\V1\SeriesController;
use App\Http\Controllers\Api\V1\VideoController;
use App\Http\Controllers\Api\V1\ProfileController;
use App\Http\Controllers\Api\V1\RecordController;
+use App\Http\Controllers\Api\V1\TagController;
use App\Http\Controllers\DirectoryController;
use Illuminate\Http\Request;
@@ -27,6 +28,7 @@ Route::group(['middleware' => ['auth:sanctum']], function () {
Route::resource('/profile', ProfileController::class)->only(['show', 'store', 'update']);
Route::resource('/series', SeriesController::class)->only(['index', 'store', 'update']);
Route::resource('/metadata', MetadataController::class)->only(['store', 'update']);
+ Route::resource('/tags', TagController::class)->only(['index', 'store']);
});
// public