Skip to content

Commit b9dd3b3

Browse files
committed
🐛 Add validation for crud (#2020)
1 parent 7f95e95 commit b9dd3b3

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/lib/services/workbooks.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ export async function getWorkBookByUrlSlug(urlSlug: string): Promise<WorkBook |
6464
// See:
6565
// https://www.prisma.io/docs/orm/prisma-schema/data-model/relations#create-a-record-and-nested-records
6666
export async function createWorkBook(workBook: WorkBook): Promise<void> {
67+
const slug = workBook.urlSlug as string;
68+
69+
if (await isExistingUrlSlug(slug)) {
70+
throw new Error(`WorkBook slug ${slug} has already existed`);
71+
}
72+
6773
const sanitizedUrl = sanitizeUrl(workBook.editorialUrl);
6874
const newWorkBookTasks: WorkBookTasksBase = await getWorkBookTasks(workBook);
6975

@@ -90,6 +96,10 @@ export async function createWorkBook(workBook: WorkBook): Promise<void> {
9096
console.log(`Created workbook with title: ${newWorkBook.title}`);
9197
}
9298

99+
async function isExistingUrlSlug(slug: string): Promise<boolean> {
100+
return (await getWorkBookByUrlSlug(slug)) ? true : false;
101+
}
102+
93103
async function isExistingWorkBook(workBookId: number): Promise<boolean> {
94104
const workBook = await getWorkBook(workBookId);
95105

0 commit comments

Comments
 (0)