from sqlalchemy import and_, select, update from common.database.db_session_manager import DB_SESSION_MNG from common.database.model.models import place_songs from common.enums import SongStatus from common.utils.gtime import GTime class SongCRUD: """place_songs 접근. 발행본이 읽는 것은 `latest_ready` 하나뿐이다.""" async def insert(self, db, row): return await DB_SESSION_MNG.insert(db, row) async def update(self, db, song_id, data: dict): return await DB_SESSION_MNG.add_with_rowcount( db, update(place_songs) .where(place_songs.song_id == song_id, place_songs.deleted == False) # noqa: E712 .values(**data, updated_at=GTime.UTC()), ) async def latest_ready(self, db, place_id): """이 업장의 **가장 최근에 완성된** 곡 하나. ★ READY 만 본다. 발행마다 새 곡을 만들므로 GENERATING 행이 함께 있을 수 있는데, 그걸 집으면 아직 없는 파일을 사이트가 가리킨다. 실패(FAILED)도 마찬가지다 — 새 곡이 실패하면 사이트는 **직전 곡을 그대로 유지**한다(빈 플레이어보다 낫다).""" return await DB_SESSION_MNG.execute( db, select(place_songs) .where(and_( place_songs.place_id == place_id, place_songs.status == SongStatus.READY.value, place_songs.deleted == False, # noqa: E712 )) .order_by(place_songs.created_at.desc()) .limit(1), )