Some changes in manage.ts --> 3 user.ts, song.ts, songbook.ts

Aufspaltung auf 3 Dateien, aber man muss noch weiter arbeiten

(Task: Von better-sqlite3 --> ORM)
This commit is contained in:
rudi committed 2026-07-30 14:13:02 +02:00
1 parent d395308c6d
commit d03ff9d26d
7 files changed
+242 -50

No files matched your search

+5 -5
View File
@@ -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')
}
}
+2
View File
@@ -0,0 +1,2 @@
import { prisma } from "@/lib/db/prisma";
+109 -35
View File
@@ -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 };
}
}
+80
View File
@@ -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" };
}
}