Module Update
This commit is contained in:
226
src/modules/textbooks/data-access.ts
Normal file
226
src/modules/textbooks/data-access.ts
Normal file
@@ -0,0 +1,226 @@
|
||||
import { Textbook, Chapter, CreateTextbookInput, CreateChapterInput, UpdateChapterContentInput, KnowledgePoint, CreateKnowledgePointInput, UpdateTextbookInput } from "./types";
|
||||
|
||||
// Mock Data (Moved from data/mock-data.ts and enhanced)
|
||||
let MOCK_TEXTBOOKS: Textbook[] = [
|
||||
// ... (previous textbooks remain same, keeping for brevity)
|
||||
{
|
||||
id: "tb_01",
|
||||
title: "Advanced Mathematics Grade 10",
|
||||
subject: "Mathematics",
|
||||
grade: "Grade 10",
|
||||
publisher: "Next Education Press",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
_count: { chapters: 12 },
|
||||
},
|
||||
// ... (other textbooks)
|
||||
];
|
||||
|
||||
let MOCK_CHAPTERS: Chapter[] = [
|
||||
// ... (previous chapters)
|
||||
{
|
||||
id: "ch_01",
|
||||
textbookId: "tb_01",
|
||||
title: "Chapter 1: Real Numbers",
|
||||
order: 1,
|
||||
parentId: null,
|
||||
content: "# Chapter 1: Real Numbers\n\nIn this chapter, we will explore the properties of real numbers...",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
children: [
|
||||
{
|
||||
id: "ch_01_01",
|
||||
textbookId: "tb_01",
|
||||
title: "1.1 Introduction to Real Numbers",
|
||||
order: 1,
|
||||
parentId: "ch_01",
|
||||
content: "## 1.1 Introduction\n\nReal numbers include rational and irrational numbers.",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
let MOCK_KNOWLEDGE_POINTS: KnowledgePoint[] = [
|
||||
{
|
||||
id: "kp_01",
|
||||
name: "Real Numbers",
|
||||
description: "Definition and properties of real numbers",
|
||||
level: 1,
|
||||
order: 1,
|
||||
chapterId: "ch_01",
|
||||
},
|
||||
{
|
||||
id: "kp_02",
|
||||
name: "Rational Numbers",
|
||||
description: "Numbers that can be expressed as a fraction",
|
||||
level: 2,
|
||||
order: 1,
|
||||
chapterId: "ch_01_01",
|
||||
}
|
||||
];
|
||||
|
||||
// ... (existing imports and mock data)
|
||||
|
||||
export async function getTextbooks(query?: string, subject?: string, grade?: string): Promise<Textbook[]> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
let results = [...MOCK_TEXTBOOKS];
|
||||
// ... (filtering logic)
|
||||
return results;
|
||||
}
|
||||
|
||||
export async function getTextbookById(id: string): Promise<Textbook | undefined> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||
return MOCK_TEXTBOOKS.find((t) => t.id === id);
|
||||
}
|
||||
|
||||
export async function getChaptersByTextbookId(textbookId: string): Promise<Chapter[]> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||
return MOCK_CHAPTERS.filter((c) => c.textbookId === textbookId);
|
||||
}
|
||||
|
||||
export async function createTextbook(data: CreateTextbookInput): Promise<Textbook> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 800));
|
||||
const newTextbook: Textbook = {
|
||||
id: `tb_${Math.random().toString(36).substr(2, 9)}`,
|
||||
...data,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
_count: { chapters: 0 },
|
||||
};
|
||||
MOCK_TEXTBOOKS = [newTextbook, ...MOCK_TEXTBOOKS];
|
||||
return newTextbook;
|
||||
}
|
||||
|
||||
export async function updateTextbook(data: UpdateTextbookInput): Promise<Textbook> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 800));
|
||||
|
||||
const index = MOCK_TEXTBOOKS.findIndex((t) => t.id === data.id);
|
||||
if (index === -1) throw new Error("Textbook not found");
|
||||
|
||||
const updatedTextbook = {
|
||||
...MOCK_TEXTBOOKS[index],
|
||||
...data,
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
MOCK_TEXTBOOKS[index] = updatedTextbook;
|
||||
return updatedTextbook;
|
||||
}
|
||||
|
||||
export async function deleteTextbook(id: string): Promise<void> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 800));
|
||||
MOCK_TEXTBOOKS = MOCK_TEXTBOOKS.filter((t) => t.id !== id);
|
||||
}
|
||||
|
||||
// ... (rest of the file)
|
||||
|
||||
export async function createChapter(data: CreateChapterInput): Promise<Chapter> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
|
||||
const newChapter: Chapter = {
|
||||
id: `ch_${Math.random().toString(36).substr(2, 9)}`,
|
||||
textbookId: data.textbookId,
|
||||
title: data.title,
|
||||
order: data.order || 0,
|
||||
parentId: data.parentId || null,
|
||||
content: "",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
children: []
|
||||
};
|
||||
|
||||
// Logic to add to nested structure (simplified for mock: add to root or find parent)
|
||||
// For deep nesting in mock, we'd need recursive search.
|
||||
// Here we just push to root or try to find parent in top level for simplicity of demo.
|
||||
|
||||
if (data.parentId) {
|
||||
const parent = MOCK_CHAPTERS.find(c => c.id === data.parentId);
|
||||
if (parent) {
|
||||
if (!parent.children) parent.children = [];
|
||||
parent.children.push(newChapter);
|
||||
} else {
|
||||
// Try searching one level deep
|
||||
for (const ch of MOCK_CHAPTERS) {
|
||||
if (ch.children) {
|
||||
const subParent = ch.children.find(c => c.id === data.parentId);
|
||||
if (subParent) {
|
||||
if (!subParent.children) subParent.children = [];
|
||||
subParent.children.push(newChapter);
|
||||
return newChapter;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
MOCK_CHAPTERS.push(newChapter);
|
||||
}
|
||||
|
||||
return newChapter;
|
||||
}
|
||||
|
||||
export async function updateChapterContent(data: UpdateChapterContentInput): Promise<Chapter> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
|
||||
// Recursive find and update
|
||||
const updateContentRecursive = (chapters: Chapter[]): Chapter | null => {
|
||||
for (const ch of chapters) {
|
||||
if (ch.id === data.chapterId) {
|
||||
ch.content = data.content;
|
||||
ch.updatedAt = new Date();
|
||||
return ch;
|
||||
}
|
||||
if (ch.children) {
|
||||
const found = updateContentRecursive(ch.children);
|
||||
if (found) return found;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const updated = updateContentRecursive(MOCK_CHAPTERS);
|
||||
if (!updated) throw new Error("Chapter not found");
|
||||
|
||||
return updated;
|
||||
}
|
||||
|
||||
export async function deleteChapter(id: string): Promise<void> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
|
||||
// Recursive delete
|
||||
MOCK_CHAPTERS = MOCK_CHAPTERS.filter(c => c.id !== id);
|
||||
MOCK_CHAPTERS.forEach(c => {
|
||||
if (c.children) {
|
||||
c.children = c.children.filter(child => child.id !== id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Knowledge Points
|
||||
|
||||
export async function getKnowledgePointsByChapterId(chapterId: string): Promise<KnowledgePoint[]> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||
return MOCK_KNOWLEDGE_POINTS.filter(kp => kp.chapterId === chapterId);
|
||||
}
|
||||
|
||||
export async function createKnowledgePoint(data: CreateKnowledgePointInput): Promise<KnowledgePoint> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
|
||||
const newKP: KnowledgePoint = {
|
||||
id: `kp_${Math.random().toString(36).substr(2, 9)}`,
|
||||
name: data.name,
|
||||
description: data.description,
|
||||
chapterId: data.chapterId,
|
||||
level: 1, // simplified
|
||||
order: 0
|
||||
};
|
||||
|
||||
MOCK_KNOWLEDGE_POINTS.push(newKP);
|
||||
return newKP;
|
||||
}
|
||||
|
||||
export async function deleteKnowledgePoint(id: string): Promise<void> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
MOCK_KNOWLEDGE_POINTS = MOCK_KNOWLEDGE_POINTS.filter(kp => kp.id !== id);
|
||||
}
|
||||
Reference in New Issue
Block a user