diff --git a/bun.lockb b/bun.lockb index 499fbaf..8edd602 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/main/helpers/db/connectDB.ts b/main/helpers/db/connectDB.ts index 8bf5780..a81f9db 100644 --- a/main/helpers/db/connectDB.ts +++ b/main/helpers/db/connectDB.ts @@ -244,18 +244,31 @@ export const createPlaylist = async (data: any) => { }; export const deletePlaylist = async (data: { id: number }) => { - await db.transaction(async (tx) => { - // Remove all links in playlistSongs - await tx.delete(playlistSongs).where(eq(playlistSongs.playlistId, data.id)); + try { + sqlite.exec("BEGIN;"); + + // delete all songs from the playlist + await db + .delete(playlistSongs) + .where(eq(playlistSongs.playlistId, data.id)); - // Now delete the playlist - const result = await tx.delete(playlists).where(eq(playlists.id, data.id)); + // delete playlist itself + const result = await db + .delete(playlists) + .where(eq(playlists.id, data.id)); + + // check if no rows were affected, throw error if true if ("changes" in result && result.changes === 0) { throw new Error(`Playlist ${data.id} not found`); } - }); - return { message: `Playlist ${data.id} deleted successfully` }; + // commit on success + sqlite.exec("COMMIT;"); + return { message: `Playlist ${data.id} deleted successfully` }; + } catch (err: any) { + sqlite.exec("ROLLBACK;"); + throw new Error(`Failed to delete playlist: ${err.message}`); + } }; export const updatePlaylist = async (data: any) => { diff --git a/renderer/pages/playlists.tsx b/renderer/pages/playlists.tsx index fb67b3f..90b3ac1 100644 --- a/renderer/pages/playlists.tsx +++ b/renderer/pages/playlists.tsx @@ -50,7 +50,9 @@ export default function Playlists() { useEffect(() => { const load = () => window.ipc.invoke("getAllPlaylists").then((resp) => setPlaylists(resp)); + load(); + const resetListener = window.ipc.on("resetPlaylistsState", load); return () => { resetListener(); @@ -66,6 +68,7 @@ export default function Playlists() { const createPlaylist = async (data: z.infer) => { setLoading(true); let playlistCoverPath = null; + try { const files = data.playlistCover as FileList | undefined; if (files && files.length > 0) { @@ -76,16 +79,19 @@ export default function Playlists() { data: Array.from(new Uint8Array(buffer)), }); } + const resp = await window.ipc.invoke("createPlaylist", { name: data.name, description: data.description, cover: playlistCoverPath, }); + setDialogOpen(false); setPreviewUrl(""); form.reset(); router.push(`/playlists/${resp.lastInsertRowid}`); - } catch { + } catch (err) { + console.error(err); toast(
@@ -110,6 +116,7 @@ export default function Playlists() { Create Playlist
+
{playlists.map((pl) => ( @@ -139,6 +146,7 @@ export default function Playlists() { ))}
+ @@ -147,6 +155,7 @@ export default function Playlists() { Add a new playlist to your library. +
)} /> +
+