import dataclasses from folkugat_web.dal.sql import get_connection from folkugat_web.dal.sql.temes import query as temes_q from folkugat_web.dal.sql.temes import write as temes_w from folkugat_web.model import temes as model def update_title(tema_id: int, title: str) -> model.Tema: with get_connection() as con: tema = temes_q.get_tema_by_id(tema_id=tema_id, con=con) if tema is None: raise ValueError(f"No tune found with tema_id = {tema_id}!") new_tema = dataclasses.replace(tema, title=title) new_tema.compute_ngrams() temes_w.update_tema(tema=new_tema, con=con) return new_tema def update_lyric(tema_id: int, lyric_idx: int, lyric: model.Lyrics) -> model.Tema: with get_connection() as con: tema = temes_q.get_tema_by_id(tema_id=tema_id, con=con) if tema is None: raise ValueError(f"No tune found with tema_id = {tema_id}!") if lyric_idx > len(tema.lyrics): raise ValueError(f'Lyric index out of bounds') tema.lyrics[lyric_idx] = lyric temes_w.update_tema(tema=tema, con=con) return tema def delete_lyric(tema_id: int, lyric_idx: int) -> model.Tema: with get_connection() as con: tema = temes_q.get_tema_by_id(tema_id=tema_id, con=con) if tema is None: raise ValueError(f"No tune found with tema_id = {tema_id}!") if lyric_idx > len(tema.lyrics): raise ValueError(f'Lyric index out of bounds') del tema.lyrics[lyric_idx] temes_w.update_tema(tema=tema, con=con) return tema def add_lyric(tema_id: int) -> model.Tema: with get_connection() as con: tema = temes_q.get_tema_by_id(tema_id=tema_id, con=con) if tema is None: raise ValueError(f"No tune found with tema_id = {tema_id}!") tema.lyrics.append(model.Lyrics( title=tema.title, content="", )) temes_w.update_tema(tema=tema, con=con) return tema