feat: add music feature
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\MusicPlaylist;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class MusicPlaylistController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = MusicPlaylist::with(['category', 'image']);
|
||||
|
||||
if ($request->has('category_id')) {
|
||||
$query->where('category_id', $request->category_id);
|
||||
}
|
||||
|
||||
$playlists = $query->where('is_active', true)->orderBy('order')->get();
|
||||
|
||||
return response()->json($playlists);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'category_id' => 'required|exists:music_categories,id',
|
||||
'name' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'image_id' => 'nullable|exists:images,id',
|
||||
'order' => 'nullable|integer',
|
||||
'is_active' => 'nullable|boolean',
|
||||
]);
|
||||
|
||||
$data['slug'] = Str::slug($data['name']);
|
||||
|
||||
$playlist = MusicPlaylist::create($data);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Playlist created successfully',
|
||||
'playlist' => $playlist->load(['category', 'image'])
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$playlist = MusicPlaylist::with([
|
||||
'category',
|
||||
'image',
|
||||
'musics' => function($q) {
|
||||
$q->where('is_active', true)
|
||||
->with(['image', 'tags'])
|
||||
->orderBy('order');
|
||||
}
|
||||
])->findOrFail($id);
|
||||
|
||||
return response()->json($playlist);
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$playlist = MusicPlaylist::findOrFail($id);
|
||||
|
||||
$data = $request->validate([
|
||||
'category_id' => 'sometimes|exists:music_categories,id',
|
||||
'name' => 'sometimes|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'image_id' => 'nullable|exists:images,id',
|
||||
'order' => 'nullable|integer',
|
||||
'is_active' => 'nullable|boolean',
|
||||
]);
|
||||
|
||||
if (isset($data['name'])) {
|
||||
$data['slug'] = Str::slug($data['name']);
|
||||
}
|
||||
|
||||
$playlist->update($data);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Playlist updated successfully',
|
||||
'playlist' => $playlist->load(['category', 'image'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$playlist = MusicPlaylist::findOrFail($id);
|
||||
$playlist->delete();
|
||||
|
||||
return response()->json(['message' => 'Playlist deleted successfully']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user