fix(lessons): cascade delete descendants in service (no FK on parent_id)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { eq, isNull, sql } from 'drizzle-orm';
|
||||
import { eq, inArray, isNull, sql } from 'drizzle-orm';
|
||||
import type { Db } from '../db/client.js';
|
||||
import { cards, lessons } from '../db/schema.js';
|
||||
import { ApiError } from '../lib/errors.js';
|
||||
@@ -60,8 +60,10 @@ export async function updateLesson(db: Db, id: number, input: LessonUpdateInput)
|
||||
}
|
||||
|
||||
export async function deleteLesson(db: Db, id: number): Promise<void> {
|
||||
const r = db.delete(lessons).where(eq(lessons.id, id)).run();
|
||||
if (r.changes === 0) throw ApiError.notFound('Lesson');
|
||||
const existing = db.select().from(lessons).where(eq(lessons.id, id)).get();
|
||||
if (!existing) throw ApiError.notFound('Lesson');
|
||||
const ids = await getDescendantLessonIds(db, id);
|
||||
db.delete(lessons).where(inArray(lessons.id, ids)).run();
|
||||
}
|
||||
|
||||
export async function moveLesson(db: Db, id: number, input: LessonMoveInput): Promise<Lesson> {
|
||||
|
||||
Reference in New Issue
Block a user