feat: add comment for playlist
This commit is contained in:
@@ -127,10 +127,11 @@ private function getModelClass($type)
|
||||
$models = [
|
||||
'music' => \App\Models\Music::class,
|
||||
'media' => \App\Models\Media::class,
|
||||
'playlist' => \App\Models\MusicPlaylist::class,
|
||||
];
|
||||
|
||||
if (!isset($models[$type])) {
|
||||
abort(404, 'Invalid model type');
|
||||
abort(404, 'Invalid model type. Supported types: music, media, playlist');
|
||||
}
|
||||
|
||||
return $models[$type];
|
||||
|
||||
@@ -55,19 +55,41 @@ public function store(Request $request)
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$playlist = MusicPlaylist::with([
|
||||
'category',
|
||||
'image',
|
||||
'musics' => function($q) {
|
||||
$q->where('is_active', true)
|
||||
->with(['image', 'tags'])
|
||||
->orderBy('order');
|
||||
}
|
||||
])->findOrFail($id);
|
||||
{
|
||||
$playlist = MusicPlaylist::with([
|
||||
'category',
|
||||
'subcategory',
|
||||
'image',
|
||||
'musics' => function($q) {
|
||||
$q->where('is_active', true)
|
||||
->with(['image', 'tags'])
|
||||
->orderBy('order');
|
||||
},
|
||||
'comments' => function($q) { // Add comments relationship
|
||||
$q->with('user')->latest()->limit(10);
|
||||
}
|
||||
])->findOrFail($id);
|
||||
$userComment = $playlist->userComment();
|
||||
$comments = $playlist->comments()
|
||||
->with('user')
|
||||
->latest()
|
||||
->paginate(15);
|
||||
|
||||
return response()->json($playlist);
|
||||
}
|
||||
return response()->json([
|
||||
'playlist' => $playlist,
|
||||
'statistics' => [
|
||||
'total_musics' => $playlist->musics->count(),
|
||||
'total_duration' => $playlist->total_duration,
|
||||
'total_comments' => $playlist->comments_count,
|
||||
],
|
||||
'user_interaction' => [
|
||||
'has_commented' => $playlist->has_user_commented,
|
||||
'user_comment' => $playlist->user_comment,
|
||||
'user_comment_id' => $playlist->user_comment_id,
|
||||
],
|
||||
'comments' => $comments,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
|
||||
@@ -5,8 +5,10 @@
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo; // ← Add this
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany; // ← Add this
|
||||
use App\Traits\HasComments; // Add this
|
||||
class MusicPlaylist extends Model
|
||||
{
|
||||
use HasComments;
|
||||
protected $table = 'music_playlists';
|
||||
|
||||
protected $fillable = [
|
||||
@@ -17,7 +19,12 @@ class MusicPlaylist extends Model
|
||||
'is_active' => 'boolean',
|
||||
'order' => 'integer',
|
||||
];
|
||||
|
||||
protected $appends = [
|
||||
'comments_count',
|
||||
'has_user_commented',
|
||||
'user_comment',
|
||||
'user_comment_id'
|
||||
];
|
||||
public function category(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MusicCategory::class, 'category_id');
|
||||
|
||||
Reference in New Issue
Block a user