Compare commits
20
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8339a03168 | ||
|
|
e314f95656 | ||
|
|
f3685d4ca9 | ||
|
|
2c40ce1e46 | ||
|
|
c980c2cd9d | ||
|
|
d0d48866f0 | ||
|
|
02448db346 | ||
|
|
6f5224a5d9 | ||
|
|
e446de79fd | ||
|
|
65db1bd5da | ||
|
|
b63fdc5439 | ||
|
|
676522d70d | ||
|
|
4d0823ef86 | ||
|
|
40f3db00ba | ||
|
|
29005f00da | ||
|
|
be69e5cfda | ||
|
|
23cd9c7b38 | ||
|
|
834c09025f | ||
|
|
12a0500520 | ||
|
|
6912059469 |
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CommentController extends Controller
|
||||
{
|
||||
/**
|
||||
* Add comment to any model
|
||||
*/
|
||||
public function addComment(Request $request, $type, $id)
|
||||
{
|
||||
$request->validate([
|
||||
'content' => 'required|string|max:1000',
|
||||
]);
|
||||
|
||||
$model = $this->getModel($type, $id);
|
||||
$this->checkAccess($model);
|
||||
|
||||
$comment = $model->comments()->create([
|
||||
'user_id' => auth()->id(),
|
||||
'content' => $request->content,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Comment added successfully',
|
||||
'comment' => $comment->load('user'),
|
||||
], 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all comments for a model
|
||||
*/
|
||||
public function getComments($type, $id)
|
||||
{
|
||||
$model = $this->getModel($type, $id);
|
||||
$this->checkAccess($model);
|
||||
|
||||
$comments = $model->comments()
|
||||
->with('user')
|
||||
->latest()
|
||||
->paginate(20);
|
||||
|
||||
return response()->json($comments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update comment
|
||||
*/
|
||||
public function updateComment(Request $request, $type, $id, $commentId)
|
||||
{
|
||||
$request->validate([
|
||||
'content' => 'required|string|max:1000',
|
||||
]);
|
||||
|
||||
$comment = Comment::where('id', $commentId)
|
||||
->where('user_id', auth()->id())
|
||||
->where('commentable_id', $id)
|
||||
->where('commentable_type', $this->getModelClass($type))
|
||||
->firstOrFail();
|
||||
|
||||
$comment->update([
|
||||
'content' => $request->content,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Comment updated successfully',
|
||||
'comment' => $comment->load('user'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete comment
|
||||
*/
|
||||
public function deleteComment($type, $id, $commentId)
|
||||
{
|
||||
$comment = Comment::where('id', $commentId)
|
||||
->where('user_id', auth()->id())
|
||||
->where('commentable_id', $id)
|
||||
->where('commentable_type', $this->getModelClass($type))
|
||||
->firstOrFail();
|
||||
|
||||
$comment->delete();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Comment deleted successfully',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get most commented items
|
||||
*/
|
||||
public function mostCommented($type)
|
||||
{
|
||||
$modelClass = $this->getModelClass($type);
|
||||
$userId = auth()->id();
|
||||
|
||||
$items = $modelClass::with(['image', 'user'])
|
||||
->withCount('comments')
|
||||
->where(function($query) use ($modelClass, $userId) {
|
||||
if (property_exists($modelClass, 'type')) {
|
||||
$query->where('type', 'public')
|
||||
->orWhere(function($q) use ($userId) {
|
||||
$q->where('type', 'private')
|
||||
->where('user_id', $userId);
|
||||
});
|
||||
}
|
||||
})
|
||||
->orderBy('comments_count', 'desc')
|
||||
->limit(10)
|
||||
->get();
|
||||
|
||||
return response()->json($items);
|
||||
}
|
||||
|
||||
private function getModel($type, $id)
|
||||
{
|
||||
$modelClass = $this->getModelClass($type);
|
||||
$model = $modelClass::findOrFail($id);
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
private function getModelClass($type)
|
||||
{
|
||||
$models = [
|
||||
'music' => \App\Models\Music::class,
|
||||
'media' => \App\Models\Media::class,
|
||||
];
|
||||
|
||||
if (!isset($models[$type])) {
|
||||
abort(404, 'Invalid model type');
|
||||
}
|
||||
|
||||
return $models[$type];
|
||||
}
|
||||
|
||||
private function checkAccess($model)
|
||||
{
|
||||
if (property_exists($model, 'type') && $model->type === 'private') {
|
||||
if (auth()->id() !== $model->user_id) {
|
||||
abort(403, 'You do not have access to this item');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
use App\Models\Media;
|
||||
use App\Models\Category;
|
||||
use App\Models\Tag;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
@@ -22,7 +22,7 @@ public function store(Request $request)
|
||||
'category_name' => 'nullable|string|max:255',
|
||||
'image_id' => 'nullable|exists:images,id',
|
||||
'duration' => 'nullable|integer',
|
||||
|
||||
'is_premium' => 'nullable|boolean',
|
||||
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:512000',
|
||||
'external_url' => 'nullable|string',
|
||||
'visibility' => 'nullable|in:public,private',
|
||||
@@ -55,6 +55,7 @@ public function store(Request $request)
|
||||
'category_id' => $categoryId,
|
||||
'duration' => $data['duration'] ?? null,
|
||||
'visibility' => $data['visibility'] ?? 'public',
|
||||
'is_premium'=> $data['is_premium'] ?? false
|
||||
]);
|
||||
if (!empty($data['tags'])) {
|
||||
$tagIds = [];
|
||||
@@ -73,49 +74,263 @@ public function store(Request $request)
|
||||
}
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = Media::with(['image', 'category', 'myNote', 'tags'])
|
||||
->where(function($q) {
|
||||
$query = Media::with(['image', 'category', 'myNote', 'tags','comments'])
|
||||
->where(function ($q) {
|
||||
$q->where('visibility', 'public')
|
||||
->orWhere('user_id', auth()->id());
|
||||
});
|
||||
if ($request->filled('category')) {
|
||||
$query->where('category_id', $request->category);
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| 1️⃣ Multi Category
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
if ($request->filled('categories')) {
|
||||
$categories = explode(',', $request->categories);
|
||||
$query->whereIn('category_id', $categories);
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| 2️⃣ Multi Duration Ranges
|
||||
|--------------------------------------------------------------------------
|
||||
| duration stored in minutes (integer)
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
if ($request->filled('durations')) {
|
||||
|
||||
$ranges = explode(',', $request->durations);
|
||||
|
||||
$query->where(function ($q) use ($ranges) {
|
||||
|
||||
foreach ($ranges as $range) {
|
||||
|
||||
if ($range === '1-2') {
|
||||
$q->orWhereBetween('duration', [1, 2]);
|
||||
}
|
||||
|
||||
if ($range === '2-5') {
|
||||
$q->orWhereBetween('duration', [3, 5]);
|
||||
}
|
||||
|
||||
if ($range === '5-10') {
|
||||
$q->orWhereBetween('duration', [6, 10]);
|
||||
}
|
||||
if ($range === '10-30') {
|
||||
$q->orWhereBetween('duration', [10, 30]);
|
||||
}
|
||||
if ($range === '30-60') {
|
||||
$q->orWhereBetween('duration', [30, 60]);
|
||||
}
|
||||
if ($range === '60-120') {
|
||||
$q->orWhereBetween('duration', [60, 120]);
|
||||
}
|
||||
|
||||
if ($range === 'other') {
|
||||
$q->orWhere('duration', '>', 120);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| 3️⃣ Tags
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
if ($request->filled('tags')) {
|
||||
$tags = explode(',', $request->tags);
|
||||
|
||||
$query->whereHas('tags', function($q) use ($tags) {
|
||||
$query->whereHas('tags', function ($q) use ($tags) {
|
||||
$q->whereIn('name', $tags);
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| 4️⃣ Search
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
if ($request->filled('search')) {
|
||||
|
||||
$search = $request->search;
|
||||
|
||||
$query->where(function($q) use ($search) {
|
||||
$query->where(function ($q) use ($search) {
|
||||
|
||||
// title & caption
|
||||
$q->where('title', 'LIKE', "%$search%")
|
||||
->orWhere('caption', 'LIKE', "%$search%")
|
||||
->orWhere('caption', 'LIKE', "%$search%")
|
||||
->orWhereHas('category', function ($c) use ($search) {
|
||||
$c->where('name', 'LIKE', "%$search%");
|
||||
})
|
||||
->orWhereHas('tags', function ($t) use ($search) {
|
||||
$t->where('name', 'LIKE', "%$search%");
|
||||
});
|
||||
|
||||
// category (join)
|
||||
->orWhereHas('category', function($c) use ($search) {
|
||||
$c->where('name', 'LIKE', "%$search%");
|
||||
})
|
||||
|
||||
// tags
|
||||
->orWhereHas('tags', function($t) use ($search) {
|
||||
$t->where('name', 'LIKE', "%$search%");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return response()->json(
|
||||
$query->orderBy('created_at', 'desc')->get()
|
||||
);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$media = Media::with(['image', 'category', 'myNote', 'tags'])
|
||||
public function submitFeedback(Request $request, $mediaId)
|
||||
{
|
||||
$request->validate([
|
||||
'stars' => 'nullable|integer|min:1|max:5',
|
||||
'content' => 'nullable|string|max:2000',
|
||||
]);
|
||||
|
||||
// حداقل یکی باید ارسال شود
|
||||
if (!$request->filled('stars') && !$request->filled('content')) {
|
||||
return response()->json([
|
||||
'message' => 'Stars or comment is required'
|
||||
], 422);
|
||||
}
|
||||
|
||||
$media = Media::where('id', $mediaId)
|
||||
->where(function ($q) {
|
||||
$q->where('visibility', 'public')
|
||||
->orWhere('user_id', auth()->id());
|
||||
})
|
||||
->firstOrFail();
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
|
||||
$rating = null;
|
||||
$comment = null;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| ⭐ Handle Rating (update or create)
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
if ($request->filled('stars')) {
|
||||
$rating = $media->ratings()->updateOrCreate(
|
||||
['user_id' => auth()->id()],
|
||||
['stars' => $request->stars]
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| 💬 Handle Comment (create only if exists)
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
if ($request->filled('content')) {
|
||||
$comment = $media->comments()->create([
|
||||
'user_id' => auth()->id(),
|
||||
'content' => $request->content,
|
||||
]);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Feedback submitted successfully',
|
||||
'average_rating' => $media->fresh()->average_rating,
|
||||
'your_rating' => optional($rating)->stars,
|
||||
'comment' => $comment ? $comment->load('user') : null,
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
|
||||
DB::rollBack();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Something went wrong',
|
||||
'error' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
public function filters(Request $request)
|
||||
{
|
||||
$baseQuery = Media::query()
|
||||
->where(function ($q) {
|
||||
$q->where('visibility', 'public')
|
||||
->orWhere('user_id', auth()->id());
|
||||
});
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| 1️⃣ Categories with media count
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
$categories = Category::select(
|
||||
'categories.id',
|
||||
'categories.name',
|
||||
DB::raw('COUNT(media.id) as media_count')
|
||||
)
|
||||
->leftJoin('media', function ($join) {
|
||||
$join->on('categories.id', '=', 'media.category_id')
|
||||
->where(function ($q) {
|
||||
$q->where('media.visibility', 'public')
|
||||
->orWhere('media.user_id', auth()->id());
|
||||
});
|
||||
})
|
||||
->groupBy('categories.id', 'categories.name')
|
||||
->orderByDesc('media_count')
|
||||
->get();
|
||||
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| 2️⃣ Duration ranges
|
||||
|--------------------------------------------------------------------------
|
||||
| duration is stored in seconds
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
$durations = Media::select(
|
||||
DB::raw("
|
||||
CASE
|
||||
WHEN duration BETWEEN 1 AND 2 THEN '1-2'
|
||||
WHEN duration BETWEEN 3 AND 5 THEN '2-5'
|
||||
WHEN duration BETWEEN 5 AND 10 THEN '5-10'
|
||||
WHEN duration BETWEEN 10 AND 20 THEN '10-20'
|
||||
WHEN duration BETWEEN 20 AND 30 THEN '20-30'
|
||||
WHEN duration BETWEEN 60 AND 120 THEN '60-120'
|
||||
ELSE 'other'
|
||||
END as duration_range
|
||||
"),
|
||||
DB::raw('COUNT(*) as total')
|
||||
)
|
||||
->whereNotNull('duration')
|
||||
->where(function ($q) {
|
||||
$q->where('visibility', 'public')
|
||||
->orWhere('user_id', auth()->id());
|
||||
})
|
||||
->groupBy('duration_range')
|
||||
->get();
|
||||
|
||||
return response()->json([
|
||||
'categories' => $categories,
|
||||
'durations' => $durations,
|
||||
]);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$media = Media::with([
|
||||
'image',
|
||||
'category',
|
||||
'myNote',
|
||||
'tags',
|
||||
'comments' => function($query) {
|
||||
$query->with('user')->latest()->limit(10);
|
||||
},
|
||||
'ratings'
|
||||
])
|
||||
->where('id', $id)
|
||||
->where(function($query) {
|
||||
$query->where('visibility', 'public')
|
||||
@@ -123,7 +338,16 @@ public function show($id)
|
||||
})
|
||||
->firstOrFail();
|
||||
|
||||
return response()->json([
|
||||
// Get user's specific comment
|
||||
$userComment = $media->userComment();
|
||||
|
||||
// Get paginated comments
|
||||
$comments = $media->comments()
|
||||
->with('user')
|
||||
->latest()
|
||||
->paginate(15);
|
||||
|
||||
return response()->json([
|
||||
'id' => $media->id,
|
||||
'title' => $media->title,
|
||||
'caption' => $media->caption,
|
||||
@@ -134,14 +358,78 @@ public function show($id)
|
||||
'visibility' => $media->visibility,
|
||||
'created_at' => $media->created_at,
|
||||
'updated_at' => $media->updated_at,
|
||||
'is_premium' => $media->is_premium,
|
||||
'image' => $media->image,
|
||||
'category' => $media->category,
|
||||
'tags' => $media->tags,
|
||||
'myNote' => $media->myNote,
|
||||
'is_saved' => $media->is_saved,
|
||||
'statistics' => [
|
||||
'average_rating' => $media->average_rating,
|
||||
'total_ratings' => $media->ratings_count,
|
||||
'total_comments' => $media->comments_count,
|
||||
'rating_distribution' => $media->rating_distribution,
|
||||
],
|
||||
'user_interaction' => [
|
||||
'has_rated' => $media->has_user_rated,
|
||||
'user_rating' => $media->user_rating,
|
||||
'has_commented' => $media->has_user_commented,
|
||||
'user_comment' => $media->user_comment,
|
||||
'user_comment_id' => $media->user_comment_id,
|
||||
],
|
||||
'comments' => $comments,
|
||||
]);
|
||||
}
|
||||
public function rate(Request $request, $mediaId)
|
||||
{
|
||||
$request->validate([
|
||||
'stars' => 'required|integer|min:1|max:5'
|
||||
]);
|
||||
}
|
||||
|
||||
$media = Media::findOrFail($mediaId);
|
||||
|
||||
$rating = $media->ratings()->updateOrCreate(
|
||||
['user_id' => auth()->id()],
|
||||
['stars' => $request->stars]
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Rating saved',
|
||||
'average_rating' => $media->fresh()->average_rating,
|
||||
'your_rating' => $rating->stars
|
||||
]);
|
||||
}
|
||||
|
||||
public function storeComment(Request $request, $mediaId)
|
||||
{
|
||||
$request->validate([
|
||||
'content' => 'required|string|max:1000'
|
||||
]);
|
||||
|
||||
$media = Media::findOrFail($mediaId);
|
||||
|
||||
$comment = $media->comments()->create([
|
||||
'user_id' => auth()->id(),
|
||||
'content' => $request->content
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Comment added',
|
||||
'comment' => $comment->load('user')
|
||||
]);
|
||||
}
|
||||
|
||||
public function getComments($mediaId)
|
||||
{
|
||||
$media = Media::findOrFail($mediaId);
|
||||
|
||||
return response()->json(
|
||||
$media->comments()
|
||||
->with('user')
|
||||
->latest()
|
||||
->paginate(10)
|
||||
);
|
||||
}
|
||||
// UPDATE media
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
@@ -157,7 +445,7 @@ public function update(Request $request, $id)
|
||||
// support both: category_id or category_name
|
||||
'category_id' => 'nullable|exists:categories,id',
|
||||
'category_name' => 'nullable|string|max:255',
|
||||
|
||||
'is_premium' => 'nullable|boolean',
|
||||
'image_id' => 'nullable|exists:images,id',
|
||||
'duration' => 'nullable|integer',
|
||||
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:512000',
|
||||
@@ -189,6 +477,7 @@ public function update(Request $request, $id)
|
||||
'caption' => $data['caption'] ?? $media->caption,
|
||||
'type' => $data['type'] ?? $media->type,
|
||||
'category_id' => $categoryId,
|
||||
'is_premium' => $data['is_premium'] ?? $media->is_premium,
|
||||
'duration' => $data['duration'] ?? $media->duration,
|
||||
'external_url' => $data['external_url'] ?? $media->external_url,
|
||||
'visibility' => $data['visibility'] ?? $media->visibility,
|
||||
@@ -239,12 +528,20 @@ public function destroy($id)
|
||||
}
|
||||
|
||||
// SAVE media
|
||||
public function saveMedia($id)
|
||||
{
|
||||
auth()->user()->savedMedia()->syncWithoutDetaching([$id]);
|
||||
public function toggleSaveMedia($id)
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
if ($user->savedMedia()->where('media_id', $id)->exists()) {
|
||||
// already saved → unsave
|
||||
$user->savedMedia()->detach($id);
|
||||
return response()->json(['message' => 'Unsaved!']);
|
||||
} else {
|
||||
// not saved → save
|
||||
$user->savedMedia()->attach($id);
|
||||
return response()->json(['message' => 'Saved!']);
|
||||
}
|
||||
}
|
||||
|
||||
// GET saved
|
||||
public function saved()
|
||||
@@ -252,19 +549,40 @@ public function saved()
|
||||
return auth()->user()->savedMedia()->with(['image','category' , 'myNote' , 'tags'])->get();
|
||||
}
|
||||
|
||||
public function storeNote(Request $request, $mediaId)
|
||||
public function storeNote(Request $request, $mediaId)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'content' => 'required|string'
|
||||
]);
|
||||
|
||||
$media = Media::findOrFail($mediaId);
|
||||
$userId = auth()->id();
|
||||
|
||||
$note = $media->notes()->create([
|
||||
'user_id' => auth()->id(),
|
||||
'content' => $data['content'],
|
||||
$content = $request->input('content');
|
||||
|
||||
// Check if note already exists for this user
|
||||
$note = $media->notes()->where('user_id', $userId)->first();
|
||||
|
||||
if (empty($content)) {
|
||||
// If content is empty, delete the note if it exists
|
||||
if ($note) {
|
||||
$note->delete();
|
||||
return response()->json(['message' => 'Note deleted']);
|
||||
}
|
||||
return response()->json(['message' => 'No note to delete']);
|
||||
}
|
||||
|
||||
if ($note) {
|
||||
// Update existing note
|
||||
$note->update(['content' => $content]);
|
||||
} else {
|
||||
// Create new note
|
||||
$note = $media->notes()->create([
|
||||
'user_id' => $userId,
|
||||
'content' => $content,
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Note saved successfully',
|
||||
'note' => $note
|
||||
]);
|
||||
|
||||
return response()->json(['message' => 'Note added', 'note' => $note]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
use App\Models\MusicPlaylist;
|
||||
use App\Models\MusicCategory;
|
||||
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)
|
||||
{
|
||||
try {
|
||||
$data = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'image_id' => 'nullable|exists:images,id',
|
||||
'order' => 'nullable|integer',
|
||||
'is_active' => 'nullable|boolean',
|
||||
]);
|
||||
|
||||
// Check for duplicate name
|
||||
$slug = Str::slug($data['name']);
|
||||
$existingCategory = MusicCategory::where('slug', $slug)->first();
|
||||
|
||||
if ($existingCategory) {
|
||||
return response()->json([
|
||||
'message' => 'A category with this name already exists',
|
||||
'errors' => [
|
||||
'name' => ['The category name "' . $data['name'] . '" is already taken. Please use a different name.']
|
||||
]
|
||||
], 422);
|
||||
}
|
||||
|
||||
// Optional: Check for duplicate name with case-insensitive comparison
|
||||
$existingName = MusicCategory::whereRaw('LOWER(name) = ?', [strtolower($data['name'])])->first();
|
||||
if ($existingName) {
|
||||
return response()->json([
|
||||
'message' => 'A category with a similar name already exists',
|
||||
'errors' => [
|
||||
'name' => ['Category "' . $existingName->name . '" already exists. Please use a different name.']
|
||||
]
|
||||
], 422);
|
||||
}
|
||||
|
||||
$data['slug'] = $slug;
|
||||
$category = MusicCategory::create($data);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Category created successfully',
|
||||
'category' => $category->load('image')
|
||||
], 201);
|
||||
|
||||
} catch (\Illuminate\Database\QueryException $e) {
|
||||
// Handle database duplicate entry error (if unique constraint exists)
|
||||
if ($e->errorInfo[1] == 1062) { // MySQL duplicate entry error code
|
||||
return response()->json([
|
||||
'message' => 'A category with this name already exists',
|
||||
'errors' => [
|
||||
'name' => ['The category name must be unique.']
|
||||
]
|
||||
], 422);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => 'An error occurred while creating the category',
|
||||
'error' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
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,91 +3,295 @@
|
||||
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
|
||||
{
|
||||
// ✅ 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',
|
||||
'image_id' => 'nullable|exists:images,id',
|
||||
]);
|
||||
|
||||
$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',
|
||||
'image_id' => $data['image_id'] ?? null,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Music uploaded successfully',
|
||||
'music' => $music->load('image'),
|
||||
'url' => asset('storage/' . $path),
|
||||
]);
|
||||
}
|
||||
|
||||
// ✅ Get all public + user private music
|
||||
public function all()
|
||||
// Add this new method to your MusicController
|
||||
public function getAllMusic()
|
||||
{
|
||||
$userId = auth()->id();
|
||||
|
||||
$music = Music::where('type', 'public')
|
||||
->orWhere(function($query) use ($userId) {
|
||||
$query->where('type', 'private')->where('user_id', $userId);
|
||||
})
|
||||
->get();
|
||||
|
||||
$music = Music::with('image') // eager load image
|
||||
$music = Music::with(['image', 'playlist'])
|
||||
->where('type', 'public')
|
||||
->orWhere(function($query) use ($userId) {
|
||||
$query->where('type', 'private')->where('user_id', $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);
|
||||
}
|
||||
|
||||
// ✅ Update music
|
||||
public function update(Request $request, $id)
|
||||
public function index()
|
||||
{
|
||||
$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',
|
||||
'image_id' => 'nullable|exists:images,id',
|
||||
]);
|
||||
|
||||
if ($request->hasFile('file')) {
|
||||
Storage::disk('public')->delete($music->file_path);
|
||||
$path = $request->file('file')->store('music', 'public');
|
||||
$music->file_path = $path;
|
||||
}
|
||||
|
||||
$music->fill($data);
|
||||
$music->save();
|
||||
$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 response()->json([
|
||||
'message' => 'Music updated successfully',
|
||||
'music' => $music->load('image'),
|
||||
'url' => asset('storage/' . $music->file_path),
|
||||
'data' => $music,
|
||||
'total' => $music->count()
|
||||
]);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
try {
|
||||
$data = $request->validate([
|
||||
'title' => 'required|string|max:255',
|
||||
'artist' => 'nullable|string|max:255',
|
||||
'file' => 'required|mimes:mp3,wav,ogg,flac|max:20971520',
|
||||
'type' => 'nullable|in:public,private',
|
||||
'image_id' => 'nullable|exists:images,id',
|
||||
'playlist_id' => 'nullable|exists:music_playlists,id',
|
||||
'duration' => 'nullable|integer|min:1', // validates mm:ss or hh:mm:ss
|
||||
]);
|
||||
|
||||
// Handle file upload
|
||||
if (!$request->hasFile('file')) {
|
||||
return response()->json([
|
||||
'message' => 'No file was uploaded'
|
||||
], 400);
|
||||
}
|
||||
|
||||
$file = $request->file('file');
|
||||
|
||||
// Validate file size
|
||||
if ($file->getSize() > 20971520) {
|
||||
return response()->json([
|
||||
'message' => 'File size exceeds 10MB limit'
|
||||
], 422);
|
||||
}
|
||||
|
||||
$path = $file->store('music', 'public');
|
||||
|
||||
if (!$path) {
|
||||
return response()->json([
|
||||
'message' => 'Failed to store the file'
|
||||
], 500);
|
||||
}
|
||||
|
||||
$music = Music::create([
|
||||
'user_id' => auth()->id(),
|
||||
'title' => $data['title'],
|
||||
'artist' => $data['artist'] ?? null,
|
||||
'file_path' => $path,
|
||||
'type' => $data['type'] ?? 'private',
|
||||
'image_id' => $data['image_id'] ?? null,
|
||||
'playlist_id' => $data['playlist_id'] ?? null,
|
||||
'duration' => $data['duration'] ?? null, // Store as string
|
||||
'order' => $this->getNextOrderInPlaylist($data['playlist_id'] ?? null),
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Music uploaded successfully',
|
||||
'music' => $music->load(['image', 'playlist', 'tags']),
|
||||
'url' => asset('storage/' . $path),
|
||||
], 201);
|
||||
|
||||
} catch (\Illuminate\Validation\ValidationException $e) {
|
||||
return response()->json([
|
||||
'message' => 'Validation failed',
|
||||
'errors' => $e->errors()
|
||||
], 422);
|
||||
} catch (\Exception $e) {
|
||||
return response()->json([
|
||||
'message' => 'An error occurred while uploading the music',
|
||||
'error' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
// Helper method to get next order number in playlist
|
||||
private function getNextOrderInPlaylist($playlistId)
|
||||
{
|
||||
if (!$playlistId) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$maxOrder = Music::where('playlist_id', $playlistId)->max('order');
|
||||
return ($maxOrder ?? -1) + 1;
|
||||
}
|
||||
|
||||
// ✅ Update music with string duration
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
try {
|
||||
$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,flac|max:20971520',
|
||||
'image_id' => 'nullable|exists:images,id',
|
||||
'playlist_id' => 'nullable|exists:music_playlists,id',
|
||||
'duration' => 'nullable|integer|min:1',
|
||||
'order' => 'nullable|integer',
|
||||
'is_active' => 'nullable|boolean',
|
||||
]);
|
||||
|
||||
if ($request->hasFile('file')) {
|
||||
// Delete old file
|
||||
Storage::disk('public')->delete($music->file_path);
|
||||
$path = $request->file('file')->store('music', 'public');
|
||||
$music->file_path = $path;
|
||||
}
|
||||
|
||||
// Update only provided fields
|
||||
$music->fill($data);
|
||||
$music->save();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Music updated successfully',
|
||||
'music' => $music->load(['image', 'playlist', 'tags']),
|
||||
'url' => asset('storage/' . $music->file_path),
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return response()->json([
|
||||
'message' => 'An error occurred while updating the music',
|
||||
'error' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ✅ Get all public + user private music
|
||||
public function show($id)
|
||||
{
|
||||
$userId = auth()->id();
|
||||
|
||||
$music = Music::with([
|
||||
'image',
|
||||
'playlist',
|
||||
'tags',
|
||||
'comments' => function($query) {
|
||||
$query->with('user')->latest()->limit(10);
|
||||
},
|
||||
'ratings'
|
||||
])
|
||||
->where(function($query) use ($userId) {
|
||||
$query->where('type', 'public')
|
||||
->orWhere(function($q) use ($userId) {
|
||||
$q->where('type', 'private')
|
||||
->where('user_id', $userId);
|
||||
});
|
||||
})
|
||||
->findOrFail($id);
|
||||
|
||||
// Get user's specific comment
|
||||
$userComment = $music->userComment();
|
||||
|
||||
// Get paginated comments for the response
|
||||
$comments = $music->comments()
|
||||
->with('user')
|
||||
->latest()
|
||||
->paginate(15);
|
||||
|
||||
return response()->json([
|
||||
'music' => $music,
|
||||
'statistics' => [
|
||||
'average_rating' => $music->average_rating,
|
||||
'total_ratings' => $music->ratings_count,
|
||||
'total_comments' => $music->comments_count,
|
||||
'rating_distribution' => $music->rating_distribution,
|
||||
],
|
||||
'user_interaction' => [
|
||||
'has_rated' => $music->has_user_rated,
|
||||
'user_rating' => $music->user_rating,
|
||||
'has_commented' => $music->has_user_commented,
|
||||
'user_comment' => $music->user_comment,
|
||||
'user_comment_id' => $music->user_comment_id,
|
||||
],
|
||||
'comments' => $comments,
|
||||
]);
|
||||
}
|
||||
|
||||
// ✅ Delete music
|
||||
public function destroy($id)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
<?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', 'subcategory', 'image']);
|
||||
|
||||
if ($request->has('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();
|
||||
|
||||
return response()->json($playlists);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'category_id' => 'nullable|exists:music_categories,id',
|
||||
'subcategory_id' => 'nullable|exists:music_subcategories,id',
|
||||
'name' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'image_id' => 'nullable|exists:images,id',
|
||||
'order' => 'nullable|integer',
|
||||
'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']);
|
||||
|
||||
$playlist = MusicPlaylist::create($data);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Playlist created successfully',
|
||||
'playlist' => $playlist->load(['category', 'subcategory', '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']);
|
||||
}
|
||||
}
|
||||
@@ -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,151 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class RatingController extends Controller
|
||||
{
|
||||
/**
|
||||
* Add or update rating for any model
|
||||
*
|
||||
* @param Request $request
|
||||
* @param string $type (music, media, etc.)
|
||||
* @param int $id
|
||||
*/
|
||||
public function rate(Request $request, $type, $id)
|
||||
{
|
||||
$request->validate([
|
||||
'stars' => 'required|integer|min:1|max:5',
|
||||
]);
|
||||
|
||||
$model = $this->getModel($type, $id);
|
||||
$this->checkAccess($model);
|
||||
|
||||
$rating = $model->ratings()->updateOrCreate(
|
||||
['user_id' => auth()->id()],
|
||||
['stars' => $request->stars]
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Rating submitted successfully',
|
||||
'rating' => $rating,
|
||||
'average_rating' => $model->fresh()->average_rating,
|
||||
'user_rating' => $rating->stars,
|
||||
'total_ratings' => $model->fresh()->ratings_count,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user's rating for a model
|
||||
*/
|
||||
public function getUserRating($type, $id)
|
||||
{
|
||||
$model = $this->getModel($type, $id);
|
||||
|
||||
$rating = $model->ratings()
|
||||
->where('user_id', auth()->id())
|
||||
->first();
|
||||
|
||||
return response()->json([
|
||||
'rating' => $rating ? $rating->stars : null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete user's rating
|
||||
*/
|
||||
public function deleteRating($type, $id)
|
||||
{
|
||||
$model = $this->getModel($type, $id);
|
||||
|
||||
$deleted = $model->ratings()
|
||||
->where('user_id', auth()->id())
|
||||
->delete();
|
||||
|
||||
return response()->json([
|
||||
'message' => $deleted ? 'Rating deleted successfully' : 'No rating found',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all ratings for a model
|
||||
*/
|
||||
public function getRatings($type, $id)
|
||||
{
|
||||
$model = $this->getModel($type, $id);
|
||||
$this->checkAccess($model);
|
||||
|
||||
$ratings = $model->ratings()
|
||||
->with('user')
|
||||
->latest()
|
||||
->paginate(20);
|
||||
|
||||
return response()->json([
|
||||
'average' => $model->average_rating,
|
||||
'total' => $model->ratings_count,
|
||||
'ratings' => $ratings,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get top rated items
|
||||
*/
|
||||
public function topRated($type)
|
||||
{
|
||||
$modelClass = $this->getModelClass($type);
|
||||
$userId = auth()->id();
|
||||
|
||||
$items = $modelClass::with(['image', 'user'])
|
||||
->withAvg('ratings', 'stars')
|
||||
->where(function($query) use ($modelClass, $userId) {
|
||||
if (method_exists($modelClass, 'isAccessible')) {
|
||||
// Use model-specific access logic
|
||||
} elseif (property_exists($modelClass, 'type')) {
|
||||
$query->where('type', 'public')
|
||||
->orWhere(function($q) use ($userId) {
|
||||
$q->where('type', 'private')
|
||||
->where('user_id', $userId);
|
||||
});
|
||||
}
|
||||
})
|
||||
->having('ratings_avg_stars', '>', 0)
|
||||
->orderBy('ratings_avg_stars', 'desc')
|
||||
->limit(10)
|
||||
->get();
|
||||
|
||||
return response()->json($items);
|
||||
}
|
||||
|
||||
private function getModel($type, $id)
|
||||
{
|
||||
$modelClass = $this->getModelClass($type);
|
||||
$model = $modelClass::findOrFail($id);
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
private function getModelClass($type)
|
||||
{
|
||||
$models = [
|
||||
'music' => \App\Models\Music::class,
|
||||
'media' => \App\Models\Media::class,
|
||||
];
|
||||
|
||||
if (!isset($models[$type])) {
|
||||
abort(404, 'Invalid model type');
|
||||
}
|
||||
|
||||
return $models[$type];
|
||||
}
|
||||
|
||||
private function checkAccess($model)
|
||||
{
|
||||
// Check if model has 'type' property (public/private)
|
||||
if (property_exists($model, 'type') && $model->type === 'private') {
|
||||
if (auth()->id() !== $model->user_id) {
|
||||
abort(403, 'You do not have access to this item');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
@@ -486,9 +486,22 @@ public function profile(Request $request)
|
||||
if ($response->successful()) {
|
||||
$user = $this->syncUserFromStatus($response->json(), $user);
|
||||
}
|
||||
|
||||
$remindersResponse = Http::acceptJson()
|
||||
->withToken($data['token'])
|
||||
->get('https://api.approagency.ir/api/reminders', [
|
||||
'type' => 'reminder',
|
||||
'package_name' => $data['package_name'],
|
||||
]);
|
||||
|
||||
$reminders = $remindersResponse->successful()
|
||||
? $remindersResponse->json()
|
||||
: [];
|
||||
|
||||
$user->load(['breathingSessions.template']);
|
||||
return response()->json([
|
||||
'user' => $user
|
||||
'user' => $user,
|
||||
'reminders' => $reminders,
|
||||
]);
|
||||
}
|
||||
public function status(Request $request)
|
||||
@@ -616,7 +629,7 @@ public function googleLogin(Request $request)
|
||||
|
||||
public function leaderBoard(){
|
||||
$users = User::with(['breathingSessions.template'])->where('xp', '>', 0)
|
||||
->orderBy('xp', 'desc')
|
||||
->orderBy('xp', 'desc')->limit(20)
|
||||
->get();
|
||||
|
||||
return $users;
|
||||
|
||||
@@ -3,16 +3,18 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Traits\HasSaves;
|
||||
|
||||
class BreathingTemplate extends Model
|
||||
{
|
||||
use HasSaves;
|
||||
protected $fillable = ['user_id', 'name', 'inhale', 'exhale', 'breath_hold' , 'duration' , 'description','image_id' , 'source'];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
protected $appends = ['image_url'];
|
||||
protected $appends = ['image_url', 'is_saved','saved_count'];
|
||||
|
||||
public function getImageUrlAttribute()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Comment extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'commentable_id',
|
||||
'commentable_type',
|
||||
'content',
|
||||
];
|
||||
protected $with = ['user'];
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function commentable()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
// For backward compatibility with Media
|
||||
public function media()
|
||||
{
|
||||
return $this->belongsTo(Media::class, 'commentable_id')->where('commentable_type', Media::class);
|
||||
}
|
||||
|
||||
// For Music
|
||||
public function music()
|
||||
{
|
||||
return $this->belongsTo(Music::class, 'commentable_id')->where('commentable_type', Music::class);
|
||||
}
|
||||
|
||||
// Check if comment belongs to current user
|
||||
public function getIsOwnerAttribute()
|
||||
{
|
||||
return auth()->check() && $this->user_id === auth()->id();
|
||||
}
|
||||
|
||||
// Add timestamps formatted
|
||||
public function getFormattedCreatedAtAttribute()
|
||||
{
|
||||
return $this->created_at->diffForHumans();
|
||||
}
|
||||
|
||||
protected $appends = ['is_owner', 'formatted_created_at'];
|
||||
}
|
||||
+17
-2
@@ -3,9 +3,13 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Traits\HasRatings;
|
||||
use App\Traits\HasComments;
|
||||
use App\Traits\HasSaves;
|
||||
|
||||
class Media extends Model
|
||||
{
|
||||
use HasRatings, HasComments,HasSaves;
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'image_id',
|
||||
@@ -17,8 +21,20 @@ class Media extends Model
|
||||
'external_url',
|
||||
'duration',
|
||||
'visibility',
|
||||
'is_premium'
|
||||
];
|
||||
protected $appends = [
|
||||
'average_rating',
|
||||
'user_rating',
|
||||
'ratings_count',
|
||||
'comments_count',
|
||||
'has_user_commented',
|
||||
'user_comment',
|
||||
'user_comment_id',
|
||||
'has_user_rated',
|
||||
'is_saved',
|
||||
'saved_count'
|
||||
];
|
||||
|
||||
public function image()
|
||||
{
|
||||
return $this->belongsTo(Image::class);
|
||||
@@ -64,5 +80,4 @@ public function getIsSavedAttribute()
|
||||
->where('user_id', auth()->id())
|
||||
->exists();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+44
-11
@@ -4,26 +4,59 @@
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo; // ← Add this
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany; // ← Add this
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany; // ← Add this
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany; // ← Add this
|
||||
use App\Traits\HasRatings;
|
||||
use App\Traits\HasComments;
|
||||
use App\Traits\HasSaves;
|
||||
|
||||
class Music extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
{
|
||||
use HasFactory, HasRatings, HasComments , HasSaves;
|
||||
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' => 'integer',
|
||||
'order' => 'integer',
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
protected $attributes = [
|
||||
'type' => 'public', // Default value
|
||||
];
|
||||
// Relation to user (optional)
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
protected $appends = ['url', 'image_url'];
|
||||
// Accessor for full URL
|
||||
public function playlist(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MusicPlaylist::class, 'playlist_id');
|
||||
}
|
||||
public function tags(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Tag::class, 'music_tags');
|
||||
}
|
||||
|
||||
protected $appends = ['url',
|
||||
'image_url' ,
|
||||
'average_rating',
|
||||
'user_rating',
|
||||
'comments_count' ,
|
||||
'has_user_commented',
|
||||
'user_comment',
|
||||
'user_comment_id',
|
||||
'has_user_rated' ,
|
||||
'is_saved',
|
||||
'saved_count'
|
||||
];
|
||||
|
||||
public function getUrlAttribute()
|
||||
{
|
||||
return asset('storage/' . $this->file_path);
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo; // ← Add this
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany; // ← Add this
|
||||
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 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
|
||||
{
|
||||
return $this->belongsTo(Image::class);
|
||||
}
|
||||
|
||||
public function getActivePlaylistsAttribute()
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo; // ← Add this
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany; // ← Add this
|
||||
class MusicPlaylist extends Model
|
||||
{
|
||||
protected $table = 'music_playlists';
|
||||
|
||||
protected $fillable = [
|
||||
'category_id', 'subcategory_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 subcategory(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MusicSubcategory::class, 'subcategory_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,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,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Rating extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'rateable_id',
|
||||
'rateable_type',
|
||||
'stars',
|
||||
];
|
||||
|
||||
public function rateable()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
// For backward compatibility with Media
|
||||
public function media()
|
||||
{
|
||||
return $this->belongsTo(Media::class, 'rateable_id')->where('rateable_type', Media::class);
|
||||
}
|
||||
|
||||
// For Music
|
||||
public function music()
|
||||
{
|
||||
return $this->belongsTo(Music::class, 'rateable_id')->where('rateable_type', Music::class);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -3,7 +3,8 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo; // ← Add this
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany; // ← Add this
|
||||
class Tag extends Model
|
||||
{
|
||||
protected $fillable = ['name'];
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
// app/Traits/HasComments.php
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Models\Comment;
|
||||
|
||||
trait HasComments
|
||||
{
|
||||
public function comments()
|
||||
{
|
||||
return $this->morphMany(Comment::class, 'commentable');
|
||||
}
|
||||
// Get user's specific comment
|
||||
public function userComment()
|
||||
{
|
||||
if (!auth()->check()) return null;
|
||||
|
||||
return $this->comments()
|
||||
->where('user_id', auth()->id())
|
||||
->first();
|
||||
}
|
||||
|
||||
// Check if user has commented
|
||||
public function getHasUserCommentedAttribute()
|
||||
{
|
||||
return !is_null($this->userComment());
|
||||
}
|
||||
|
||||
// Get user's comment content
|
||||
public function getUserCommentAttribute()
|
||||
{
|
||||
$comment = $this->userComment();
|
||||
return $comment ? $comment->content : null;
|
||||
}
|
||||
|
||||
// Get user's comment id
|
||||
public function getUserCommentIdAttribute()
|
||||
{
|
||||
$comment = $this->userComment();
|
||||
return $comment ? $comment->id : null;
|
||||
}
|
||||
|
||||
// Get latest comments with user info
|
||||
public function getLatestCommentsAttribute()
|
||||
{
|
||||
return $this->comments()
|
||||
->with('user')
|
||||
->latest()
|
||||
->limit(10)
|
||||
->get();
|
||||
}
|
||||
|
||||
// Get paginated comments
|
||||
public function getPaginatedComments($perPage = 15)
|
||||
{
|
||||
return $this->comments()
|
||||
->with('user')
|
||||
->latest()
|
||||
->paginate($perPage);
|
||||
}
|
||||
|
||||
|
||||
public function getCommentsCountAttribute()
|
||||
{
|
||||
return $this->comments()->count();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
// app/Traits/HasRatings.php
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Models\Rating;
|
||||
|
||||
trait HasRatings
|
||||
{
|
||||
public function ratings()
|
||||
{
|
||||
return $this->morphMany(Rating::class, 'rateable');
|
||||
}
|
||||
|
||||
public function getAverageRatingAttribute()
|
||||
{
|
||||
return round($this->ratings()->avg('stars'), 1);
|
||||
}
|
||||
|
||||
public function getUserRatingAttribute()
|
||||
{
|
||||
if (!auth()->check()) return null;
|
||||
|
||||
return $this->ratings()
|
||||
->where('user_id', auth()->id())
|
||||
->value('stars');
|
||||
}
|
||||
// Get user's rating object
|
||||
public function userRating()
|
||||
{
|
||||
if (!auth()->check()) return null;
|
||||
|
||||
return $this->ratings()
|
||||
->where('user_id', auth()->id())
|
||||
->first();
|
||||
}
|
||||
|
||||
// Check if user has rated
|
||||
public function getHasUserRatedAttribute()
|
||||
{
|
||||
return !is_null($this->userRating());
|
||||
}
|
||||
|
||||
public function getRatingsCountAttribute()
|
||||
{
|
||||
return $this->ratings()->count();
|
||||
}
|
||||
|
||||
// Get rating distribution
|
||||
public function getRatingDistributionAttribute()
|
||||
{
|
||||
$distribution = [];
|
||||
for ($i = 1; $i <= 5; $i++) {
|
||||
$distribution[$i] = $this->ratings()->where('stars', $i)->count();
|
||||
}
|
||||
return $distribution;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Regular → Executable
@@ -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('media', function (Blueprint $table) {
|
||||
$table->boolean('is_premium')->default(false);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('media', function (Blueprint $table) {
|
||||
$table->dropColumn('is_premium');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -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('ratings', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('media_id')->constrained()->cascadeOnDelete();
|
||||
$table->tinyInteger('stars'); // 1-5
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['user_id', 'media_id']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('ratings');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
<?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('comments', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('media_id')->constrained()->cascadeOnDelete();
|
||||
$table->text('content');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('comments');
|
||||
}
|
||||
};
|
||||
@@ -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');
|
||||
}
|
||||
};
|
||||
@@ -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('comments', function (Blueprint $table) {
|
||||
$table->dropForeign(['media_id']);
|
||||
$table->dropColumn('media_id');
|
||||
|
||||
// Add polymorphic columns
|
||||
$table->morphs('commentable');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('comments', function (Blueprint $table) {
|
||||
$table->dropMorphs('commentable');
|
||||
$table->foreignId('media_id')->constrained()->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,65 @@
|
||||
<?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::dropIfExists('ratings');
|
||||
|
||||
Schema::create('ratings', function (Blueprint $table) {
|
||||
$table->id();
|
||||
|
||||
$table->foreignId('user_id')
|
||||
->constrained()
|
||||
->cascadeOnDelete();
|
||||
|
||||
// polymorphic relation
|
||||
$table->morphs('rateable');
|
||||
|
||||
$table->tinyInteger('stars'); // 1-5
|
||||
|
||||
$table->timestamps();
|
||||
|
||||
// one rating per user per item
|
||||
$table->unique([
|
||||
'user_id',
|
||||
'rateable_id',
|
||||
'rateable_type'
|
||||
], 'ratings_user_rateable_unique');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('ratings');
|
||||
|
||||
// optional: rollback old structure (if you still need it)
|
||||
Schema::create('ratings', function (Blueprint $table) {
|
||||
$table->id();
|
||||
|
||||
$table->foreignId('user_id')
|
||||
->constrained()
|
||||
->cascadeOnDelete();
|
||||
|
||||
$table->foreignId('media_id')
|
||||
->constrained()
|
||||
->cascadeOnDelete();
|
||||
|
||||
$table->tinyInteger('stars');
|
||||
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['user_id', 'media_id']);
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -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');
|
||||
}
|
||||
};
|
||||
@@ -1,3 +1,18 @@
|
||||
|
||||
<IfModule mod_php7.c>
|
||||
php_value post_max_size 128M
|
||||
php_value upload_max_filesize 128M
|
||||
php_value max_execution_time 300
|
||||
php_value max_input_time 300
|
||||
</IfModule>
|
||||
|
||||
<IfModule mod_php8.c>
|
||||
php_value post_max_size 128M
|
||||
php_value upload_max_filesize 128M
|
||||
php_value max_execution_time 300
|
||||
php_value max_input_time 300
|
||||
</IfModule>
|
||||
|
||||
<IfModule mod_rewrite.c>
|
||||
<IfModule mod_negotiation.c>
|
||||
Options -MultiViews -Indexes
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
<?php
|
||||
phpinfo();
|
||||
?>
|
||||
+73
-6
@@ -15,6 +15,12 @@
|
||||
use App\Http\Controllers\ImageController;
|
||||
use App\Http\Controllers\MusicController;
|
||||
use App\Http\Controllers\MediaController;
|
||||
use App\Http\Controllers\MusicCategoryController;
|
||||
use App\Http\Controllers\MusicPlaylistController;
|
||||
use App\Http\Controllers\RatingController;
|
||||
use App\Http\Controllers\CommentController;
|
||||
use App\Http\Controllers\MusicSubcategoryController;
|
||||
use App\Http\Controllers\SaveController;
|
||||
|
||||
Route::get('/test-hash', function() {
|
||||
$plain = 'amnk1380';
|
||||
@@ -120,10 +126,10 @@
|
||||
|
||||
/// 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']);
|
||||
// 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']);
|
||||
|
||||
|
||||
Route::get('/slider', [SliderController::class, 'index']); // list all sliders
|
||||
@@ -135,13 +141,74 @@
|
||||
|
||||
|
||||
///media
|
||||
Route::get('/media/filters', [MediaController::class, 'filters']);
|
||||
Route::post('/media', [MediaController::class, 'store']);
|
||||
Route::get('/media', [MediaController::class, 'index']);
|
||||
Route::post('/media/{id}', [MediaController::class, 'update']);
|
||||
Route::get('/media/saved', [MediaController::class, 'saved']);
|
||||
Route::delete('/media/{id}', [MediaController::class, 'destroy']);
|
||||
Route::get('/media/{id}', [MediaController::class, 'show']);
|
||||
Route::post('/media/{id}/save', [MediaController::class, 'saveMedia']);
|
||||
Route::get('/media/saved', [MediaController::class, 'saved']);
|
||||
Route::post('/media/{id}/save', [MediaController::class, 'toggleSaveMedia']);
|
||||
Route::post('/media/{id}/rate', [MediaController::class, 'rate']);
|
||||
Route::post('/media/{id}/comment', [MediaController::class, 'storeComment']);
|
||||
Route::get('/media/{id}/comments', [MediaController::class, 'getComments']);
|
||||
Route::post('/media/{id}/feedback', [MediaController::class, 'submitFeedback']);
|
||||
|
||||
//add note to media
|
||||
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']);
|
||||
|
||||
// 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
|
||||
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);
|
||||
|
||||
|
||||
|
||||
// Generic Rating Routes (works for both music and media)
|
||||
Route::prefix('ratings')->group(function () {
|
||||
Route::post('{type}/{id}', [RatingController::class, 'rate']);
|
||||
Route::get('{type}/{id}/user', [RatingController::class, 'getUserRating']);
|
||||
Route::delete('{type}/{id}', [RatingController::class, 'deleteRating']);
|
||||
Route::get('{type}/{id}', [RatingController::class, 'getRatings']);
|
||||
Route::get('top/{type}', [RatingController::class, 'topRated']);
|
||||
});
|
||||
|
||||
// Generic Comment Routes (works for both music and media)
|
||||
Route::prefix('comments')->group(function () {
|
||||
Route::post('{type}/{id}', [CommentController::class, 'addComment']);
|
||||
Route::get('{type}/{id}', [CommentController::class, 'getComments']);
|
||||
Route::put('{type}/{id}/{commentId}', [CommentController::class, 'updateComment']);
|
||||
Route::delete('{type}/{id}/{commentId}', [CommentController::class, 'deleteComment']);
|
||||
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']);
|
||||
});
|
||||
});
|
||||
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Reference in New Issue
Block a user