feat: user rating and comment
This commit is contained in:
@@ -320,8 +320,17 @@ public function filters(Request $request)
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$media = Media::with(['image', 'category', 'myNote', 'tags'])
|
||||
{
|
||||
$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')
|
||||
@@ -329,6 +338,15 @@ public function show($id)
|
||||
})
|
||||
->firstOrFail();
|
||||
|
||||
// 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,
|
||||
@@ -340,18 +358,28 @@ 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,
|
||||
'is_premium' => $media->is_premium,
|
||||
'comments' => $media->comments,
|
||||
'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,
|
||||
'comments_count' => $media->comments()->count(),
|
||||
'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([
|
||||
|
||||
@@ -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) {
|
||||
@@ -237,7 +245,32 @@ 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
|
||||
|
||||
+15
-1
@@ -12,7 +12,7 @@ class Comment extends Model
|
||||
'commentable_type',
|
||||
'content',
|
||||
];
|
||||
|
||||
protected $with = ['user'];
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
@@ -34,4 +34,18 @@ 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'];
|
||||
}
|
||||
|
||||
@@ -25,7 +25,11 @@ class Media extends Model
|
||||
'average_rating',
|
||||
'user_rating',
|
||||
'ratings_count',
|
||||
'comments_count'
|
||||
'comments_count',
|
||||
'has_user_commented', // Add this
|
||||
'user_comment', // Add this
|
||||
'user_comment_id', // Add this
|
||||
'has_user_rated' // Add this
|
||||
];
|
||||
public function image()
|
||||
{
|
||||
|
||||
@@ -43,7 +43,11 @@ public function tags(): BelongsToMany
|
||||
return $this->belongsToMany(Tag::class, 'music_tags');
|
||||
}
|
||||
|
||||
protected $appends = ['url', 'image_url' , 'average_rating', 'user_rating', 'comments_count'];
|
||||
protected $appends = ['url', 'image_url' , 'average_rating', 'user_rating', 'comments_count' , 'has_user_commented', // Add this
|
||||
'user_comment', // Add this
|
||||
'user_comment_id', // Add this
|
||||
'has_user_rated' // Add this];
|
||||
];
|
||||
|
||||
public function getUrlAttribute()
|
||||
{
|
||||
|
||||
@@ -10,12 +10,56 @@ 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()->latest()->limit(5)->get();
|
||||
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();
|
||||
|
||||
@@ -24,9 +24,34 @@ public function getUserRatingAttribute()
|
||||
->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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user