Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8339a03168 | ||
|
|
e314f95656 | ||
|
|
f3685d4ca9 | ||
|
|
2c40ce1e46 | ||
|
|
c980c2cd9d |
@@ -10,6 +10,25 @@
|
|||||||
class MusicController extends Controller
|
class MusicController extends Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
|
// Add this new method to your MusicController
|
||||||
|
public function getAllMusic()
|
||||||
|
{
|
||||||
|
$userId = auth()->id();
|
||||||
|
|
||||||
|
$music = Music::with(['image', 'playlist'])
|
||||||
|
->where('type', 'public')
|
||||||
|
->orWhere(function($query) use ($userId) {
|
||||||
|
$query->where('type', 'private')
|
||||||
|
->where('user_id', $userId);
|
||||||
|
})
|
||||||
|
->orderBy('created_at', 'desc')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
// Return as array directly (not wrapped in 'data' object)
|
||||||
|
// to match what your old Flutter app expects
|
||||||
|
return response()->json($music);
|
||||||
|
}
|
||||||
|
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$userId = auth()->id();
|
$userId = auth()->id();
|
||||||
@@ -104,11 +123,11 @@ public function store(Request $request)
|
|||||||
$data = $request->validate([
|
$data = $request->validate([
|
||||||
'title' => 'required|string|max:255',
|
'title' => 'required|string|max:255',
|
||||||
'artist' => 'nullable|string|max:255',
|
'artist' => 'nullable|string|max:255',
|
||||||
'file' => 'required|mimes:mp3,wav,ogg|max:20971520',
|
'file' => 'required|mimes:mp3,wav,ogg,flac|max:20971520',
|
||||||
'type' => 'nullable|in:public,private',
|
'type' => 'nullable|in:public,private',
|
||||||
'image_id' => 'nullable|exists:images,id',
|
'image_id' => 'nullable|exists:images,id',
|
||||||
'playlist_id' => 'nullable|exists:music_playlists,id',
|
'playlist_id' => 'nullable|exists:music_playlists,id',
|
||||||
'duration' => 'nullable|string|regex:/^(?:\d+:)?[0-5]?\d:[0-5]\d$/', // validates mm:ss or hh:mm:ss
|
'duration' => 'nullable|integer|min:1', // validates mm:ss or hh:mm:ss
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Handle file upload
|
// Handle file upload
|
||||||
@@ -188,10 +207,10 @@ public function update(Request $request, $id)
|
|||||||
'title' => 'nullable|string|max:255',
|
'title' => 'nullable|string|max:255',
|
||||||
'artist' => 'nullable|string|max:255',
|
'artist' => 'nullable|string|max:255',
|
||||||
'type' => 'nullable|in:public,private',
|
'type' => 'nullable|in:public,private',
|
||||||
'file' => 'nullable|mimes:mp3,wav,ogg|max:10240',
|
'file' => 'nullable|mimes:mp3,wav,ogg,flac|max:20971520',
|
||||||
'image_id' => 'nullable|exists:images,id',
|
'image_id' => 'nullable|exists:images,id',
|
||||||
'playlist_id' => 'nullable|exists:music_playlists,id',
|
'playlist_id' => 'nullable|exists:music_playlists,id',
|
||||||
'duration' => 'nullable|string|regex:/^(?:\d+:)?[0-5]?\d:[0-5]\d$/',
|
'duration' => 'nullable|integer|min:1',
|
||||||
'order' => 'nullable|integer',
|
'order' => 'nullable|integer',
|
||||||
'is_active' => 'nullable|boolean',
|
'is_active' => 'nullable|boolean',
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -10,10 +10,14 @@ class MusicPlaylistController extends Controller
|
|||||||
{
|
{
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$query = MusicPlaylist::with(['category', 'image']);
|
$query = MusicPlaylist::with(['category', 'subcategory', 'image']);
|
||||||
|
|
||||||
if ($request->has('category_id')) {
|
if ($request->has('category_id')) {
|
||||||
$query->where('category_id', $request->category_id);
|
$query->where('category_id', $request->category_id)->whereNull('subcategory_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->has('subcategory_id')) {
|
||||||
|
$query->where('subcategory_id', $request->subcategory_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
$playlists = $query->where('is_active', true)->orderBy('order')->get();
|
$playlists = $query->where('is_active', true)->orderBy('order')->get();
|
||||||
@@ -22,9 +26,10 @@ public function index(Request $request)
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$data = $request->validate([
|
$data = $request->validate([
|
||||||
'category_id' => 'required|exists:music_categories,id',
|
'category_id' => 'nullable|exists:music_categories,id',
|
||||||
|
'subcategory_id' => 'nullable|exists:music_subcategories,id',
|
||||||
'name' => 'required|string|max:255',
|
'name' => 'required|string|max:255',
|
||||||
'description' => 'nullable|string',
|
'description' => 'nullable|string',
|
||||||
'image_id' => 'nullable|exists:images,id',
|
'image_id' => 'nullable|exists:images,id',
|
||||||
@@ -32,15 +37,22 @@ public function store(Request $request)
|
|||||||
'is_active' => 'nullable|boolean',
|
'is_active' => 'nullable|boolean',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// Ensure either category_id or subcategory_id is provided
|
||||||
|
if (!$data['category_id'] && !$data['subcategory_id']) {
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Either category_id or subcategory_id is required'
|
||||||
|
], 422);
|
||||||
|
}
|
||||||
|
|
||||||
$data['slug'] = Str::slug($data['name']);
|
$data['slug'] = Str::slug($data['name']);
|
||||||
|
|
||||||
$playlist = MusicPlaylist::create($data);
|
$playlist = MusicPlaylist::create($data);
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Playlist created successfully',
|
'message' => 'Playlist created successfully',
|
||||||
'playlist' => $playlist->load(['category', 'image'])
|
'playlist' => $playlist->load(['category', 'subcategory', 'image'])
|
||||||
], 201);
|
], 201);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function show($id)
|
public function show($id)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,117 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
|
||||||
|
use App\Models\MusicSubcategory;
|
||||||
|
use App\Models\MusicCategory;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
class MusicSubcategoryController extends Controller
|
||||||
|
{
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$query = MusicSubcategory::with(['category', 'image', 'playlists' => function($q) {
|
||||||
|
$q->where('is_active', true)->orderBy('order');
|
||||||
|
}]);
|
||||||
|
|
||||||
|
if ($request->has('category_id')) {
|
||||||
|
$query->where('category_id', $request->category_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
$subcategories = $query->where('is_active', true)->orderBy('order')->get();
|
||||||
|
|
||||||
|
return response()->json($subcategories);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$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',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$slug = Str::slug($data['name']);
|
||||||
|
|
||||||
|
// Check for duplicate in same category
|
||||||
|
$existing = MusicSubcategory::where('category_id', $data['category_id'])
|
||||||
|
->where('slug', $slug)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($existing) {
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'A subcategory with this name already exists in this category',
|
||||||
|
'errors' => ['name' => ['The subcategory name must be unique within this category.']]
|
||||||
|
], 422);
|
||||||
|
}
|
||||||
|
|
||||||
|
$data['slug'] = $slug;
|
||||||
|
$subcategory = MusicSubcategory::create($data);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Subcategory created successfully',
|
||||||
|
'subcategory' => $subcategory->load(['category', 'image'])
|
||||||
|
], 201);
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'An error occurred while creating the subcategory',
|
||||||
|
'error' => $e->getMessage()
|
||||||
|
], 500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show($id)
|
||||||
|
{
|
||||||
|
$subcategory = MusicSubcategory::with([
|
||||||
|
'category',
|
||||||
|
'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($subcategory);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(Request $request, $id)
|
||||||
|
{
|
||||||
|
$subcategory = MusicSubcategory::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']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$subcategory->update($data);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Subcategory updated successfully',
|
||||||
|
'subcategory' => $subcategory->load(['category', 'image'])
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy($id)
|
||||||
|
{
|
||||||
|
$subcategory = MusicSubcategory::findOrFail($id);
|
||||||
|
$subcategory->delete();
|
||||||
|
|
||||||
|
return response()->json(['message' => 'Subcategory deleted successfully']);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,225 @@
|
|||||||
|
<?php
|
||||||
|
// app/Http/Controllers/SaveController.php
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use App\Models\SavedItem;
|
||||||
|
|
||||||
|
class SaveController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Save an item (music, media, breathing template, etc.)
|
||||||
|
*/
|
||||||
|
public function save(Request $request)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
'type' => 'required|string|in:music,media,breathing-template',
|
||||||
|
'id' => 'required|integer',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$model = $this->getModel($request->type, $request->id);
|
||||||
|
|
||||||
|
if (!$model) {
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Item not found'
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = $model->addSave();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Item saved successfully',
|
||||||
|
'is_saved' => true,
|
||||||
|
'saved_count' => $model->saved_count
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unsave an item
|
||||||
|
*/
|
||||||
|
public function unsave(Request $request)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
'type' => 'required|string|in:music,media,breathing-template',
|
||||||
|
'id' => 'required|integer',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$model = $this->getModel($request->type, $request->id);
|
||||||
|
|
||||||
|
if (!$model) {
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Item not found'
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = $model->removeSave();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Item removed from saved',
|
||||||
|
'is_saved' => false,
|
||||||
|
'saved_count' => $model->saved_count
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggle save status
|
||||||
|
*/
|
||||||
|
public function toggleSave(Request $request)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
'type' => 'required|string|in:music,media,breathing-template',
|
||||||
|
'id' => 'required|integer',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$model = $this->getModel($request->type, $request->id);
|
||||||
|
|
||||||
|
if (!$model) {
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Item not found'
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = $model->toggleSaveStatus();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => $result ? 'Item saved successfully' : 'Item removed from saved',
|
||||||
|
'is_saved' => $model->is_saved,
|
||||||
|
'saved_count' => $model->saved_count
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all saved items for the authenticated user
|
||||||
|
*/
|
||||||
|
public function mySavedItems(Request $request)
|
||||||
|
{
|
||||||
|
$type = $request->get('type'); // Optional filter by type
|
||||||
|
|
||||||
|
$query = SavedItem::with('saveable')
|
||||||
|
->where('user_id', auth()->id());
|
||||||
|
|
||||||
|
if ($type) {
|
||||||
|
$modelClass = $this->getModelClass($type);
|
||||||
|
$query->where('saveable_type', $modelClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
$savedItems = $query->latest()->paginate(20);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'data' => $savedItems,
|
||||||
|
'total' => $savedItems->total(),
|
||||||
|
'types' => [
|
||||||
|
'music' => 'App\\Models\\Music',
|
||||||
|
'media' => 'App\\Models\\Media',
|
||||||
|
'breathing-template' => 'App\\Models\\BreathingTemplate',
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Check if specific item is saved by user
|
||||||
|
*/
|
||||||
|
public function checkSaved(Request $request)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
'type' => 'required|string|in:music,media,breathing-template',
|
||||||
|
'id' => 'required|integer',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$model = $this->getModel($request->type, $request->id);
|
||||||
|
|
||||||
|
if (!$model) {
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Item not found'
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'is_saved' => $model->is_saved,
|
||||||
|
'saved_count' => $model->saved_count
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getModel($type, $id)
|
||||||
|
{
|
||||||
|
$modelClass = $this->getModelClass($type);
|
||||||
|
return $modelClass::find($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getModelClass($type)
|
||||||
|
{
|
||||||
|
$models = [
|
||||||
|
'music' => \App\Models\Music::class,
|
||||||
|
'media' => \App\Models\Media::class,
|
||||||
|
'breathing-template' => \App\Models\BreathingTemplate::class,
|
||||||
|
];
|
||||||
|
|
||||||
|
return $models[$type] ?? null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// public function mySavedItems(Request $request)
|
||||||
|
// {
|
||||||
|
// $type = $request->get('type');
|
||||||
|
|
||||||
|
// $query = SavedItem::with('saveable')
|
||||||
|
// ->where('user_id', auth()->id());
|
||||||
|
|
||||||
|
// if ($type) {
|
||||||
|
// $modelClass = $this->getModelClass($type);
|
||||||
|
// $query->where('saveable_type', $modelClass);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// $savedItems = $query->latest()->paginate(20);
|
||||||
|
|
||||||
|
// // Transform the response to include formatted data
|
||||||
|
// $transformedItems = $savedItems->map(function ($savedItem) {
|
||||||
|
// $item = $savedItem->saveable;
|
||||||
|
|
||||||
|
// if (!$item) return null;
|
||||||
|
|
||||||
|
// $baseData = [
|
||||||
|
// 'saved_id' => $savedItem->id,
|
||||||
|
// 'saved_at' => $savedItem->created_at,
|
||||||
|
// 'type' => class_basename($savedItem->saveable_type),
|
||||||
|
// ];
|
||||||
|
|
||||||
|
// // Add type-specific data
|
||||||
|
// if ($item instanceof \App\Models\Music) {
|
||||||
|
// return array_merge($baseData, [
|
||||||
|
// 'id' => $item->id,
|
||||||
|
// 'title' => $item->title,
|
||||||
|
// 'artist' => $item->artist,
|
||||||
|
// 'duration' => $item->duration_formatted ?? $item->duration,
|
||||||
|
// 'image_url' => $item->image_url,
|
||||||
|
// 'is_saved' => true,
|
||||||
|
// ]);
|
||||||
|
// } elseif ($item instanceof \App\Models\Media) {
|
||||||
|
// return array_merge($baseData, [
|
||||||
|
// 'id' => $item->id,
|
||||||
|
// 'title' => $item->title,
|
||||||
|
// 'caption' => $item->caption,
|
||||||
|
// 'type' => $item->type,
|
||||||
|
// 'duration' => $item->duration,
|
||||||
|
// 'image_url' => $item->image->url ?? null,
|
||||||
|
// 'is_saved' => true,
|
||||||
|
// ]);
|
||||||
|
// } elseif ($item instanceof \App\Models\BreathingTemplate) {
|
||||||
|
// return array_merge($baseData, [
|
||||||
|
// 'id' => $item->id,
|
||||||
|
// 'name' => $item->name,
|
||||||
|
// 'description' => $item->description,
|
||||||
|
// 'duration' => $item->duration,
|
||||||
|
// 'inhale' => $item->inhale,
|
||||||
|
// 'exhale' => $item->exhale,
|
||||||
|
// 'breath_hold' => $item->breath_hold,
|
||||||
|
// 'image_url' => $item->image_url,
|
||||||
|
// 'is_saved' => true,
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return $baseData;
|
||||||
|
// })->filter();
|
||||||
@@ -3,16 +3,18 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use App\Traits\HasSaves;
|
||||||
|
|
||||||
class BreathingTemplate extends Model
|
class BreathingTemplate extends Model
|
||||||
{
|
{
|
||||||
|
use HasSaves;
|
||||||
protected $fillable = ['user_id', 'name', 'inhale', 'exhale', 'breath_hold' , 'duration' , 'description','image_id' , 'source'];
|
protected $fillable = ['user_id', 'name', 'inhale', 'exhale', 'breath_hold' , 'duration' , 'description','image_id' , 'source'];
|
||||||
|
|
||||||
public function user()
|
public function user()
|
||||||
{
|
{
|
||||||
return $this->belongsTo(User::class);
|
return $this->belongsTo(User::class);
|
||||||
}
|
}
|
||||||
protected $appends = ['image_url'];
|
protected $appends = ['image_url', 'is_saved','saved_count'];
|
||||||
|
|
||||||
public function getImageUrlAttribute()
|
public function getImageUrlAttribute()
|
||||||
{
|
{
|
||||||
|
|||||||
+11
-7
@@ -5,9 +5,11 @@
|
|||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use App\Traits\HasRatings;
|
use App\Traits\HasRatings;
|
||||||
use App\Traits\HasComments;
|
use App\Traits\HasComments;
|
||||||
|
use App\Traits\HasSaves;
|
||||||
|
|
||||||
class Media extends Model
|
class Media extends Model
|
||||||
{
|
{
|
||||||
use HasRatings, HasComments;
|
use HasRatings, HasComments,HasSaves;
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'user_id',
|
'user_id',
|
||||||
'image_id',
|
'image_id',
|
||||||
@@ -21,16 +23,18 @@ class Media extends Model
|
|||||||
'visibility',
|
'visibility',
|
||||||
'is_premium'
|
'is_premium'
|
||||||
];
|
];
|
||||||
protected $appends = [
|
protected $appends = [
|
||||||
'average_rating',
|
'average_rating',
|
||||||
'user_rating',
|
'user_rating',
|
||||||
'ratings_count',
|
'ratings_count',
|
||||||
'comments_count',
|
'comments_count',
|
||||||
'has_user_commented', // Add this
|
'has_user_commented',
|
||||||
'user_comment', // Add this
|
'user_comment',
|
||||||
'user_comment_id', // Add this
|
'user_comment_id',
|
||||||
'has_user_rated' // Add this
|
'has_user_rated',
|
||||||
];
|
'is_saved',
|
||||||
|
'saved_count'
|
||||||
|
];
|
||||||
public function image()
|
public function image()
|
||||||
{
|
{
|
||||||
return $this->belongsTo(Image::class);
|
return $this->belongsTo(Image::class);
|
||||||
|
|||||||
+14
-6
@@ -10,10 +10,11 @@
|
|||||||
use Illuminate\Database\Eloquent\Relations\MorphMany; // ← Add this
|
use Illuminate\Database\Eloquent\Relations\MorphMany; // ← Add this
|
||||||
use App\Traits\HasRatings;
|
use App\Traits\HasRatings;
|
||||||
use App\Traits\HasComments;
|
use App\Traits\HasComments;
|
||||||
|
use App\Traits\HasSaves;
|
||||||
|
|
||||||
class Music extends Model
|
class Music extends Model
|
||||||
{
|
{
|
||||||
use HasFactory, HasRatings, HasComments;
|
use HasFactory, HasRatings, HasComments , HasSaves;
|
||||||
protected $table = 'music';
|
protected $table = 'music';
|
||||||
|
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ class Music extends Model
|
|||||||
'playlist_id', 'image_id', 'duration', 'order', 'is_active'
|
'playlist_id', 'image_id', 'duration', 'order', 'is_active'
|
||||||
];
|
];
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'duration' => 'string',
|
'duration' => 'integer',
|
||||||
'order' => 'integer',
|
'order' => 'integer',
|
||||||
'is_active' => 'boolean',
|
'is_active' => 'boolean',
|
||||||
];
|
];
|
||||||
@@ -43,10 +44,17 @@ public function tags(): BelongsToMany
|
|||||||
return $this->belongsToMany(Tag::class, 'music_tags');
|
return $this->belongsToMany(Tag::class, 'music_tags');
|
||||||
}
|
}
|
||||||
|
|
||||||
protected $appends = ['url', 'image_url' , 'average_rating', 'user_rating', 'comments_count' , 'has_user_commented', // Add this
|
protected $appends = ['url',
|
||||||
'user_comment', // Add this
|
'image_url' ,
|
||||||
'user_comment_id', // Add this
|
'average_rating',
|
||||||
'has_user_rated' // Add this];
|
'user_rating',
|
||||||
|
'comments_count' ,
|
||||||
|
'has_user_commented',
|
||||||
|
'user_comment',
|
||||||
|
'user_comment_id',
|
||||||
|
'has_user_rated' ,
|
||||||
|
'is_saved',
|
||||||
|
'saved_count'
|
||||||
];
|
];
|
||||||
|
|
||||||
public function getUrlAttribute()
|
public function getUrlAttribute()
|
||||||
|
|||||||
@@ -23,6 +23,21 @@ public function playlists(): HasMany
|
|||||||
return $this->hasMany(MusicPlaylist::class, 'category_id');
|
return $this->hasMany(MusicPlaylist::class, 'category_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function subcategories(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(MusicSubcategory::class, 'category_id');
|
||||||
|
}
|
||||||
|
// All playlists (including those in subcategories)
|
||||||
|
public function allPlaylists()
|
||||||
|
{
|
||||||
|
$playlists = collect($this->playlists);
|
||||||
|
|
||||||
|
foreach ($this->subcategories as $subcategory) {
|
||||||
|
$playlists = $playlists->merge($subcategory->playlists);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $playlists;
|
||||||
|
}
|
||||||
public function image(): BelongsTo
|
public function image(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(Image::class);
|
return $this->belongsTo(Image::class);
|
||||||
@@ -32,4 +47,18 @@ public function getActivePlaylistsAttribute()
|
|||||||
{
|
{
|
||||||
return $this->playlists()->where('is_active', true)->get();
|
return $this->playlists()->where('is_active', true)->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Total music count across all playlists and subcategories
|
||||||
|
public function getTotalMusicCountAttribute()
|
||||||
|
{
|
||||||
|
$count = $this->playlists->sum(function($playlist) {
|
||||||
|
return $playlist->musics->count();
|
||||||
|
});
|
||||||
|
|
||||||
|
foreach ($this->subcategories as $subcategory) {
|
||||||
|
$count += $subcategory->total_music_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $count;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ class MusicPlaylist extends Model
|
|||||||
protected $table = 'music_playlists';
|
protected $table = 'music_playlists';
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'category_id', 'name', 'slug', 'description', 'image_id', 'order', 'is_active'
|
'category_id', 'subcategory_id','name', 'slug', 'description', 'image_id', 'order', 'is_active'
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
@@ -23,6 +23,12 @@ public function category(): BelongsTo
|
|||||||
return $this->belongsTo(MusicCategory::class, 'category_id');
|
return $this->belongsTo(MusicCategory::class, 'category_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function subcategory(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(MusicSubcategory::class, 'subcategory_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function musics(): HasMany
|
public function musics(): HasMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(Music::class, 'playlist_id');
|
return $this->hasMany(Music::class, 'playlist_id');
|
||||||
|
|||||||
@@ -0,0 +1,85 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
class MusicSubcategory extends Model
|
||||||
|
{
|
||||||
|
protected $table = 'music_subcategories';
|
||||||
|
|
||||||
|
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 playlists(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(MusicPlaylist::class, 'subcategory_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function image(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Image::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getActivePlaylistsAttribute()
|
||||||
|
{
|
||||||
|
return $this->playlists()->where('is_active', true)->orderBy('order')->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get total music count across all playlists in this subcategory
|
||||||
|
public function getTotalMusicCountAttribute()
|
||||||
|
{
|
||||||
|
return $this->playlists()
|
||||||
|
->withCount('musics')
|
||||||
|
->get()
|
||||||
|
->sum('musics_count');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get total duration across all music in this subcategory
|
||||||
|
public function getTotalDurationAttribute()
|
||||||
|
{
|
||||||
|
$totalSeconds = 0;
|
||||||
|
foreach ($this->playlists as $playlist) {
|
||||||
|
foreach ($playlist->musics as $music) {
|
||||||
|
$totalSeconds += $this->durationToSeconds($music->duration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this->secondsToDuration($totalSeconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function durationToSeconds($duration)
|
||||||
|
{
|
||||||
|
if (!$duration) return 0;
|
||||||
|
$parts = explode(':', $duration);
|
||||||
|
if (count($parts) === 2) {
|
||||||
|
return (int)$parts[0] * 60 + (int)$parts[1];
|
||||||
|
} elseif (count($parts) === 3) {
|
||||||
|
return (int)$parts[0] * 3600 + (int)$parts[1] * 60 + (int)$parts[2];
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function secondsToDuration($seconds)
|
||||||
|
{
|
||||||
|
$hours = floor($seconds / 3600);
|
||||||
|
$minutes = floor(($seconds % 3600) / 60);
|
||||||
|
$secs = $seconds % 60;
|
||||||
|
|
||||||
|
if ($hours > 0) {
|
||||||
|
return sprintf("%d:%02d:%02d", $hours, $minutes, $secs);
|
||||||
|
}
|
||||||
|
return sprintf("%d:%02d", $minutes, $secs);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||||
|
class SavedItem extends Model
|
||||||
|
{
|
||||||
|
protected $table = 'saved_items';
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'user_id',
|
||||||
|
'saveable_id',
|
||||||
|
'saveable_type',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function user(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function saveable(): MorphTo
|
||||||
|
{
|
||||||
|
return $this->morphTo();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper to get saved items by type
|
||||||
|
public static function getSavedItemsForUser($userId, $type = null)
|
||||||
|
{
|
||||||
|
$query = self::with('saveable')->where('user_id', $userId);
|
||||||
|
|
||||||
|
if ($type) {
|
||||||
|
$query->where('saveable_type', $type);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query->latest()->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if user has saved specific item
|
||||||
|
public static function isSavedByUser($userId, $saveableId, $saveableType)
|
||||||
|
{
|
||||||
|
return self::where([
|
||||||
|
'user_id' => $userId,
|
||||||
|
'saveable_id' => $saveableId,
|
||||||
|
'saveable_type' => $saveableType,
|
||||||
|
])->exists();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
// app/Traits/HasSaves.php
|
||||||
|
namespace App\Traits;
|
||||||
|
|
||||||
|
use App\Models\SavedItem;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||||
|
|
||||||
|
trait HasSaves
|
||||||
|
{
|
||||||
|
public function saves()
|
||||||
|
{
|
||||||
|
return $this->morphMany(SavedItem::class, 'saveable');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getIsSavedAttribute()
|
||||||
|
{
|
||||||
|
if (!auth()->check()) return false;
|
||||||
|
|
||||||
|
return $this->saves()
|
||||||
|
->where('user_id', auth()->id())
|
||||||
|
->exists();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSavedCountAttribute()
|
||||||
|
{
|
||||||
|
return $this->saves()->count();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toggleSaveStatus()
|
||||||
|
{
|
||||||
|
if ($this->getIsSavedAttribute()) {
|
||||||
|
return $this->removeSave();
|
||||||
|
} else {
|
||||||
|
return $this->addSave();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addSave()
|
||||||
|
{
|
||||||
|
if ($this->getIsSavedAttribute()) return false;
|
||||||
|
|
||||||
|
return SavedItem::create([
|
||||||
|
'user_id' => auth()->id(),
|
||||||
|
'saveable_id' => $this->id,
|
||||||
|
'saveable_type' => get_class($this),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeSave()
|
||||||
|
{
|
||||||
|
if (!$this->getIsSavedAttribute()) return false;
|
||||||
|
|
||||||
|
return SavedItem::where([
|
||||||
|
'user_id' => auth()->id(),
|
||||||
|
'saveable_id' => $this->id,
|
||||||
|
'saveable_type' => get_class($this),
|
||||||
|
])->delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
<?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_subcategories', 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();
|
||||||
|
|
||||||
|
$table->index(['category_id', 'order']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('music_subcategories');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -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::table('music_playlists', function (Blueprint $table) {
|
||||||
|
$table->foreignId('subcategory_id')->nullable()->after('category_id')
|
||||||
|
->constrained('music_subcategories')->onDelete('cascade');
|
||||||
|
// Make category_id nullable since playlist can belong to subcategory
|
||||||
|
$table->foreignId('category_id')->nullable()->change();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('music_playlists', function (Blueprint $table) {
|
||||||
|
$table->dropForeign(['subcategory_id']);
|
||||||
|
$table->dropColumn('subcategory_id');
|
||||||
|
$table->foreignId('category_id')->nullable(false)->change();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?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->integer('duration')->nullable()->change();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('music', function (Blueprint $table) {
|
||||||
|
$table->string('duration')->nullable()->change();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -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('saved_items', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('user_id')->constrained()->onDelete('cascade');
|
||||||
|
$table->morphs('saveable'); // saveable_id + saveable_type
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['user_id', 'saveable_id', 'saveable_type']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('saved_items');
|
||||||
|
}
|
||||||
|
};
|
||||||
+21
-1
@@ -19,7 +19,8 @@
|
|||||||
use App\Http\Controllers\MusicPlaylistController;
|
use App\Http\Controllers\MusicPlaylistController;
|
||||||
use App\Http\Controllers\RatingController;
|
use App\Http\Controllers\RatingController;
|
||||||
use App\Http\Controllers\CommentController;
|
use App\Http\Controllers\CommentController;
|
||||||
|
use App\Http\Controllers\MusicSubcategoryController;
|
||||||
|
use App\Http\Controllers\SaveController;
|
||||||
|
|
||||||
Route::get('/test-hash', function() {
|
Route::get('/test-hash', function() {
|
||||||
$plain = 'amnk1380';
|
$plain = 'amnk1380';
|
||||||
@@ -166,6 +167,14 @@
|
|||||||
Route::apiResource('music-playlists', MusicPlaylistController::class);
|
Route::apiResource('music-playlists', MusicPlaylistController::class);
|
||||||
Route::get('playlists/by-category/{categoryId}', [MusicPlaylistController::class, 'index']);
|
Route::get('playlists/by-category/{categoryId}', [MusicPlaylistController::class, 'index']);
|
||||||
|
|
||||||
|
// Subcategory routes
|
||||||
|
Route::apiResource('music-subcategories', MusicSubcategoryController::class);
|
||||||
|
Route::get('subcategories/by-category/{categoryId}', [MusicSubcategoryController::class, 'index']);
|
||||||
|
|
||||||
|
|
||||||
|
// Music Routes - Add this line before your other routes
|
||||||
|
Route::get('music/all', [MusicController::class, 'getAllMusic']); // For old app compatibility
|
||||||
|
|
||||||
// Music
|
// Music
|
||||||
Route::get('music/playlist/{playlistId}', [MusicController::class, 'getMusicByPlaylist']);
|
Route::get('music/playlist/{playlistId}', [MusicController::class, 'getMusicByPlaylist']);
|
||||||
Route::post('music/{musicId}/add-to-playlist', [MusicController::class, 'addToPlaylist']);
|
Route::post('music/{musicId}/add-to-playlist', [MusicController::class, 'addToPlaylist']);
|
||||||
@@ -174,6 +183,7 @@
|
|||||||
Route::apiResource('music', MusicController::class);
|
Route::apiResource('music', MusicController::class);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Generic Rating Routes (works for both music and media)
|
// Generic Rating Routes (works for both music and media)
|
||||||
Route::prefix('ratings')->group(function () {
|
Route::prefix('ratings')->group(function () {
|
||||||
Route::post('{type}/{id}', [RatingController::class, 'rate']);
|
Route::post('{type}/{id}', [RatingController::class, 'rate']);
|
||||||
@@ -191,4 +201,14 @@
|
|||||||
Route::delete('{type}/{id}/{commentId}', [CommentController::class, 'deleteComment']);
|
Route::delete('{type}/{id}/{commentId}', [CommentController::class, 'deleteComment']);
|
||||||
Route::get('most/{type}', [CommentController::class, 'mostCommented']);
|
Route::get('most/{type}', [CommentController::class, 'mostCommented']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// Save routes (works for all models)
|
||||||
|
Route::prefix('saves')->group(function () {
|
||||||
|
Route::post('/save', [SaveController::class, 'save']);
|
||||||
|
Route::post('/unsave', [SaveController::class, 'unsave']);
|
||||||
|
Route::post('/toggle', [SaveController::class, 'toggleSave']);
|
||||||
|
Route::get('/my-saved', [SaveController::class, 'mySavedItems']);
|
||||||
|
Route::post('/check', [SaveController::class, 'checkSaved']);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user