feat: user rating and comment

This commit is contained in:
2026-05-20 15:52:28 +03:30
parent 02448db346
commit d0d48866f0
7 changed files with 171 additions and 19 deletions
+37 -4
View File
@@ -223,11 +223,19 @@ public function update(Request $request, $id)
// ✅ Get all public + user private music
public function show($id)
public function show($id)
{
$userId = auth()->id();
$music = Music::with(['image', 'playlist', 'tags'])
$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) {
@@ -236,8 +244,33 @@ public function show($id)
});
})
->findOrFail($id);
return response()->json($music);
// 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