feat: add music feature
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
use App\Models\MusicPlaylist;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class MusicCategoryController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$categories = MusicCategory::with(['image', 'playlists' => function($q) {
|
||||
$q->where('is_active', true)->orderBy('order');
|
||||
}])->where('is_active', true)->orderBy('order')->get();
|
||||
|
||||
return response()->json($categories);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'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']);
|
||||
|
||||
$category = MusicCategory::create($data);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Category created successfully',
|
||||
'category' => $category->load('image')
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$category = MusicCategory::with(['image', 'playlists' => function($q) {
|
||||
$q->with(['image', 'musics' => function($q2) {
|
||||
$q2->where('is_active', true)->with('image')->orderBy('order');
|
||||
}])->where('is_active', true)->orderBy('order');
|
||||
}])->findOrFail($id);
|
||||
|
||||
return response()->json($category);
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$category = MusicCategory::findOrFail($id);
|
||||
|
||||
$data = $request->validate([
|
||||
'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']);
|
||||
}
|
||||
|
||||
$category->update($data);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Category updated successfully',
|
||||
'category' => $category->load('image')
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$category = MusicCategory::findOrFail($id);
|
||||
$category->delete();
|
||||
|
||||
return response()->json(['message' => 'Category deleted successfully']);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -3,11 +3,79 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\MusicPlaylist;
|
||||
use App\Models\Music;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class MusicController extends Controller
|
||||
{
|
||||
{ public function getMusicByPlaylist($playlistId)
|
||||
{
|
||||
$playlist = MusicPlaylist::findOrFail($playlistId);
|
||||
|
||||
$music = Music::where('playlist_id', $playlistId)
|
||||
->where('is_active', true)
|
||||
->with(['image', 'tags'])
|
||||
->orderBy('order')
|
||||
->get();
|
||||
|
||||
return response()->json([
|
||||
'playlist' => $playlist->load('image'),
|
||||
'musics' => $music
|
||||
]);
|
||||
}
|
||||
|
||||
public function addToPlaylist(Request $request, $musicId)
|
||||
{
|
||||
$music = Music::where('id', $musicId)
|
||||
->where(function($q) {
|
||||
$q->where('type', 'public')
|
||||
->orWhere('user_id', auth()->id());
|
||||
})
|
||||
->firstOrFail();
|
||||
|
||||
$data = $request->validate([
|
||||
'playlist_id' => 'required|exists:music_playlists,id',
|
||||
'order' => 'nullable|integer',
|
||||
]);
|
||||
|
||||
$music->update([
|
||||
'playlist_id' => $data['playlist_id'],
|
||||
'order' => $data['order'] ?? $music->order,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Music added to playlist successfully',
|
||||
'music' => $music->load(['image', 'playlist'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function removeFromPlaylist($musicId)
|
||||
{
|
||||
$music = Music::where('id', $musicId)
|
||||
->where('user_id', auth()->id())
|
||||
->firstOrFail();
|
||||
|
||||
$music->update(['playlist_id' => null]);
|
||||
|
||||
return response()->json(['message' => 'Music removed from playlist']);
|
||||
}
|
||||
|
||||
public function updateOrder(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'musics' => 'required|array',
|
||||
'musics.*.id' => 'required|exists:music,id',
|
||||
'musics.*.order' => 'required|integer',
|
||||
]);
|
||||
|
||||
foreach ($data['musics'] as $item) {
|
||||
Music::where('id', $item['id'])
|
||||
->where('user_id', auth()->id())
|
||||
->update(['order' => $item['order']]);
|
||||
}
|
||||
|
||||
return response()->json(['message' => 'Order updated successfully']);
|
||||
}
|
||||
// ✅ Upload music
|
||||
public function store(Request $request)
|
||||
{
|
||||
|
||||
@@ -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']);
|
||||
}
|
||||
}
|
||||
+19
-8
@@ -5,23 +5,34 @@
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
class Music extends Model
|
||||
{
|
||||
{
|
||||
use HasFactory;
|
||||
protected $table = 'music';
|
||||
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'title',
|
||||
'artist',
|
||||
'file_path',
|
||||
'type', // public or private
|
||||
'image_id',
|
||||
'user_id', 'title', 'artist', 'file_path', 'type',
|
||||
'playlist_id', 'image_id', 'duration', 'order', 'is_active'
|
||||
];
|
||||
protected $casts = [
|
||||
'duration' => 'string',
|
||||
'order' => 'integer',
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
|
||||
// Relation to user (optional)
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
public function playlist(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MusicPlaylist::class, 'playlist_id');
|
||||
}
|
||||
public function tags(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Tag::class, 'music_tag');
|
||||
}
|
||||
|
||||
protected $appends = ['url', 'image_url'];
|
||||
// Accessor for full URL
|
||||
public function getUrlAttribute()
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class MusicCategory extends Model
|
||||
{
|
||||
protected $table = 'music_categories';
|
||||
|
||||
protected $fillable = [
|
||||
'name', 'slug', 'description', 'image_id', 'order', 'is_active'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean',
|
||||
'order' => 'integer',
|
||||
];
|
||||
|
||||
public function playlists(): HasMany
|
||||
{
|
||||
return $this->hasMany(MusicPlaylist::class, 'category_id');
|
||||
}
|
||||
|
||||
public function image(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Image::class);
|
||||
}
|
||||
|
||||
public function getActivePlaylistsAttribute()
|
||||
{
|
||||
return $this->playlists()->where('is_active', true)->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class MusicPlaylist extends Model
|
||||
{
|
||||
protected $table = 'music_playlists';
|
||||
|
||||
protected $fillable = [
|
||||
'category_id', 'name', 'slug', 'description', 'image_id', 'order', 'is_active'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean',
|
||||
'order' => 'integer',
|
||||
];
|
||||
|
||||
public function category(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MusicCategory::class, 'category_id');
|
||||
}
|
||||
|
||||
public function musics(): HasMany
|
||||
{
|
||||
return $this->hasMany(Music::class, 'playlist_id');
|
||||
}
|
||||
|
||||
public function image(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Image::class);
|
||||
}
|
||||
|
||||
public function getActiveMusicsAttribute()
|
||||
{
|
||||
return $this->musics()->where('is_active', true)->orderBy('order')->get();
|
||||
}
|
||||
|
||||
public function getTotalDurationAttribute()
|
||||
{
|
||||
return $this->musics()->where('is_active', true)->sum('duration');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user