feat: many category and sub can be in playlist
This commit is contained in:
@@ -8,57 +8,69 @@
|
||||
|
||||
class MusicPlaylistController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
public function index(Request $request, $categoryId = null)
|
||||
{
|
||||
$query = MusicPlaylist::with(['category', 'subcategory', 'image']);
|
||||
|
||||
if ($request->has('category_id')) {
|
||||
$query->where('category_id', $request->category_id)->whereNull('subcategory_id');
|
||||
$query = MusicPlaylist::with(['categories', 'subcategories', 'image']);
|
||||
|
||||
$categoryId = $categoryId ?? $request->input('category_id');
|
||||
|
||||
if ($categoryId) {
|
||||
$query->whereHas('categories', function ($q) use ($categoryId) {
|
||||
$q->where('music_categories.id', $categoryId);
|
||||
});
|
||||
}
|
||||
|
||||
if ($request->has('subcategory_id')) {
|
||||
$query->where('subcategory_id', $request->subcategory_id);
|
||||
|
||||
if ($request->filled('subcategory_id')) {
|
||||
$subcategoryId = $request->input('subcategory_id');
|
||||
$query->whereHas('subcategories', function ($q) use ($subcategoryId) {
|
||||
$q->where('music_subcategories.id', $subcategoryId);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
$playlists = $query->where('is_active', true)->orderBy('order')->get();
|
||||
|
||||
|
||||
return response()->json($playlists);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'category_id' => 'nullable|exists:music_categories,id',
|
||||
'subcategory_id' => 'nullable|exists:music_subcategories,id',
|
||||
'category_ids' => 'nullable|array',
|
||||
'category_ids.*' => 'integer|exists:music_categories,id',
|
||||
'subcategory_ids' => 'nullable|array',
|
||||
'subcategory_ids.*' => 'integer|exists:music_subcategories,id',
|
||||
'name' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'image_id' => 'nullable|exists:images,id',
|
||||
'order' => 'nullable|integer',
|
||||
'is_active' => 'nullable|boolean',
|
||||
]);
|
||||
|
||||
// Ensure either category_id or subcategory_id is provided
|
||||
if (!$data['category_id'] && !$data['subcategory_id']) {
|
||||
|
||||
// Ensure at least one category or subcategory is provided
|
||||
if (empty($data['category_ids']) && empty($data['subcategory_ids'])) {
|
||||
return response()->json([
|
||||
'message' => 'Either category_id or subcategory_id is required'
|
||||
'message' => 'At least one category or subcategory is required'
|
||||
], 422);
|
||||
}
|
||||
|
||||
|
||||
$data['slug'] = Str::slug($data['name']);
|
||||
|
||||
|
||||
$playlist = MusicPlaylist::create($data);
|
||||
|
||||
|
||||
$playlist->categories()->sync($data['category_ids'] ?? []);
|
||||
$playlist->subcategories()->sync($data['subcategory_ids'] ?? []);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Playlist created successfully',
|
||||
'playlist' => $playlist->load(['category', 'subcategory', 'image'])
|
||||
'playlist' => $playlist->load(['categories', 'subcategories', 'image'])
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$playlist = MusicPlaylist::with([
|
||||
'category',
|
||||
'subcategory',
|
||||
'categories',
|
||||
'subcategories',
|
||||
'image',
|
||||
'musics' => function($q) {
|
||||
$q->where('is_active', true)
|
||||
@@ -100,23 +112,34 @@ public function update(Request $request, $id)
|
||||
$playlist = MusicPlaylist::findOrFail($id);
|
||||
|
||||
$data = $request->validate([
|
||||
'category_id' => 'sometimes|exists:music_categories,id',
|
||||
'category_ids' => 'sometimes|array',
|
||||
'category_ids.*' => 'integer|exists:music_categories,id',
|
||||
'subcategory_ids' => 'sometimes|array',
|
||||
'subcategory_ids.*' => 'integer|exists:music_subcategories,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);
|
||||
|
||||
|
||||
if (array_key_exists('category_ids', $data)) {
|
||||
$playlist->categories()->sync($data['category_ids'] ?? []);
|
||||
}
|
||||
|
||||
if (array_key_exists('subcategory_ids', $data)) {
|
||||
$playlist->subcategories()->sync($data['subcategory_ids'] ?? []);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Playlist updated successfully',
|
||||
'playlist' => $playlist->load(['category', 'image'])
|
||||
'playlist' => $playlist->load(['categories', 'subcategories', 'image'])
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user