202508281602

調整 語法收到 Model
This commit is contained in:
allen.yan 2025-08-28 16:06:07 +08:00
parent 46caedfc10
commit 6cff9886d4
2 changed files with 36 additions and 25 deletions

View File

@ -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;
}

View File

@ -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');
}
}