MCPcopy Create free account
hub / github.com/WebDevSimplified/course-platform / updateLesson

Function updateLesson

src/features/lessons/db/lessons.ts:37–78  ·  view source on GitHub ↗
(
  id: string,
  data: Partial<typeof LessonTable.$inferInsert>
)

Source from the content-addressed store, hash-verified

35}
36
37export async function updateLesson(
38 id: string,
39 data: Partial<typeof LessonTable.$inferInsert>
40) {
41 const [updatedLesson, courseId] = await db.transaction(async trx => {
42 const currentLesson = await trx.query.LessonTable.findFirst({
43 where: eq(LessonTable.id, id),
44 columns: { sectionId: true },
45 })
46
47 if (
48 data.sectionId != null &&
49 currentLesson?.sectionId !== data.sectionId &&
50 data.order == null
51 ) {
52 data.order = await getNextCourseLessonOrder(data.sectionId)
53 }
54
55 const [updatedLesson] = await trx
56 .update(LessonTable)
57 .set(data)
58 .where(eq(LessonTable.id, id))
59 .returning()
60 if (updatedLesson == null) {
61 trx.rollback()
62 throw new Error("Failed to update lesson")
63 }
64
65 const section = await trx.query.CourseSectionTable.findFirst({
66 columns: { courseId: true },
67 where: eq(CourseSectionTable.id, updatedLesson.sectionId),
68 })
69
70 if (section == null) return trx.rollback()
71
72 return [updatedLesson, section.courseId]
73 })
74
75 revalidateLessonCache({ courseId, id: updatedLesson.id })
76
77 return updatedLesson
78}
79
80export async function deleteLesson(id: string) {
81 const [deletedLesson, courseId] = await db.transaction(async trx => {

Callers

nothing calls this directly

Calls 2

revalidateLessonCacheFunction · 0.90
getNextCourseLessonOrderFunction · 0.85

Tested by

no test coverage detected