import { eq, and } from "drizzle-orm"; import { getDb } from "../config/database.js"; import { lessonPlans, type LessonPlan, type NewLessonPlan, } from "./lesson-plans.schema.js"; export class LessonPlansRepository { async findById(id: string): Promise { const [result] = await getDb() .select() .from(lessonPlans) .where(eq(lessonPlans.id, id)) .limit(1); return result; } async findByTeacher(query: { teacherId: string; classId?: string; subjectId?: string; }): Promise { const db = getDb(); const conditions = [eq(lessonPlans.teacherId, query.teacherId)]; if (query.classId) { conditions.push(eq(lessonPlans.classId, query.classId)); } if (query.subjectId) { conditions.push(eq(lessonPlans.subjectId, query.subjectId)); } return db .select() .from(lessonPlans) .where(and(...conditions)); } async findByStudent(query: { studentId: string; classId?: string; }): Promise { // 学生视角:按班级查询教师发布的备课计划 // studentId 不直接关联备课计划(备课计划是教师创建的), // 通过 classId 过滤学生所在班级的备课计划 const db = getDb(); const conditions = [eq(lessonPlans.status, "published")]; if (query.classId) { conditions.push(eq(lessonPlans.classId, query.classId)); } return db .select() .from(lessonPlans) .where(and(...conditions)); } async create(data: NewLessonPlan): Promise { await getDb().insert(lessonPlans).values(data); } async update(id: string, data: Partial): Promise { await getDb().update(lessonPlans).set(data).where(eq(lessonPlans.id, id)); } async delete(id: string): Promise { await getDb().delete(lessonPlans).where(eq(lessonPlans.id, id)); } } export const lessonPlansRepository = new LessonPlansRepository();