diff --git a/app/Http/Controllers/MusicController.php b/app/Http/Controllers/MusicController.php new file mode 100644 index 0000000..d0a6d58 --- /dev/null +++ b/app/Http/Controllers/MusicController.php @@ -0,0 +1,97 @@ +validate([ + 'title' => 'required|string|max:255', + 'artist' => 'nullable|string|max:255', + 'file' => 'required|mimes:mp3,wav,ogg|max:10240', // max 10MB + 'type' => 'nullable|in:public,private', + ]); + + $path = $request->file('file')->store('music', 'public'); + + $music = Music::create([ + 'user_id' => auth()->id(), + 'title' => $data['title'], + 'artist' => $data['artist'] ?? null, + 'file_path' => $path, + 'type' => $data['type'] ?? 'private', + ]); + + return response()->json([ + 'message' => 'Music uploaded successfully', + 'music' => $music, + 'url' => asset('storage/' . $path), + ]); + } + + // ✅ Get all public + user private music + public function all() + { + $userId = auth()->id(); + + $music = Music::where('type', 'public') + ->orWhere(function($query) use ($userId) { + $query->where('type', 'private')->where('user_id', $userId); + }) + ->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), + ])); + } + + // ✅ Update music + public function update(Request $request, $id) + { + $music = Music::where('id', $id)->where('user_id', auth()->id())->firstOrFail(); + + $data = $request->validate([ + 'title' => 'nullable|string|max:255', + 'artist' => 'nullable|string|max:255', + 'type' => 'nullable|in:public,private', + 'file' => 'nullable|mimes:mp3,wav,ogg|max:10240', + ]); + + if ($request->hasFile('file')) { + Storage::disk('public')->delete($music->file_path); + $path = $request->file('file')->store('music', 'public'); + $music->file_path = $path; + } + + $music->title = $data['title'] ?? $music->title; + $music->artist = $data['artist'] ?? $music->artist; + $music->type = $data['type'] ?? $music->type; + $music->save(); + + return response()->json([ + 'message' => 'Music updated successfully', + 'music' => $music, + 'url' => asset('storage/' . $music->file_path), + ]); + } + + // ✅ Delete music + public function destroy($id) + { + $music = Music::where('id', $id)->where('user_id', auth()->id())->firstOrFail(); + Storage::disk('public')->delete($music->file_path); + $music->delete(); + + return response()->json(['message' => 'Music deleted successfully']); + } +} diff --git a/app/Models/Music.php b/app/Models/Music.php new file mode 100644 index 0000000..6484659 --- /dev/null +++ b/app/Models/Music.php @@ -0,0 +1,30 @@ +belongsTo(User::class); + } + + // Accessor for full URL + public function getUrlAttribute() + { + return asset('storage/' . $this->file_path); + } +} diff --git a/database/migrations/2025_09_26_093732_create_music_table.php b/database/migrations/2025_09_26_093732_create_music_table.php new file mode 100644 index 0000000..aad83f6 --- /dev/null +++ b/database/migrations/2025_09_26_093732_create_music_table.php @@ -0,0 +1,32 @@ +id(); + $table->foreignId('user_id')->nullable()->constrained()->onDelete('cascade'); // optional user + $table->string('title'); + $table->string('artist')->nullable(); + $table->string('file_path'); // path in storage + $table->enum('type', ['public','private'])->default('private'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('music'); + } +}; diff --git a/routes/api.php b/routes/api.php index c5524d6..8dd5ced 100644 --- a/routes/api.php +++ b/routes/api.php @@ -13,6 +13,7 @@ use App\Http\Controllers\QuestionController; use App\Http\Controllers\SliderController; use App\Http\Controllers\ImageController; +use App\Http\Controllers\MusicController; Route::get('/test-hash', function() { $plain = 'amnk1380'; @@ -116,6 +117,11 @@ Route::put('/images/{id}', [ImageController::class, 'update']); // ✅ update Route::delete('/images/{id}', [ImageController::class, 'destroy']); // ✅ delete + /// music + Route::post('/music', [MusicController::class, 'store']); + Route::get('/music/all', [MusicController::class, 'all']); + Route::put('/music/{id}', [MusicController::class, 'update']); + Route::delete('/music/{id}', [MusicController::class, 'destroy']); }); \ No newline at end of file