diff --git a/lib/db/initDB.ts b/lib/db/initDB.ts index 38a2341..5ccd3db 100644 --- a/lib/db/initDB.ts +++ b/lib/db/initDB.ts @@ -1,18 +1,173 @@ import { prisma } from "@/lib/db/prisma" import argon2 from 'argon2' + +// TEST +import { getSongs, getSongById, searchSongsByTitle, createSong, updateSong, deleteSong } from "@/lib/db/song"; +import { getSongBooks, getSongBookById, searchSongBooksByTitle, createSongBook, updateSongBook, deleteSongBook } from "@/lib/db/songbook"; +import { addSongToBook, updateSongInBook, removeSongFromBook, getSongByNumber, isSongNumberTaken } from "@/lib/db/songbooksongs"; +import { getUsers, getUserById, getUserByUsername, createUser, updateUser, deleteUser } from "@/lib/db/user"; +// TEST + + + + + export async function initDB() { const count = await prisma.user.count() - if (count === 0) { - await prisma.user.create({ - data: { - username: process.env.ADMIN_USERNAME!, - password: await argon2.hash(process.env.ADMIN_PASSWORD!), - role: 'admin', - name: 'Admin' - } - }) - console.log('Created admin user') - } + // if (count === 0) { + // await prisma.user.create({ + // data: { + // username: process.env.ADMIN_USERNAME!, + // password: await argon2.hash(process.env.ADMIN_PASSWORD!), + // role: 'admin', + // name: 'Admin' + // } + // }) + // console.log('Created admin user') + // } + + // TEST + runTests(); + } + +async function runTests() { + // ========================================== + // TEST: USER + // ========================================== + console.log("\n############ USER #############"); + + const user1 = await createUser({ username: "1 User", password: "admin" }); + if (user1.success) { + console.log("✅ [PASSED] create user 1"); + } else { + console.log("❌ [FAILED] create user 1", user1); + } + + const user1_dupl = await createUser({ username: "1 User", password: "admin" }); + if (!user1_dupl.success) { + console.log("✅ [PASSED] prevent duplicate user"); + } else { + console.log("❌ [FAILED] prevent duplicate user", user1_dupl); + } + + const user2 = await createUser({ username: "UserUser", password: "Admin" }); + if (user2.success) { // Korrigiert: user2 statt user1 + console.log("✅ [PASSED] create user 2"); + } else { + console.log("❌ [FAILED] create user 2", user2); + } + + const users = await getUsers(); + const userList = users.users || users.data || []; + if (users.success && userList.length === 2) { + console.log("✅ [PASSED] getUsers count === 2"); + } else { + console.log("❌ [FAILED] getUsers count (expected 2)", users); + } + + // Weitere User Actions testen + const userById = await getUserById(1); + console.log(userById.success ? "✅ [PASSED] getUserById" : "❌ [FAILED] getUserById", userById); + + const userByName = await getUserByUsername("1 User"); + console.log(userByName.success ? "✅ [PASSED] getUserByUsername" : "❌ [FAILED] getUserByUsername", userByName); + + const updatedUser = await updateUser(1, { username: "UserUserUser", role: "admin" }); + console.log(updatedUser.success ? "✅ [PASSED] updateUser" : "❌ [FAILED] updateUser", updatedUser); + + const deletedUser = await deleteUser(1); + console.log(deletedUser.success ? "✅ [PASSED] deleteUser" : "❌ [FAILED] deleteUser", deletedUser); + + + // ========================================== + // TEST: SONGBOOKS + // ========================================== + console.log("\n############ SONGBOOKS #############"); + + const sb1 = await createSongBook({ title: "Gesangbuch1", language: "DE" }); + console.log(sb1.success ? "✅ [PASSED] createSongBook 1" : "❌ [FAILED] createSongBook 1", sb1); + + const sb1_dupl = await createSongBook({ title: "Gesangbuch1", language: "DE" }); + console.log(!sb1_dupl.success ? "✅ [PASSED] prevent duplicate songbook" : "❌ [FAILED] prevent duplicate songbook", sb1_dupl); + + const sb2 = await createSongBook({ title: "Gesangbuch2", language: "DE" }); + console.log(sb2.success ? "✅ [PASSED] createSongBook 2" : "❌ [FAILED] createSongBook 2", sb2); + + const sbooks = await getSongBooks(); + const sbookList = sbooks.songBooks || sbooks.data || []; + console.log(sbooks.success && sbookList.length === 2 ? "✅ [PASSED] getSongBooks count === 2" : "❌ [FAILED] getSongBooks count", sbooks); + + const sbById = await getSongBookById(1); + console.log(sbById.success ? "✅ [PASSED] getSongBookById" : "❌ [FAILED] getSongBookById", sbById); + + const sbSearch = await searchSongBooksByTitle("GesangBuch"); + console.log(sbSearch.success ? "✅ [PASSED] searchSongBooksByTitle" : "❌ [FAILED] searchSongBooksByTitle", sbSearch); + + const sbUpdate = await updateSongBook(1, { title: "Liederbuch", language: "Deutsch" }); + console.log(sbUpdate.success ? "✅ [PASSED] updateSongBook" : "❌ [FAILED] updateSongBook", sbUpdate); + + const sbDel = await deleteSongBook(1); + console.log(sbDel.success ? "✅ [PASSED] deleteSongBook" : "❌ [FAILED] deleteSongBook", sbDel); + + + // ========================================== + // TEST: SONGS + // ========================================== + console.log("\n############ SONGS #############"); + + const song1 = await createSong({ title: "1 Lied" }); + console.log(song1.success ? "✅ [PASSED] createSong 1" : "❌ [FAILED] createSong 1", song1); + + const song1_dupl = await createSong({ title: "1 Lied" }); + console.log(!song1_dupl.success ? "✅ [PASSED] prevent duplicate song" : "❌ [FAILED] prevent duplicate song", song1_dupl); + + const song2 = await createSong({ title: "Liiiied" }); + console.log(song2.success ? "✅ [PASSED] createSong 2" : "❌ [FAILED] createSong 2", song2); + + const songs = await getSongs(); + const songList = songs.songs || songs.data || []; + console.log(songs.success && songList.length === 2 ? "✅ [PASSED] getSongs count === 2" : "❌ [FAILED] getSongs count", songs); + + const songById = await getSongById(2); + console.log(songById.success ? "✅ [PASSED] getSongById" : "❌ [FAILED] getSongById", songById); + + const songSearch = await searchSongsByTitle("liEd"); + console.log(songSearch.success ? "✅ [PASSED] searchSongsByTitle" : "❌ [FAILED] searchSongsByTitle", songSearch); + + const songUpdate = await updateSong(1, { title: "Lied der Lieder" }); + console.log(songUpdate.success ? "✅ [PASSED] updateSong" : "❌ [FAILED] updateSong", songUpdate); + + const songDel = await deleteSong(1); + console.log(songDel.success ? "✅ [PASSED] deleteSong" : "❌ [FAILED] deleteSong", songDel); + + + // ========================================== + // TEST: SONGBOOK SONGS (RELATIONS) + // ========================================== + console.log("\n############ SONGBOOKSONGS #############"); + + const addSong1 = await addSongToBook(1, 1, 1); // bookId, songId, songNumber + console.log(addSong1.success ? "✅ [PASSED] addSongToBook 1" : "❌ [FAILED] addSongToBook 1", addSong1); + + const addSong1_dupl = await addSongToBook(1, 1, 1); + console.log(!addSong1_dupl.success ? "✅ [PASSED] prevent duplicate song in book" : "❌ [FAILED] prevent duplicate song in book", addSong1_dupl); + + const addSong2 = await addSongToBook(1, 2, 2); + console.log(addSong2.success ? "✅ [PASSED] addSongToBook 2" : "❌ [FAILED] addSongToBook 2", addSong2); + + const songByNum = await getSongByNumber(1, 2); + console.log(songByNum.success ? "✅ [PASSED] getSongByNumber" : "❌ [FAILED] getSongByNumber", songByNum); + + const updateInBook = await updateSongInBook(1, 1, { songNumber: 3 }); + console.log(updateInBook.success ? "✅ [PASSED] updateSongInBook" : "❌ [FAILED] updateSongInBook", updateInBook); + + const isTaken = await isSongNumberTaken(1, 2); + console.log(typeof isTaken === "boolean" || isTaken.success ? "✅ [PASSED] isSongNumberTaken" : "❌ [FAILED] isSongNumberTaken", isTaken); + + const removeSong = await removeSongFromBook(1, 1); + console.log(removeSong.success ? "✅ [PASSED] removeSongFromBook" : "❌ [FAILED] removeSongFromBook", removeSong); +} + diff --git a/lib/db/song.ts b/lib/db/song.ts index 29cc1f4..e5734e5 100644 --- a/lib/db/song.ts +++ b/lib/db/song.ts @@ -85,7 +85,6 @@ export async function searchSongsByTitle(query: string, limit: number = 20) { where: { title: { contains: query.trim(), - mode: "insensitive", // Textsearch }, }, select: { diff --git a/lib/db/songbook.ts b/lib/db/songbook.ts index 18501a2..f80c00d 100644 --- a/lib/db/songbook.ts +++ b/lib/db/songbook.ts @@ -78,7 +78,7 @@ export async function getSongBookById(id: number) { * @param limit - Max number of songbooks returned for performance (default: 20). * @returns Object with success state and an array of matching songbooks. */ -export async function searchSongBookByTitle(query: string, limit: number = 20) { +export async function searchSongBooksByTitle(query: string, limit: number = 20) { try { const cleanQuery = query.trim(); if (!cleanQuery) { @@ -89,7 +89,6 @@ export async function searchSongBookByTitle(query: string, limit: number = 20) { where: { title: { contains: cleanQuery, - mode: "insensitive", // Textsearch }, }, select: { diff --git a/lib/db/songbooksongs.ts b/lib/db/songbooksongs.ts index 50d5742..57183b4 100644 --- a/lib/db/songbooksongs.ts +++ b/lib/db/songbooksongs.ts @@ -3,7 +3,7 @@ import { prisma } from "@/lib/db/prisma"; export type DbResponse = | { success: true; status: number, entry: { songId: number | undefined; - bookId: number | undefined; + songBookId: number | undefined; songNumber: number | undefined }, song: {id: number, title: string} | undefined } | { success: false; status: number; error: string }; @@ -17,9 +17,9 @@ export type DbResponse = * @param songNumber - Track/Song number in that specific book * @returns {"success": boolean, "status": number, "entry": {"songId": number, "bookId": number, "songNumber": number}} */ -export async function addSongToBook(songId: number, bookId: number, songNumber: number): Promise { +export async function addSongToBook(songId: number, songBookId: number, songNumber: number): Promise { try { - const isTaken = await isSongNumberTaken(bookId, songNumber); + const isTaken = await isSongNumberTaken(songBookId, songNumber); if (isTaken) { return { success: false, status: 409, error: "Songnumber already taken in this songbook" }; } @@ -27,7 +27,7 @@ export async function addSongToBook(songId: number, bookId: number, songNumber: const entry = await prisma.songBookSongs.create({ data: { songId, - bookId, + songBookId, songNumber, }, }); @@ -54,18 +54,18 @@ export async function addSongToBook(songId: number, bookId: number, songNumber: * @param data - Object of the new data * @returns {"success": true, "status": number, "entry": {"songId": number, "bookId": number, "songNumber": number}} | {"success": false, "error": string} */ -export async function updateSongInBook(songId: number, bookId: number, data: {songNumber: number}) { +export async function updateSongInBook(songId: number, songBookId: number, data: {songNumber: number}) { try { - const isTaken = await isSongNumberTaken(bookId, data.songNumber); + const isTaken = await isSongNumberTaken(songBookId, data.songNumber); if (isTaken) { return { success: false, status: 409, error: "New song number is already taken in this songbook" }; } const entry = await prisma.songBookSongs.update({ where: { - songId_bookId: { + songBookId_songId: { songId, - bookId, + songBookId, }, }, data: { @@ -73,7 +73,7 @@ export async function updateSongInBook(songId: number, bookId: number, data: {so }, select: { songId: true, - bookId: true, + songBookId: true, songNumber: true, }, }); @@ -103,13 +103,13 @@ export async function updateSongInBook(songId: number, bookId: number, data: {so * @param bookId - ID of the songbook * @returns {"success": boolean, "status": number} */ -export async function removeSongFromBook(songId: number, bookId: number) { +export async function removeSongFromBook(songId: number, songBookId: number) { try { await prisma.songBookSongs.delete({ where: { - songId_bookId: { + songBookId_songId: { songId, - bookId, + songBookId, }, }, }); @@ -168,11 +168,11 @@ export async function getSongByNumber(bookId: number, songNumber: number) { * @param songNumber - The song number to check * @returns true if taken, false if available */ -export async function isSongNumberTaken(bookId: number, songNumber: number): Promise { +export async function isSongNumberTaken(songBookId: number, songNumber: number): Promise { try { const existing = await prisma.songBookSongs.findFirst({ where: { - bookId, + songBookId, songNumber, }, select: {