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']); } }