202508281602
調整 語法收到 Model
This commit is contained in:
parent
46caedfc10
commit
6cff9886d4
@ -59,7 +59,7 @@ class RoomSongController extends Controller
|
|||||||
$roomSession = $this->getRoomSession($request->api_token) ;
|
$roomSession = $this->getRoomSession($request->api_token) ;
|
||||||
|
|
||||||
// 找這個 session 的最大 order_number,下一首加 1
|
// 找這個 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;
|
$orderNumber = $lastOrder ? $lastOrder + 1 : 1;
|
||||||
|
|
||||||
// 取得歌曲名稱
|
// 取得歌曲名稱
|
||||||
@ -78,7 +78,7 @@ class RoomSongController extends Controller
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
// 檢查這首歌在此 session 是否第一次點
|
// 檢查這首歌在此 session 是否第一次點
|
||||||
$countInSession = OrderedSong::where('room_session_id', $roomSession->id)
|
$countInSession = OrderedSong::forSession($roomSession->id)
|
||||||
->where('song_id', $request->song_id)
|
->where('song_id', $request->song_id)
|
||||||
->count();
|
->count();
|
||||||
|
|
||||||
@ -139,23 +139,10 @@ class RoomSongController extends Controller
|
|||||||
{
|
{
|
||||||
$roomSession = $this->getRoomSession($request->api_token) ;
|
$roomSession = $this->getRoomSession($request->api_token) ;
|
||||||
// 已結束 (finished + canceled)
|
// 已結束 (finished + canceled)
|
||||||
$played = OrderedSong::where('room_session_id', $roomSession->id)
|
$played = OrderedSong::forSession($roomSession->id)->finished()->get();
|
||||||
->whereIn('status', ['Played', 'Skipped', 'NoFile'])
|
$playing = OrderedSong::forSession($roomSession->id)->playing()->get();
|
||||||
->orderByDesc('finished_at')
|
|
||||||
->get();
|
|
||||||
$playing = OrderedSong::where('room_session_id', $roomSession->id)
|
|
||||||
->whereIn('status', ['Playing'])
|
|
||||||
->get();
|
|
||||||
|
|
||||||
// 正在播 + 插播 + 待播
|
// 正在播 + 插播 + 待播
|
||||||
$not_played = OrderedSong::where('room_session_id', $roomSession->id)
|
$not_played = OrderedSong::forSession($roomSession->id)->nextSong()->get();
|
||||||
->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();
|
|
||||||
|
|
||||||
return ApiResponse::success([
|
return ApiResponse::success([
|
||||||
'played' => $played,
|
'played' => $played,
|
||||||
@ -167,13 +154,7 @@ class RoomSongController extends Controller
|
|||||||
private function nextSongName(RoomSession $roomSession)
|
private function nextSongName(RoomSession $roomSession)
|
||||||
{
|
{
|
||||||
// 找下首
|
// 找下首
|
||||||
$next = OrderedSong::where('room_session_id', $roomSession->id)
|
$next = OrderedSong::forSession($roomSession->id)->with('song')->nextSong()->first();
|
||||||
->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();
|
|
||||||
return $next?->song?->name;
|
return $next?->song?->name;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ namespace App\Models;
|
|||||||
|
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use App\Enums\OrderedSongStatus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @OA\Schema(
|
* @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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user