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;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use App\Models\MusicPlaylist;
|
||||||
use App\Models\Music;
|
use App\Models\Music;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
class MusicController extends Controller
|
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
|
// ✅ Upload music
|
||||||
public function store(Request $request)
|
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\Model;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
class Music extends Model
|
class Music extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
protected $table = 'music';
|
||||||
|
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'user_id',
|
'user_id', 'title', 'artist', 'file_path', 'type',
|
||||||
'title',
|
'playlist_id', 'image_id', 'duration', 'order', 'is_active'
|
||||||
'artist',
|
];
|
||||||
'file_path',
|
protected $casts = [
|
||||||
'type', // public or private
|
'duration' => 'string',
|
||||||
'image_id',
|
'order' => 'integer',
|
||||||
|
'is_active' => 'boolean',
|
||||||
];
|
];
|
||||||
|
|
||||||
// Relation to user (optional)
|
// Relation to user (optional)
|
||||||
public function user()
|
public function user()
|
||||||
{
|
{
|
||||||
return $this->belongsTo(User::class);
|
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'];
|
protected $appends = ['url', 'image_url'];
|
||||||
// Accessor for full URL
|
// Accessor for full URL
|
||||||
public function getUrlAttribute()
|
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');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('music_categories', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name');
|
||||||
|
$table->string('slug')->unique();
|
||||||
|
$table->text('description')->nullable();
|
||||||
|
$table->foreignId('image_id')->nullable()->constrained('images')->onDelete('set null');
|
||||||
|
$table->integer('order')->default(0);
|
||||||
|
$table->boolean('is_active')->default(true);
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('music_categories');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('music_playlists', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('category_id')->constrained('music_categories')->onDelete('cascade');
|
||||||
|
$table->string('name');
|
||||||
|
$table->string('slug')->unique();
|
||||||
|
$table->text('description')->nullable();
|
||||||
|
$table->foreignId('image_id')->nullable()->constrained('images')->onDelete('set null');
|
||||||
|
$table->integer('order')->default(0);
|
||||||
|
$table->boolean('is_active')->default(true);
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('music_playlists');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('music', function (Blueprint $table) {
|
||||||
|
$table->foreignId('playlist_id')->nullable()->constrained('music_playlists')->onDelete('cascade');
|
||||||
|
$table->string('duration')->nullable();
|
||||||
|
$table->integer('order')->default(0);
|
||||||
|
$table->boolean('is_active')->default(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('music', function (Blueprint $table) {
|
||||||
|
$table->dropForeign(['playlist_id']);
|
||||||
|
$table->dropColumn(['playlist_id', 'image_id', 'duration', 'order', 'is_active']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('music_tags', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('music_id')->constrained()->onDelete('cascade');
|
||||||
|
$table->foreignId('tag_id')->constrained()->onDelete('cascade');
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['music_id', 'tag_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('music_tags');
|
||||||
|
}
|
||||||
|
};
|
||||||
+18
-1
@@ -148,6 +148,23 @@
|
|||||||
Route::get('/media/{id}/comments', [MediaController::class, 'getComments']);
|
Route::get('/media/{id}/comments', [MediaController::class, 'getComments']);
|
||||||
Route::post('/media/{id}/feedback', [MediaController::class, 'submitFeedback']);
|
Route::post('/media/{id}/feedback', [MediaController::class, 'submitFeedback']);
|
||||||
|
|
||||||
|
//add note to media
|
||||||
Route::post('/media/{id}/note', [MediaController::class, 'storeNote']);
|
Route::post('/media/{id}/note', [MediaController::class, 'storeNote']);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Music Categories
|
||||||
|
Route::apiResource('music-categories', MusicCategoryController::class);
|
||||||
|
Route::get('public/music-categories', [MusicCategoryController::class, 'index']);
|
||||||
|
|
||||||
|
// Music Playlists
|
||||||
|
Route::apiResource('music-playlists', MusicPlaylistController::class);
|
||||||
|
Route::get('playlists/by-category/{categoryId}', [MusicPlaylistController::class, 'index']);
|
||||||
|
|
||||||
|
// Music
|
||||||
|
Route::get('music/playlist/{playlistId}', [MusicController::class, 'getMusicByPlaylist']);
|
||||||
|
Route::post('music/{musicId}/add-to-playlist', [MusicController::class, 'addToPlaylist']);
|
||||||
|
Route::delete('music/{musicId}/remove-from-playlist', [MusicController::class, 'removeFromPlaylist']);
|
||||||
|
Route::post('music/update-order', [MusicController::class, 'updateOrder']);
|
||||||
|
Route::apiResource('music', MusicController::class);
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user