Added UpdateSongBook
This commit is contained in:
1 parent
389fd21975
commit
cb18b0e14f
1 file changed
+53
-1
+53
-1
@@ -78,7 +78,7 @@ export async function getSongBookById(id: number) {
|
||||
* @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) {
|
||||
export async function searchSongBookByTitle(query: string, limit: number = 20) {
|
||||
try {
|
||||
const cleanQuery = query.trim();
|
||||
if (!cleanQuery) {
|
||||
@@ -153,6 +153,58 @@ export async function createSongBook(data: { title: string; language: string })
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* UpdateSongBook
|
||||
* Updates an existing songbook by ID
|
||||
*
|
||||
* @param id - ID of the songbook to update
|
||||
* @param data - object with optional title and language
|
||||
* @returns {"success": boolean, "songBook": {"id": number, "title": string, "language": string}}
|
||||
*/
|
||||
export async function updateSongBook(
|
||||
id: number,
|
||||
data: { title?: string; language?: string }
|
||||
) {
|
||||
try {
|
||||
// Prepare update fields dynamically
|
||||
const updateData: { title?: string; language?: string } = {};
|
||||
|
||||
if (data.title) {
|
||||
updateData.title = data.title;
|
||||
}
|
||||
|
||||
if (data.language) {
|
||||
updateData.language = data.language;
|
||||
}
|
||||
|
||||
// If no fields to update were provided
|
||||
if (Object.keys(updateData).length === 0) {
|
||||
return { success: false, error: "No fields provided to update" };
|
||||
}
|
||||
const songBook = await prisma.songBook.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
language: true,
|
||||
},
|
||||
});
|
||||
|
||||
return { success: true, songBook };
|
||||
} catch (error: any) {
|
||||
if (error.code === "P2025") {
|
||||
return { success: false, error: "SongBook not found" };
|
||||
}
|
||||
if (error.code === "P2002") {
|
||||
return { success: false, error: "SongBook title already exists" };
|
||||
}
|
||||
|
||||
console.error("Error in updateSongBook:", error);
|
||||
return { success: false, error: "Error while updating SongBook" };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* DeleteSongBook
|
||||
* Deletes a songbook by its ID and returns it
|
||||
|
||||
Reference in new issue
Block a user