包含 classes/exams/homework/grades/attendance/scheduling 域、outbox、iam-consumer、redis 配置等完整实现
48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
import { eq, inArray } from "drizzle-orm";
|
|
import { db } from "../config/database.js";
|
|
import { classes, type Class } from "./classes.schema.js";
|
|
import { teacherAssociations } from "./teacher-associations.schema.js";
|
|
|
|
export class ClassesRepository {
|
|
async findById(id: string): Promise<Class | undefined> {
|
|
const [result] = await db
|
|
.select()
|
|
.from(classes)
|
|
.where(eq(classes.id, id))
|
|
.limit(1);
|
|
return result;
|
|
}
|
|
|
|
async findByIds(ids: string[]): Promise<Class[]> {
|
|
if (ids.length === 0) return [];
|
|
return db.select().from(classes).where(inArray(classes.id, ids));
|
|
}
|
|
|
|
async findByTeacherId(teacherId: string): Promise<Class[]> {
|
|
// Via teacher associations
|
|
const associations = await db
|
|
.select({ classId: teacherAssociations.classId })
|
|
.from(teacherAssociations)
|
|
.where(eq(teacherAssociations.teacherId, teacherId));
|
|
const classIds = [...new Set(associations.map((a) => a.classId))];
|
|
if (classIds.length === 0) return [];
|
|
return db.select().from(classes).where(inArray(classes.id, classIds));
|
|
}
|
|
|
|
async create(data: {
|
|
id: string;
|
|
name: string;
|
|
gradeId: string;
|
|
headTeacherId?: string;
|
|
description?: string;
|
|
}): Promise<void> {
|
|
await db.insert(classes).values(data);
|
|
}
|
|
|
|
async update(id: string, data: Partial<Class>): Promise<void> {
|
|
await db.update(classes).set(data).where(eq(classes.id, id));
|
|
}
|
|
}
|
|
|
|
export const classesRepository = new ClassesRepository();
|