Song Getter & Setter
Ähnlich wie bei user
This commit is contained in:
1 parent
8deff3b39f
commit
b2545bbdf1
1 file changed
+223
+223
@@ -1,2 +1,225 @@
|
||||
import { prisma } from "@/lib/db/prisma";
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* GetSongs
|
||||
* Returns all songs
|
||||
*
|
||||
* @returns {"success": boolean, "songs": [{"id":number, "title": string},...]}
|
||||
*/
|
||||
export async function getSongs() {
|
||||
try{
|
||||
const songs = await prisma.song.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
},
|
||||
orderBy: [
|
||||
{ id: "asc" },
|
||||
],
|
||||
});
|
||||
|
||||
return { success: true, songs };
|
||||
} catch(error) {
|
||||
console.error("Error in getSongs", error);
|
||||
return { success: false, error: "Error while loading Songs" };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* GetSongById
|
||||
* Returns a song by his ID
|
||||
* @param id - ID indicatin the song
|
||||
* @returns {"success": boolean, "song": {"id":number, "title": string, "songBooks": [{"songNumber": number, "book": {"title": string, "language": string}},...]}}
|
||||
*/
|
||||
export async function getSongById(id: number) {
|
||||
try{
|
||||
const song = await prisma.song.findUnique({
|
||||
where: { id },
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
songBooks: {
|
||||
select: {
|
||||
songNumber: true,
|
||||
book: {
|
||||
select: {
|
||||
title: true,
|
||||
language: true,
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
if (!song) {
|
||||
return { success: false, error: "Song not found" };
|
||||
}
|
||||
|
||||
return { success: true, song };
|
||||
} catch(error) {
|
||||
console.error("Error in getSongById", error);
|
||||
return { success: false, error: "Error while loading Song" };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SearchSongsByTitle
|
||||
* Searches for songs matching the given title query.
|
||||
*
|
||||
* @param query - The search term for the song title.
|
||||
* @param limit - Max number of songs returned for performance (default: 20).
|
||||
* @returns Object with success state and an array of matching songs.
|
||||
*/
|
||||
export async function searchSongsByTitle(query: string, limit: number = 20) {
|
||||
try {
|
||||
// If string is empty
|
||||
if (!query || query.trim() === "") {
|
||||
return { success: true, songs: [] };
|
||||
}
|
||||
|
||||
const songs = await prisma.song.findMany({
|
||||
where: {
|
||||
title: {
|
||||
contains: query.trim(),
|
||||
mode: "insensitive", // Groß-/Kleinschreibung ignorieren
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
songBooks: {
|
||||
select: {
|
||||
songNumber: true,
|
||||
book: {
|
||||
select: {
|
||||
title: true,
|
||||
language: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
take: limit, // limit out
|
||||
});
|
||||
|
||||
return { success: true, songs };
|
||||
} catch (error) {
|
||||
console.error("Error in searchSongsByTitle:", error);
|
||||
return { success: false, error: "Error while searching Songs" };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* CreateSong
|
||||
* Creates an song and returns this song
|
||||
* @param data - object with title
|
||||
* @returns {"success": boolean, "song": {"id":number, "title": string}}
|
||||
*/
|
||||
export async function createSong(data: { title: string }) {
|
||||
try{
|
||||
const song = await prisma.song.create({
|
||||
data: {
|
||||
title: data.title
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
}
|
||||
});
|
||||
|
||||
if (!song) {
|
||||
return { success: false, error: "Could not create song" };
|
||||
}
|
||||
|
||||
return { success: true, song };
|
||||
} catch(error) {
|
||||
console.error("Error in createSong", error);
|
||||
}
|
||||
// P2002 = Unique Constraint Violation (username)
|
||||
if (error.code === "P2002") {
|
||||
return { success: false, error: "Song already exists" };
|
||||
}
|
||||
return { success: false, error: "Error while creating Song" };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* UpdateSong
|
||||
* Updates an existing song and returns the updated song
|
||||
* @param id - ID indicatin the song
|
||||
* @param data - object with optionally username, password and role
|
||||
* @returns {"success": boolean, "song": {"id": number, "title": string}}
|
||||
*/
|
||||
export async function updateSong(
|
||||
id: number,
|
||||
data: { title?: string }
|
||||
) {
|
||||
try {
|
||||
// Prepare update fields dynamically
|
||||
const updateData: { title?: string } = {};
|
||||
|
||||
if (data.title) {
|
||||
updateData.title = data.title;
|
||||
}
|
||||
|
||||
// If no fields to update were provided
|
||||
if (Object.keys(updateData).length === 0) {
|
||||
return { success: false, error: "No fields provided to update" };
|
||||
}
|
||||
|
||||
const song = await prisma.song.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
}
|
||||
});
|
||||
|
||||
return { success: true, song };
|
||||
|
||||
} catch (error) {
|
||||
console.error("Error in updateSong", error);
|
||||
// P2025 = Record to update not found
|
||||
if (error.code === "P2025") {
|
||||
return { success: false, error: "Song not found" };
|
||||
}
|
||||
// P2002 = Unique Constraint Violation (username)
|
||||
if (error.code === "P2002") {
|
||||
return { success: false, error: "Song already exists" };
|
||||
}
|
||||
return { success: false, error: "Error while updating Song" };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* DeleteSong
|
||||
* Deletes an existing song and returns the deleted song
|
||||
* @param id - ID indicatin the song
|
||||
* @returns {"success": boolean, "song": {"id": number, "title": string}}
|
||||
*/
|
||||
export async function deleteSong(id: number) {
|
||||
try {
|
||||
const song = await prisma.song.delete({
|
||||
where: { id },
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
}
|
||||
});
|
||||
|
||||
return { success: true, song };
|
||||
|
||||
} catch (error) {
|
||||
console.error("Error in deleteSong", error);
|
||||
// P2025 = Record to update not found
|
||||
if (error.code === "P2025") {
|
||||
return { success: false, error: "Song not found" };
|
||||
}
|
||||
return { success: false, error: "Error while deleting Song" };
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user