Added Statuscodes
Status Rückmeldungen den 'returns' hinzugefügt. Schanbro/PsalmTafel#30
This commit is contained in:
1 parent
80d88608bd
commit
a81ea69a8a
2 files changed
+36
-28
No files matched your search
+5
-5
@@ -9,7 +9,7 @@ import { prisma } from "@/lib/db/prisma";
|
|||||||
*/
|
*/
|
||||||
export async function getSongBooks() {
|
export async function getSongBooks() {
|
||||||
try {
|
try {
|
||||||
const songBooks = await prisma.songBookModel.findMany({ // Name deines Prisma-Modells für Bücher
|
const songBooks = await prisma.songBook.findMany({
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
title: true,
|
title: true,
|
||||||
@@ -36,7 +36,7 @@ export async function getSongBooks() {
|
|||||||
*/
|
*/
|
||||||
export async function getSongBookById(id: number) {
|
export async function getSongBookById(id: number) {
|
||||||
try {
|
try {
|
||||||
const songBook = await prisma.songBookModel.findUnique({
|
const songBook = await prisma.songBook.findUnique({
|
||||||
where: { id },
|
where: { id },
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
@@ -85,7 +85,7 @@ export async function searchSongBookByTitle(query: string, limit: number = 20) {
|
|||||||
return { success: true, songBooks: [] };
|
return { success: true, songBooks: [] };
|
||||||
}
|
}
|
||||||
|
|
||||||
const songBooks = await prisma.songBookModel.findMany({
|
const songBooks = await prisma.songBook.findMany({
|
||||||
where: {
|
where: {
|
||||||
title: {
|
title: {
|
||||||
contains: cleanQuery,
|
contains: cleanQuery,
|
||||||
@@ -130,7 +130,7 @@ export async function searchSongBookByTitle(query: string, limit: number = 20) {
|
|||||||
*/
|
*/
|
||||||
export async function createSongBook(data: { title: string; language: string }) {
|
export async function createSongBook(data: { title: string; language: string }) {
|
||||||
try {
|
try {
|
||||||
const songBook = await prisma.songBookModel.create({
|
const songBook = await prisma.songBook.create({
|
||||||
data: {
|
data: {
|
||||||
title: data.title,
|
title: data.title,
|
||||||
language: data.language,
|
language: data.language,
|
||||||
@@ -214,7 +214,7 @@ export async function updateSongBook(
|
|||||||
*/
|
*/
|
||||||
export async function deleteSongBook(id: number) {
|
export async function deleteSongBook(id: number) {
|
||||||
try {
|
try {
|
||||||
const songBook = await prisma.songBookModel.delete({
|
const songBook = await prisma.songBook.delete({
|
||||||
where: { id },
|
where: { id },
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
|
|||||||
+31
-23
@@ -1,5 +1,12 @@
|
|||||||
import { prisma } from "@/lib/db/prisma";
|
import { prisma } from "@/lib/db/prisma";
|
||||||
|
|
||||||
|
export type DbResponse =
|
||||||
|
| { success: true; status: number, entry: {
|
||||||
|
songId: number | undefined;
|
||||||
|
bookId: number | undefined;
|
||||||
|
songNumber: number | undefined
|
||||||
|
}, song: {id: number, title: string} | undefined }
|
||||||
|
| { success: false; status: number; error: string };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AddSongToBook
|
* AddSongToBook
|
||||||
@@ -8,16 +15,16 @@ import { prisma } from "@/lib/db/prisma";
|
|||||||
* @param songId - ID of the song
|
* @param songId - ID of the song
|
||||||
* @param bookId - ID of the songbook
|
* @param bookId - ID of the songbook
|
||||||
* @param songNumber - Track/Song number in that specific book
|
* @param songNumber - Track/Song number in that specific book
|
||||||
* @returns {"success": boolean, "entry": {"songId": number, "bookId": number, "songNumber": number}}
|
* @returns {"success": boolean, "status": number, "entry": {"songId": number, "bookId": number, "songNumber": number}}
|
||||||
*/
|
*/
|
||||||
export async function addSongToBook(songId: number, bookId: number, songNumber: number) {
|
export async function addSongToBook(songId: number, bookId: number, songNumber: number): Promise<DbResponse> {
|
||||||
try {
|
try {
|
||||||
const isTaken = await isSongNumberTaken(bookId, songNumber);
|
const isTaken = await isSongNumberTaken(bookId, songNumber);
|
||||||
if (isTaken) {
|
if (isTaken) {
|
||||||
return { success: false, error: "Songnumber already taken in this songbook" };
|
return { success: false, status: 409, error: "Songnumber already taken in this songbook" };
|
||||||
}
|
}
|
||||||
|
|
||||||
const entry = await prisma.songBook.create({
|
const entry = await prisma.songBookSongs.create({
|
||||||
data: {
|
data: {
|
||||||
songId,
|
songId,
|
||||||
bookId,
|
bookId,
|
||||||
@@ -27,14 +34,14 @@ export async function addSongToBook(songId: number, bookId: number, songNumber:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
return { success: true, entry };
|
return { success: true, status: 200, entry };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.code === "P2002") {
|
if (error.code === "P2002") {
|
||||||
return { success: false, error: "Song is already in this book" };
|
return { success: false, status: 409, error: "Song is already in this book" };
|
||||||
}
|
}
|
||||||
|
|
||||||
console.error("Error in addSongToBook:", error);
|
console.error("Error in addSongToBook:", error);
|
||||||
return { success: false, error: "Error while adding song to book" };
|
return { success: false, status: 500, error: "Error while adding song to book" };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,16 +52,16 @@ export async function addSongToBook(songId: number, bookId: number, songNumber:
|
|||||||
* @param songId - ID of the song
|
* @param songId - ID of the song
|
||||||
* @param bookId - ID of the songbook
|
* @param bookId - ID of the songbook
|
||||||
* @param data - Object of the new data
|
* @param data - Object of the new data
|
||||||
* @returns {"success": true, "entry": {"songId": number, "bookId": number, "songNumber": number}} | {"success": false, "error": string}
|
* @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, bookId: number, data: {songNumber: number}) {
|
||||||
try {
|
try {
|
||||||
const isTaken = await isSongNumberTaken(bookId, data.songNumber);
|
const isTaken = await isSongNumberTaken(bookId, data.songNumber);
|
||||||
if (isTaken) {
|
if (isTaken) {
|
||||||
return { success: false, error: "New song number is already taken in this songbook" };
|
return { success: false, status: 409, error: "New song number is already taken in this songbook" };
|
||||||
}
|
}
|
||||||
|
|
||||||
const entry = await prisma.songBook.update({
|
const entry = await prisma.songBookSongs.update({
|
||||||
where: {
|
where: {
|
||||||
songId_bookId: {
|
songId_bookId: {
|
||||||
songId,
|
songId,
|
||||||
@@ -71,20 +78,20 @@ export async function updateSongInBook(songId: number, bookId: number, data: {so
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return { success: true, entry };
|
return { success: true, status: 200, entry };
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
// P2025 = Record to update not found
|
// P2025 = Record to update not found
|
||||||
if (error?.code === "P2025") {
|
if (error?.code === "P2025") {
|
||||||
return { success: false, error: "Song is not assigned to this songbook" };
|
return { success: false, status: 404, error: "Song is not assigned to this songbook" };
|
||||||
}
|
}
|
||||||
|
|
||||||
// P2002 = Unique Constraint Violation
|
// P2002 = Unique Constraint Violation
|
||||||
if (error?.code === "P2002") {
|
if (error?.code === "P2002") {
|
||||||
return { success: false, error: "Song number already exists in this songbook" };
|
return { success: false, status: 409, error: "Song number already exists in this songbook" };
|
||||||
}
|
}
|
||||||
|
|
||||||
console.error("Error in updateSongInBook:", error);
|
console.error("Error in updateSongInBook:", error);
|
||||||
return { success: false, error: "Error while updating song in book" };
|
return { success: false, status: 500, error: "Error while updating song in book" };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,10 +101,11 @@ export async function updateSongInBook(songId: number, bookId: number, data: {so
|
|||||||
*
|
*
|
||||||
* @param songId - ID of the song
|
* @param songId - ID of the song
|
||||||
* @param bookId - ID of the songbook
|
* @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, bookId: number) {
|
||||||
try {
|
try {
|
||||||
await prisma.songBook.delete({
|
await prisma.songBookSongs.delete({
|
||||||
where: {
|
where: {
|
||||||
songId_bookId: {
|
songId_bookId: {
|
||||||
songId,
|
songId,
|
||||||
@@ -106,14 +114,14 @@ export async function removeSongFromBook(songId: number, bookId: number) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return { success: true };
|
return { success: true, status: 200 };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.code === "P2025") {
|
if (error.code === "P2025") {
|
||||||
return { success: false, error: "Song assignment not found" };
|
return { success: false, status: 404, error: "Song assignment not found" };
|
||||||
}
|
}
|
||||||
|
|
||||||
console.error("Error in removeSongFromBook:", error);
|
console.error("Error in removeSongFromBook:", error);
|
||||||
return { success: false, error: "Error while removing song from book" };
|
return { success: false, status: 500, error: "Error while removing song from book" };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,7 +130,7 @@ export async function removeSongFromBook(songId: number, bookId: number) {
|
|||||||
*
|
*
|
||||||
* @param bookId - The ID of the songbook
|
* @param bookId - The ID of the songbook
|
||||||
* @param songNumber - The song number in that book
|
* @param songNumber - The song number in that book
|
||||||
* @returns {"success": true, "entry": {"songNumber": number "song": {"id": number, "title": string}}}
|
* @returns {"success": true, "status": number, "entry": {"songNumber": number "song": {"id": number, "title": string}}}
|
||||||
*/
|
*/
|
||||||
export async function getSongByNumber(bookId: number, songNumber: number) {
|
export async function getSongByNumber(bookId: number, songNumber: number) {
|
||||||
try {
|
try {
|
||||||
@@ -143,13 +151,13 @@ export async function getSongByNumber(bookId: number, songNumber: number) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!entry) {
|
if (!entry) {
|
||||||
return { success: false, error: "No song found for this number" };
|
return { success: false, status: 404, error: "No song found for this number" };
|
||||||
}
|
}
|
||||||
|
|
||||||
return { success: true, entry };
|
return { success: true, status: 200, entry };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error in getSongByNumber:", error);
|
console.error("Error in getSongByNumber:", error);
|
||||||
return { success: false, error: "Error while fetching song by number" };
|
return { success: false, status: 500, error: "Error while fetching song by number" };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,7 +170,7 @@ export async function getSongByNumber(bookId: number, songNumber: number) {
|
|||||||
*/
|
*/
|
||||||
export async function isSongNumberTaken(bookId: number, songNumber: number): Promise<boolean> {
|
export async function isSongNumberTaken(bookId: number, songNumber: number): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
const existing = await prisma.songBook.findFirst({
|
const existing = await prisma.songBookSongs.findFirst({
|
||||||
where: {
|
where: {
|
||||||
bookId,
|
bookId,
|
||||||
songNumber,
|
songNumber,
|
||||||
|
|||||||
Reference in new issue
Block a user