diff --git a/app/Http/Controllers/MusicController.php b/app/Http/Controllers/MusicController.php index d0a6d58..1223669 100644 --- a/app/Http/Controllers/MusicController.php +++ b/app/Http/Controllers/MusicController.php @@ -16,6 +16,7 @@ public function store(Request $request) 'artist' => 'nullable|string|max:255', 'file' => 'required|mimes:mp3,wav,ogg|max:10240', // max 10MB 'type' => 'nullable|in:public,private', + 'image_id' => 'nullable|exists:images,id', ]); $path = $request->file('file')->store('music', 'public'); @@ -26,11 +27,12 @@ public function store(Request $request) 'artist' => $data['artist'] ?? null, 'file_path' => $path, 'type' => $data['type'] ?? 'private', + 'image_id' => $data['image_id'] ?? null, ]); return response()->json([ 'message' => 'Music uploaded successfully', - 'music' => $music, + 'music' => $music->load('image'), 'url' => asset('storage/' . $path), ]); } @@ -46,13 +48,14 @@ public function all() }) ->get(); - return response()->json($music->map(fn($m) => [ - 'id' => $m->id, - 'title' => $m->title, - 'artist' => $m->artist, - 'type' => $m->type, - 'url' => asset('storage/' . $m->file_path), - ])); + $music = Music::with('image') // eager load image + ->where('type', 'public') + ->orWhere(function($query) use ($userId) { + $query->where('type', 'private')->where('user_id', $userId); + }) + ->get(); + + return response()->json($music); } // ✅ Update music @@ -65,6 +68,7 @@ public function update(Request $request, $id) 'artist' => 'nullable|string|max:255', 'type' => 'nullable|in:public,private', 'file' => 'nullable|mimes:mp3,wav,ogg|max:10240', + 'image_id' => 'nullable|exists:images,id', ]); if ($request->hasFile('file')) { @@ -73,14 +77,13 @@ public function update(Request $request, $id) $music->file_path = $path; } - $music->title = $data['title'] ?? $music->title; - $music->artist = $data['artist'] ?? $music->artist; - $music->type = $data['type'] ?? $music->type; + $music->fill($data); $music->save(); + return response()->json([ 'message' => 'Music updated successfully', - 'music' => $music, + 'music' => $music->load('image'), 'url' => asset('storage/' . $music->file_path), ]); } diff --git a/app/Models/Music.php b/app/Models/Music.php index 6484659..f866dac 100644 --- a/app/Models/Music.php +++ b/app/Models/Music.php @@ -14,6 +14,7 @@ class Music extends Model 'artist', 'file_path', 'type', // public or private + 'image_id', ]; // Relation to user (optional) @@ -21,10 +22,18 @@ public function user() { return $this->belongsTo(User::class); } - + protected $appends = ['url', 'image_url']; // Accessor for full URL public function getUrlAttribute() { return asset('storage/' . $this->file_path); } + public function image() + { + return $this->belongsTo(Image::class, 'image_id'); + } + public function getImageUrlAttribute() + { + return $this->image ? asset('storage/' . $this->image->path) : null; + } } diff --git a/database/migrations/2025_09_27_104046_add_image_id_to_music_table.php b/database/migrations/2025_09_27_104046_add_image_id_to_music_table.php new file mode 100644 index 0000000..5da6805 --- /dev/null +++ b/database/migrations/2025_09_27_104046_add_image_id_to_music_table.php @@ -0,0 +1,35 @@ +unsignedBigInteger('image_id')->nullable()->after('file_path'); + + // add foreign key constraint with cascade on delete set null + $table->foreign('image_id') + ->references('id') + ->on('images') + ->onDelete('set null'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('music', function (Blueprint $table) { + $table->dropForeign(['image_id']); + $table->dropColumn('image_id'); + }); + } +};