213 lines
6.3 KiB
TypeScript
213 lines
6.3 KiB
TypeScript
import { prisma } from "@/lib/db/prisma";
|
|
|
|
|
|
/**
|
|
* GetSongBooks
|
|
* Returns all available songbooks
|
|
*
|
|
* @returns {"success": boolean, "songBooks": [{"id": number, "title": string, "language": string},...]}
|
|
*/
|
|
export async function getSongBooks() {
|
|
try {
|
|
const songBooks = await prisma.songBookModel.findMany({ // Name deines Prisma-Modells für Bücher
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
language: true,
|
|
},
|
|
orderBy: {
|
|
title: "asc",
|
|
},
|
|
});
|
|
|
|
return { success: true, songBooks };
|
|
} catch (error) {
|
|
console.error("Error in getSongBooks:", error);
|
|
return { success: false, error: "Error while loading SongBooks" };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* GetSongBookById
|
|
* Returns a songbook by its ID along with all included songs sorted by song number
|
|
*
|
|
* @param id - ID indicating the songbook
|
|
* @returns {"success": boolean, "songBook": object}
|
|
*/
|
|
export async function getSongBookById(id: number) {
|
|
try {
|
|
const songBook = await prisma.songBookModel.findUnique({
|
|
where: { id },
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
language: true,
|
|
songs: {
|
|
select: {
|
|
songNumber: true,
|
|
song: {
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
},
|
|
},
|
|
},
|
|
orderBy: {
|
|
songNumber: "asc", // Songs direkt nach Liednummer sortiert zurückgeben
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!songBook) {
|
|
return { success: false, error: "SongBook not found" };
|
|
}
|
|
|
|
return { success: true, songBook };
|
|
} catch (error) {
|
|
console.error("Error in getSongBookById:", error);
|
|
return { success: false, error: "Error while loading SongBook" };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* SearchSongBooksByTitle
|
|
* Searches for songbooks matching the given title query, including their songs.
|
|
*
|
|
* @param query - The search term for the songbook title.
|
|
* @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 searchSongBooksByTitle(query: string, limit: number = 20) {
|
|
try {
|
|
const cleanQuery = query.trim();
|
|
if (!cleanQuery) {
|
|
return { success: true, songBooks: [] };
|
|
}
|
|
|
|
const songBooks = await prisma.songBookModel.findMany({
|
|
where: {
|
|
title: {
|
|
contains: cleanQuery,
|
|
mode: "insensitive", // Textsearch
|
|
},
|
|
},
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
language: true,
|
|
songs: {
|
|
select: {
|
|
songNumber: true,
|
|
song: {
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
},
|
|
},
|
|
},
|
|
orderBy: {
|
|
songNumber: "asc",
|
|
},
|
|
},
|
|
},
|
|
take: limit,
|
|
});
|
|
|
|
return { success: true, songBooks };
|
|
} catch (error) {
|
|
console.error("Error in searchSongBooksByTitle:", error);
|
|
return { success: false, error: "Error while searching SongBooks" };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* CreateSongBook
|
|
* Creates a new songbook
|
|
*
|
|
* @param data - object with title and language
|
|
* @returns {"success": boolean, "songBook": {"id": number, "title": string, "language": string}}
|
|
*/
|
|
export async function createSongBook(data: { title: string; language: string }) {
|
|
try {
|
|
const songBook = await prisma.songBookModel.create({
|
|
data: {
|
|
title: data.title,
|
|
language: data.language,
|
|
},
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
language: true,
|
|
},
|
|
});
|
|
|
|
return { success: true, songBook };
|
|
} catch (error) {
|
|
if (error.code === "P2002") {
|
|
return { success: false, error: "SongBook title already exists" };
|
|
}
|
|
|
|
console.error("Error in createSongBook:", error);
|
|
return { success: false, error: "Error while creating SongBook" };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* AddSongToBook
|
|
* Connects an existing song to a songbook with a specific song number
|
|
*
|
|
* @param songId - ID of the song
|
|
* @param bookId - ID of the songbook
|
|
* @param songNumber - Track/Song number in that specific book
|
|
*/
|
|
export async function addSongToBook(songId: number, bookId: number, songNumber: number) {
|
|
try {
|
|
const entry = await prisma.songBook.create({ // Verknüpfungs-Tabelle
|
|
data: {
|
|
songId,
|
|
bookId,
|
|
songNumber,
|
|
},
|
|
});
|
|
|
|
return { success: true, entry };
|
|
} catch (error) {
|
|
if (error.code === "P2002") {
|
|
return { success: false, error: "Song is already in this book or song number is already taken" };
|
|
}
|
|
|
|
console.error("Error in addSongToBook:", error);
|
|
return { success: false, error: "Error while adding song to book" };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* RemoveSongFromBook
|
|
* Removes a song from a specific songbook
|
|
*
|
|
* @param songId - ID of the song
|
|
* @param bookId - ID of the songbook
|
|
*/
|
|
export async function removeSongFromBook(songId: number, bookId: number) {
|
|
try {
|
|
await prisma.songBook.delete({
|
|
where: {
|
|
songId_bookId: { // Composite Primary/Unique Key in Prisma (Prisma-Standard für Pivot-Tabellen)
|
|
songId,
|
|
bookId,
|
|
},
|
|
},
|
|
});
|
|
|
|
return { success: true };
|
|
} catch (error) {
|
|
if (error.code === "P2025") {
|
|
return { success: false, error: "Song assignment not found" };
|
|
}
|
|
|
|
console.error("Error in removeSongFromBook:", error);
|
|
return { success: false, error: "Error while removing song from book" };
|
|
}
|
|
}
|