- database.ts 导出db常量替代getDb()函数 - env.ts JWT_SECRET/ES_URL/NEO4J_URL改optional加DEV_MODE - neo4j.ts driver惰性创建+try/catch+connectionTimeout:3000 - health/lifecycle改用Drizzle原生查询 - textbooks.schema修复integer到int+导出NewTextbook类型 - 新建chapters/knowledge-points/questions三模块CRUD - knowledge-points含Neo4j前置依赖图非阻塞查询 - content-init.sql创建4张表 端到端验证: textbooks/chapters/knowledge-points/questions全CRUD通过
71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { eq } from "drizzle-orm";
|
|
import { Injectable } from "@nestjs/common";
|
|
import { db } from "../config/database.js";
|
|
import { textbooks, type Textbook } from "./textbooks.schema.js";
|
|
import {
|
|
NotFoundError,
|
|
ValidationError,
|
|
} from "../shared/errors/application-error.js";
|
|
|
|
export interface CreateTextbookInput {
|
|
title: string;
|
|
subjectId: string;
|
|
gradeId: string;
|
|
version: string;
|
|
}
|
|
|
|
export interface UpdateTextbookInput {
|
|
title?: string;
|
|
subjectId?: string;
|
|
gradeId?: string;
|
|
version?: string;
|
|
}
|
|
|
|
@Injectable()
|
|
export class TextbooksService {
|
|
async create(input: CreateTextbookInput): Promise<{ id: string }> {
|
|
if (!input.title || !input.subjectId || !input.gradeId || !input.version) {
|
|
throw new ValidationError(
|
|
"title, subjectId, gradeId, version are required",
|
|
);
|
|
}
|
|
|
|
const id = randomUUID();
|
|
await db.insert(textbooks).values({
|
|
id,
|
|
title: input.title,
|
|
subjectId: input.subjectId,
|
|
gradeId: input.gradeId,
|
|
version: input.version,
|
|
});
|
|
return { id };
|
|
}
|
|
|
|
async list(): Promise<Textbook[]> {
|
|
return db.select().from(textbooks);
|
|
}
|
|
|
|
async getById(id: string): Promise<Textbook> {
|
|
const [result] = await db
|
|
.select()
|
|
.from(textbooks)
|
|
.where(eq(textbooks.id, id))
|
|
.limit(1);
|
|
if (!result) {
|
|
throw new NotFoundError("Textbook", id);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
async update(id: string, data: UpdateTextbookInput): Promise<void> {
|
|
await this.getById(id);
|
|
await db.update(textbooks).set(data).where(eq(textbooks.id, id));
|
|
}
|
|
|
|
async delete(id: string): Promise<void> {
|
|
await this.getById(id);
|
|
await db.delete(textbooks).where(eq(textbooks.id, id));
|
|
}
|
|
}
|