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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user