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 접근.""" 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): """이 업장의 **가장 최근에 완성된** 곡 하나.""" 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), )