Skip to content

Commit e1f5da5

Browse files
committed
✨ Add and update crud for workbook (#2020)
1 parent 72b06e3 commit e1f5da5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/lib/services/workbooks.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,30 @@ export async function getWorkBook(workBookId: number): Promise<WorkBook | null>
3737
return workBook;
3838
}
3939

40+
/**
41+
* Retrieves a WorkBook from the database by its URL slug.
42+
*
43+
* @param urlSlug - The URL slug identifier for the WorkBook to retrieve (e.g., 'bfs', 'dfs', 'union-find', '2-sat').
44+
* @returns A Promise that resolves to the found WorkBook (with included workBookTasks
45+
* ordered by priority) or null if no WorkBook with the given slug exists
46+
*/
47+
export async function getWorkBookByUrlSlug(urlSlug: string): Promise<WorkBook | null> {
48+
const workBook = await db.workBook.findUnique({
49+
where: {
50+
urlSlug: urlSlug,
51+
},
52+
include: {
53+
workBookTasks: {
54+
orderBy: {
55+
priority: 'asc',
56+
},
57+
},
58+
},
59+
});
60+
61+
return workBook;
62+
}
63+
4064
// See:
4165
// https://www.prisma.io/docs/orm/prisma-schema/data-model/relations#create-a-record-and-nested-records
4266
export async function createWorkBook(workBook: WorkBook): Promise<void> {
@@ -53,6 +77,7 @@ export async function createWorkBook(workBook: WorkBook): Promise<void> {
5377
isOfficial: workBook.isOfficial,
5478
isReplenished: workBook.isReplenished,
5579
workBookType: workBook.workBookType as WorkBookType,
80+
urlSlug: workBook.urlSlug,
5681
workBookTasks: {
5782
create: newWorkBookTasks,
5883
},

0 commit comments

Comments
 (0)