import {NachklangFeedbackDB} from '../Feedback.db'; export const addSong = async (eventId: number, title: string, composer: string | null): Promise => { let conn = await NachklangFeedbackDB.getConnection(); try { await conn.beginTransaction(); const posRows = await conn.query('SELECT COALESCE(MAX(position), -1) + 1 as next_position FROM songs WHERE event_id = ?', [eventId]); const position = posRows[0].next_position; const res = await conn.query( 'INSERT INTO songs (event_id, title, composer, position) VALUES (?,?,?,?) RETURNING song_id', [eventId, title, composer, position] ); await conn.commit(); return res[0].song_id; } catch (err) { await conn.rollback(); throw err; } finally { await conn.end(); } }; export const updateSong = async (songId: number, title: string, composer: string | null): Promise => { let conn = await NachklangFeedbackDB.getConnection(); try { await conn.beginTransaction(); const res = await conn.query('UPDATE songs SET title = ?, composer = ? WHERE song_id = ?', [title, composer, songId]); await conn.commit(); return res.affectedRows > 0; } catch (err) { await conn.rollback(); throw err; } finally { await conn.end(); } }; /** * Removes a song. submission_answers rows referencing it keep their * song_title_snapshot (song_id is set to NULL via ON DELETE SET NULL) - * past answers still say what song was rated, even after the song is gone. */ export const deleteSong = async (songId: number): Promise => { let conn = await NachklangFeedbackDB.getConnection(); try { await conn.beginTransaction(); const res = await conn.query('DELETE FROM songs WHERE song_id = ?', [songId]); await conn.commit(); return res.affectedRows > 0; } catch (err) { await conn.rollback(); throw err; } finally { await conn.end(); } };