( id: string, data: Partial<typeof LessonTable.$inferInsert> )
| 35 | } |
| 36 | |
| 37 | export 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 | |
| 80 | export async function deleteLesson(id: string) { |
| 81 | const [deletedLesson, courseId] = await db.transaction(async trx => { |
nothing calls this directly
no test coverage detected