From 6cff9886d45ec646cbee9762d288080f44da3c43 Mon Sep 17 00:00:00 2001 From: "allen.yan" Date: Thu, 28 Aug 2025 16:06:07 +0800 Subject: [PATCH] =?UTF-8?q?202508281602=20=E8=AA=BF=E6=95=B4=20=E8=AA=9E?= =?UTF-8?q?=E6=B3=95=E6=94=B6=E5=88=B0=20Model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/RoomSongController.php | 31 ++++--------------- app/Models/OrderedSong.php | 30 ++++++++++++++++++ 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/app/Http/Controllers/Api/RoomSongController.php b/app/Http/Controllers/Api/RoomSongController.php index c581ffc..41f5fba 100644 --- a/app/Http/Controllers/Api/RoomSongController.php +++ b/app/Http/Controllers/Api/RoomSongController.php @@ -59,7 +59,7 @@ class RoomSongController extends Controller $roomSession = $this->getRoomSession($request->api_token) ; // 找這個 session 的最大 order_number,下一首加 1 - $lastOrder = OrderedSong::where('room_session_id', $roomSession->id)->max('order_number'); + $lastOrder = OrderedSong::forSession($roomSession->id)->max('order_number'); $orderNumber = $lastOrder ? $lastOrder + 1 : 1; // 取得歌曲名稱 @@ -78,7 +78,7 @@ class RoomSongController extends Controller ]); // 檢查這首歌在此 session 是否第一次點 - $countInSession = OrderedSong::where('room_session_id', $roomSession->id) + $countInSession = OrderedSong::forSession($roomSession->id) ->where('song_id', $request->song_id) ->count(); @@ -139,23 +139,10 @@ class RoomSongController extends Controller { $roomSession = $this->getRoomSession($request->api_token) ; // 已結束 (finished + canceled) - $played = OrderedSong::where('room_session_id', $roomSession->id) - ->whereIn('status', ['Played', 'Skipped', 'NoFile']) - ->orderByDesc('finished_at') - ->get(); - $playing = OrderedSong::where('room_session_id', $roomSession->id) - ->whereIn('status', ['Playing']) - ->get(); - + $played = OrderedSong::forSession($roomSession->id)->finished()->get(); + $playing = OrderedSong::forSession($roomSession->id)->playing()->get(); // 正在播 + 插播 + 待播 - $not_played = OrderedSong::where('room_session_id', $roomSession->id) - ->whereIn('status', ['Playing', 'InsertPlayback', 'NotPlayed']) - ->orderByRaw("FIELD(status, 'Playing', 'InsertPlayback', 'NotPlayed')") // playing > InsertPlayback > NotPlayed - ->orderByRaw("CASE - WHEN status = 'InsertPlayback' THEN ordered_at END DESC") // InsertPlayback 越後排越前 - ->orderByRaw("CASE - WHEN status = 'NotPlayed' THEN ordered_at END ASC") // NotPlayed 越後排越後 - ->get(); + $not_played = OrderedSong::forSession($roomSession->id)->nextSong()->get(); return ApiResponse::success([ 'played' => $played, @@ -167,13 +154,7 @@ class RoomSongController extends Controller private function nextSongName(RoomSession $roomSession) { // 找下首 - $next = OrderedSong::where('room_session_id', $roomSession->id) - ->whereIn('status', ['InsertPlayback', 'NotPlayed']) - ->with('song') - ->orderByRaw("FIELD(status, 'InsertPlayback', 'NotPlayed')") - ->orderByRaw("CASE WHEN status = 'InsertPlayback' THEN ordered_at END DESC") - ->orderByRaw("CASE WHEN status = 'NotPlayed' THEN ordered_at END ASC") - ->first(); + $next = OrderedSong::forSession($roomSession->id)->with('song')->nextSong()->first(); return $next?->song?->name; } diff --git a/app/Models/OrderedSong.php b/app/Models/OrderedSong.php index 0c92bca..b0b364e 100644 --- a/app/Models/OrderedSong.php +++ b/app/Models/OrderedSong.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use App\Enums\OrderedSongStatus; /** * @OA\Schema( @@ -86,4 +87,33 @@ class OrderedSong extends Model } ]); } + public function scopeForSession($query, $roomSessionId) + { + return $query->where('room_session_id', $roomSessionId); + } + + public function scopePlaying($query) + { + return $query->where('status', OrderedSongStatus::Playing); + } + + public function scopeNextSong($query) + { + return $query->whereIn('status', [OrderedSongStatus::InsertPlayback, OrderedSongStatus::NotPlayed]) + ->orderByRaw("FIELD(status, ?, ?)", [ + OrderedSongStatus::InsertPlayback->value, + OrderedSongStatus::NotPlayed->value, + ]) + ->orderByRaw("CASE WHEN status=? THEN ordered_at END DESC", [OrderedSongStatus::InsertPlayback->value]) + ->orderByRaw("CASE WHEN status=? THEN ordered_at END ASC", [OrderedSongStatus::NotPlayed->value]); + } + + public function scopeFinished($query) + { + return $query->whereIn('status', [ + OrderedSongStatus::Played, + OrderedSongStatus::Skipped, + OrderedSongStatus::NoFile, + ])->orderByDesc('finished_at'); + } }