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:
1 parent
d395308c6d
commit
d03ff9d26d
7 files changed
+233
-41
No files matched your search
+12
@@ -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
|
||||||
|
|
||||||
+1
-1
@@ -13,6 +13,6 @@ export async function initDB() {
|
|||||||
name: 'Admin'
|
name: 'Admin'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
console.log('Admin-User angelegt.')
|
console.log('Created admin user')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
import { prisma } from "@/lib/db/prisma";
|
||||||
|
|
||||||
@@ -1,41 +1,115 @@
|
|||||||
import { prisma } from "@/lib/db/prisma";
|
import { prisma } from "@/lib/db/prisma";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GetSongs
|
||||||
|
* Gibt alle Songs zurück.
|
||||||
|
* [{"id": int, "title": string}]
|
||||||
|
*/
|
||||||
export async function getSongs() {
|
export async function getSongs() {
|
||||||
try {
|
try {
|
||||||
const songs = db.prepare(`
|
const songs = prisma.song.findMany({
|
||||||
SELECT
|
select: {
|
||||||
ss.songbook_id,
|
id: true,
|
||||||
ss.song_id,
|
title: true,
|
||||||
ss.song_number,
|
},
|
||||||
s.title
|
orderBy: [
|
||||||
FROM songbook_songs ss
|
{ id: "asc" },
|
||||||
JOIN songs s ON ss.song_id = s.id
|
],
|
||||||
ORDER BY ss.songbook_id, ss.song_number ASC
|
});
|
||||||
`).all();
|
|
||||||
return songs;
|
return songs;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return null
|
return { success: false, error: error };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getSong(songbookId: number, songId: string) {
|
export async function getSong(songBookId: number, songId: string) {
|
||||||
try {
|
try {
|
||||||
const song = db.prepare(`
|
const song = await prisma.songBookSongs.findFirst({
|
||||||
SELECT
|
where: {
|
||||||
sb.name AS songbook_name,
|
songBookId: songBookId,
|
||||||
ss.song_number AS number,
|
songId: songId,
|
||||||
s.title AS song_title
|
},
|
||||||
FROM songbook_songs ss
|
select: {
|
||||||
JOIN songbooks sb ON ss.songbook_id = sb.id
|
songNumber: true,
|
||||||
JOIN songs s ON ss.song_id = s.id
|
book: {
|
||||||
WHERE ss.songbook_id = ? AND ss.song_id = ?
|
select: {
|
||||||
`).get(songbookId, songId) as { songbook_name: string; number: string; song_title: string } | undefined;
|
title: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
song: {
|
||||||
|
select: {
|
||||||
|
title: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
return song || null;
|
return song ? {songbook_name: song.book.name, number: song.songNumber, song_title: song.song.title,}:null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Fehler in getSong:", 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;
|
return songRecords || null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Fehler in getSong:", 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 }
|
return { success: true }
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error in addSongToSongbook:", 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 };
|
return { success: true };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error in attachSongToSongbook:", 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 };
|
return { success: info.changes > 0 };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error in removeSongfromSongbook:", 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();
|
const songbooks = db.prepare('SELECT id, name, language FROM songbooks ORDER BY name ASC').all();
|
||||||
return songbooks;
|
return songbooks;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return null;
|
return { success: false, error: error };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,6 +228,6 @@ export async function addSongbook(name: string, language: string) {
|
|||||||
}
|
}
|
||||||
return newBook;
|
return newBook;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return null;
|
return { success: false, error: error };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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" };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
+7
-10
@@ -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 {
|
generator client {
|
||||||
provider = "prisma-client"
|
provider = "prisma-client"
|
||||||
@@ -24,7 +21,7 @@ model Song {
|
|||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
title String @unique
|
title String @unique
|
||||||
|
|
||||||
songBookSongs SongBookSongs[]
|
songBooks SongBookSongs[]
|
||||||
}
|
}
|
||||||
|
|
||||||
model SongBook {
|
model SongBook {
|
||||||
@@ -32,16 +29,16 @@ model SongBook {
|
|||||||
title String @unique
|
title String @unique
|
||||||
language String
|
language String
|
||||||
|
|
||||||
songBookSongs SongBookSongs[]
|
songs SongBookSongs[]
|
||||||
}
|
}
|
||||||
|
|
||||||
model SongBookSongs {
|
model SongBookSongs {
|
||||||
SongBookId Int
|
songBookId Int
|
||||||
SongId Int
|
songId Int
|
||||||
songNumber Int
|
songNumber Int
|
||||||
|
|
||||||
song Song @relation(fields: [SongId], references: [id], onDelete: Cascade)
|
song Song @relation(fields: [songId], references: [id], onDelete: Cascade)
|
||||||
book SongBook @relation(fields: [SongBookId], references: [id], onDelete: Cascade)
|
book SongBook @relation(fields: [songBookId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
@@id([SongBookId, SongId])
|
@@id([songBookId, songId])
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user