feat: music feature added
This commit is contained in:
@@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use App\Models\Music;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
|
class MusicController extends Controller
|
||||||
|
{
|
||||||
|
// ✅ Upload music
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
$data = $request->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']);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
class Music extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'user_id',
|
||||||
|
'title',
|
||||||
|
'artist',
|
||||||
|
'file_path',
|
||||||
|
'type', // public or private
|
||||||
|
];
|
||||||
|
|
||||||
|
// Relation to user (optional)
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Accessor for full URL
|
||||||
|
public function getUrlAttribute()
|
||||||
|
{
|
||||||
|
return asset('storage/' . $this->file_path);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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::create('music', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('user_id')->nullable()->constrained()->onDelete('cascade'); // optional user
|
||||||
|
$table->string('title');
|
||||||
|
$table->string('artist')->nullable();
|
||||||
|
$table->string('file_path'); // path in storage
|
||||||
|
$table->enum('type', ['public','private'])->default('private');
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('music');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -13,6 +13,7 @@
|
|||||||
use App\Http\Controllers\QuestionController;
|
use App\Http\Controllers\QuestionController;
|
||||||
use App\Http\Controllers\SliderController;
|
use App\Http\Controllers\SliderController;
|
||||||
use App\Http\Controllers\ImageController;
|
use App\Http\Controllers\ImageController;
|
||||||
|
use App\Http\Controllers\MusicController;
|
||||||
|
|
||||||
Route::get('/test-hash', function() {
|
Route::get('/test-hash', function() {
|
||||||
$plain = 'amnk1380';
|
$plain = 'amnk1380';
|
||||||
@@ -116,6 +117,11 @@
|
|||||||
Route::put('/images/{id}', [ImageController::class, 'update']); // ✅ update
|
Route::put('/images/{id}', [ImageController::class, 'update']); // ✅ update
|
||||||
Route::delete('/images/{id}', [ImageController::class, 'destroy']); // ✅ delete
|
Route::delete('/images/{id}', [ImageController::class, 'destroy']); // ✅ delete
|
||||||
|
|
||||||
|
/// music
|
||||||
|
|
||||||
|
Route::post('/music', [MusicController::class, 'store']);
|
||||||
|
Route::get('/music/all', [MusicController::class, 'all']);
|
||||||
|
Route::put('/music/{id}', [MusicController::class, 'update']);
|
||||||
|
Route::delete('/music/{id}', [MusicController::class, 'destroy']);
|
||||||
|
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user