diff --git a/example.env b/example.env new file mode 100644 index 0000000..3580a10 --- /dev/null +++ b/example.env @@ -0,0 +1,12 @@ +# This is an Example for the Environment-Variables + +# Create the admin user +ADMIN_USERNAME="admin" +ADMIN_PASSWORD="admin" + +# Database location +DATABASE_URL="file:./songs.sqlite" + +# Sectret for JWT cookies +JWT_SECRET=3a1e8c4b7d0f2a5e9c6b3d1f4a8e2c7b0d5f3a3f9afd1b4d8f0a7e6c5b259f + diff --git a/lib/db/initDB.ts b/lib/db/initDB.ts index cf14eec..38a2341 100644 --- a/lib/db/initDB.ts +++ b/lib/db/initDB.ts @@ -7,12 +7,12 @@ export async function initDB() { 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' + username: process.env.ADMIN_USERNAME!, + password: await argon2.hash(process.env.ADMIN_PASSWORD!), + role: 'admin', + name: 'Admin' } }) - console.log('Admin-User angelegt.') + console.log('Created admin user') } } diff --git a/lib/db/song.ts b/lib/db/song.ts new file mode 100644 index 0000000..203d558 --- /dev/null +++ b/lib/db/song.ts @@ -0,0 +1,2 @@ +import { prisma } from "@/lib/db/prisma"; + diff --git a/lib/db/managing.ts b/lib/db/songbook.ts similarity index 57% rename from lib/db/managing.ts rename to lib/db/songbook.ts index 175e3cd..e5fbbaa 100644 --- a/lib/db/managing.ts +++ b/lib/db/songbook.ts @@ -1,41 +1,115 @@ import { prisma } from "@/lib/db/prisma"; -export async function getSongs() { - try { - const songs = db.prepare(` - SELECT - ss.songbook_id, - ss.song_id, - ss.song_number, - s.title - FROM songbook_songs ss - JOIN songs s ON ss.song_id = s.id - ORDER BY ss.songbook_id, ss.song_number ASC - `).all(); - return songs; - } catch (error) { - return null + + +/** + * getSongBooks + * Gibt alle Songbücher aus, mit allen Liedern + * [{"id":number, "title": string, "songs":[{"id":number, "title": string},...]},...] + */ +export async function getSongBooks() { + try{ + const songbooks = await prisma.songBook.findMany({ + include: { + songs: { + select: { + song_number: true, + song: true, + }, + orderBy: { + song_number: "asc", + }, + }, + }, + }); + return { success: true, songBooks: songbooks }; + } catch(error){ + return { success: false, error: error } } } -export async function getSong(songbookId: number, songId: string) { - try { - const song = db.prepare(` - SELECT - sb.name AS songbook_name, - ss.song_number AS number, - s.title AS song_title - FROM songbook_songs ss - JOIN songbooks sb ON ss.songbook_id = sb.id - JOIN songs s ON ss.song_id = s.id - WHERE ss.songbook_id = ? AND ss.song_id = ? - `).get(songbookId, songId) as { songbook_name: string; number: string; song_title: string } | undefined; +/** + * getSongBooks + * Gibt ein Songbuch aus, mit allen Liedern + * {"id":number, "title": string, "songs":[{"id":number, "title": string},...]} + */ +export async function getSongBook(songbookid: number) { + try{ + const songbook = await prisma.songBook.findMany({ + where: { + id: songbookid, + }, + include: { + songs: { + select: { + songNumber: true, + song: true, + }, + orderBy: { + songNumber: "asc", + }, + }, + }, + }); + return { success: true, songBook: songbook }; + } catch(error){ + return { success: false, error: error } + } +} - return song || null; + + + + +/** + * GetSongs + * Gibt alle Songs zurück. + * [{"id": int, "title": string}] + */ +export async function getSongs() { + try { + const songs = prisma.song.findMany({ + select: { + id: true, + title: true, + }, + orderBy: [ + { id: "asc" }, + ], + }); + return songs; + } catch (error) { + return { success: false, error: error }; + } +} + +export async function getSong(songBookId: number, songId: string) { + try { + const song = await prisma.songBookSongs.findFirst({ + where: { + songBookId: songBookId, + songId: songId, + }, + select: { + songNumber: true, + book: { + select: { + title: true, + }, + }, + song: { + select: { + title: true, + }, + }, + }, + }); + + return song ? {songbook_name: song.book.name, number: song.songNumber, song_title: song.song.title,}:null; } catch (error) { console.error("Fehler in getSong:", error); - return null; + return { success: false, error: error }; } } @@ -61,7 +135,7 @@ export async function getSongInfos(songId: string) { return songRecords || null; } catch (error) { console.error("Fehler in getSong:", error); - return null; + return { success: false, error: error }; } } @@ -87,7 +161,7 @@ export async function addSongToSongbook(title:string, songbookId:number, number: return { success: true } } catch (error) { console.error("Error in addSongToSongbook:", error); - return { success: false }; + return { success: false, error: error }; } } @@ -113,7 +187,7 @@ export async function attachSongToSongbook(songId: number, songbookId: number, n return { success: true }; } catch (error) { console.error("Error in attachSongToSongbook:", error); - return { success: false }; + return { success: false, error: error }; } } @@ -127,7 +201,7 @@ export async function removeSongfromSongbook(songId: number, songbookId: number) return { success: info.changes > 0 }; } catch (error) { console.error("Error in removeSongfromSongbook:", error); - return { success: false }; + return { success: false, error: error }; } } @@ -136,7 +210,7 @@ export async function getSongbooks() { const songbooks = db.prepare('SELECT id, name, language FROM songbooks ORDER BY name ASC').all(); return songbooks; } catch (error) { - return null; + return { success: false, error: error }; } } @@ -154,6 +228,6 @@ export async function addSongbook(name: string, language: string) { } return newBook; } catch (error) { - return null; + return { success: false, error: error }; } } diff --git a/lib/db/user.ts b/lib/db/user.ts new file mode 100644 index 0000000..0690e9f --- /dev/null +++ b/lib/db/user.ts @@ -0,0 +1,80 @@ +import { prisma } from "@/lib/db/prisma"; + +/* + * GetUsers + * Gibt alle user zurück + * [{"id":number, "username": string, "role": string},...] + */ +export async function getUsers() { + try{ + const users = await prisma.user.findMany({ + select: { + id: true, + username: true, + role: true, + } + }); + if (!users) { + return { success: false, error: "Benutzer nicht gefunden." }; + } + + return { success: true, users }; + } catch(error) { + console.error("Error in getUsers", error); + return { success: false, error: "Error while loading Users" }; + } +} + +/* + * GetUserById + * Gibt einen Benutzer anhand der ID zurück + * {"id":number, "username": string, "role": string} + */ +export async function getUserById(id: number) { + try{ + const user = await prisma.user.findUnique({ + where: { id }, + select: { + id: true, + username: true, + role: true, + } + }); + + if (!user) { + return { success: false, error: "Benutzer nicht gefunden." }; + } + + return { success: true, user }; + } catch(error) { + console.error("Error in getUserById", error); + return { success: false, error: "Error while loading User" }; + } +} + +/* + * GetUserByUsername + * Gibt einen Benutzer anhand des Usernames zurück + * {"id":number, "username": string, "role": string} + */ +export async function getUserByUsername(username: string) { + try{ + const user = await prisma.user.findUnique({ + where: { username }, + select: { + id: true, + username: true, + role: true, + } + }); + + if (!user) { + return { success: false, error: "Benutzer nicht gefunden." }; + } + + return { success: true, user }; + } catch(error) { + console.error("Error in getUserByUsername", error); + return { success: false, error: "Error while loading User" }; + } +} diff --git a/prisma/migrations/20260730110855_correct_var_names/migration.sql b/prisma/migrations/20260730110855_correct_var_names/migration.sql new file mode 100644 index 0000000..016caeb --- /dev/null +++ b/prisma/migrations/20260730110855_correct_var_names/migration.sql @@ -0,0 +1,27 @@ +/* + Warnings: + + - The primary key for the `SongBookSongs` table will be changed. If it partially fails, the table could be left without primary key constraint. + - You are about to drop the column `SongBookId` on the `SongBookSongs` table. All the data in the column will be lost. + - You are about to drop the column `SongId` on the `SongBookSongs` table. All the data in the column will be lost. + - Added the required column `songBookId` to the `SongBookSongs` table without a default value. This is not possible if the table is not empty. + - Added the required column `songId` to the `SongBookSongs` table without a default value. This is not possible if the table is not empty. + +*/ +-- RedefineTables +PRAGMA defer_foreign_keys=ON; +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_SongBookSongs" ( + "songBookId" INTEGER NOT NULL, + "songId" INTEGER NOT NULL, + "songNumber" INTEGER NOT NULL, + + PRIMARY KEY ("songBookId", "songId"), + CONSTRAINT "SongBookSongs_songId_fkey" FOREIGN KEY ("songId") REFERENCES "Song" ("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "SongBookSongs_songBookId_fkey" FOREIGN KEY ("songBookId") REFERENCES "SongBook" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); +INSERT INTO "new_SongBookSongs" ("songNumber") SELECT "songNumber" FROM "SongBookSongs"; +DROP TABLE "SongBookSongs"; +ALTER TABLE "new_SongBookSongs" RENAME TO "SongBookSongs"; +PRAGMA foreign_keys=ON; +PRAGMA defer_foreign_keys=OFF; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 18da547..62de787 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -1,7 +1,4 @@ -// This is your Prisma schema file, -// learn more about it in the docs: https://pris.ly/d/prisma-schema -// Get a free hosted Postgres database in seconds: `npx create-db` generator client { provider = "prisma-client" @@ -24,7 +21,7 @@ model Song { id Int @id @default(autoincrement()) title String @unique - songBookSongs SongBookSongs[] + songBooks SongBookSongs[] } model SongBook { @@ -32,16 +29,16 @@ model SongBook { title String @unique language String - songBookSongs SongBookSongs[] + songs SongBookSongs[] } model SongBookSongs { - SongBookId Int - SongId Int + songBookId Int + songId Int songNumber Int - song Song @relation(fields: [SongId], references: [id], onDelete: Cascade) - book SongBook @relation(fields: [SongBookId], references: [id], onDelete: Cascade) + song Song @relation(fields: [songId], references: [id], onDelete: Cascade) + book SongBook @relation(fields: [songBookId], references: [id], onDelete: Cascade) - @@id([SongBookId, SongId]) + @@id([songBookId, songId]) }