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