init
This commit is contained in:
commit
5577214005
22 changed files with 4997 additions and 0 deletions
127
src/services/db.ts
Normal file
127
src/services/db.ts
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { DatabaseSchema, StudentStore, Assignment } from '../types';
|
||||
|
||||
const DB_FILE = path.resolve(process.cwd(), 'data', 'db.json');
|
||||
|
||||
export class LocalDatabase {
|
||||
private static instance: LocalDatabase;
|
||||
private cache: DatabaseSchema = { students: {} };
|
||||
private initialized = false;
|
||||
|
||||
private constructor() {
|
||||
this.ensureDbExists();
|
||||
this.load();
|
||||
}
|
||||
|
||||
public static getInstance(): LocalDatabase {
|
||||
if (!LocalDatabase.instance) {
|
||||
LocalDatabase.instance = new LocalDatabase();
|
||||
}
|
||||
return LocalDatabase.instance;
|
||||
}
|
||||
|
||||
private ensureDbExists(): void {
|
||||
const dir = path.dirname(DB_FILE);
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
if (!fs.existsSync(DB_FILE)) {
|
||||
const initial: DatabaseSchema = { students: {} };
|
||||
fs.writeFileSync(DB_FILE, JSON.stringify(initial, null, 2), 'utf-8');
|
||||
}
|
||||
}
|
||||
|
||||
private load(): void {
|
||||
try {
|
||||
if (fs.existsSync(DB_FILE)) {
|
||||
const raw = fs.readFileSync(DB_FILE, 'utf-8');
|
||||
this.cache = JSON.parse(raw) as DatabaseSchema;
|
||||
if (!this.cache.students) {
|
||||
this.cache.students = {};
|
||||
}
|
||||
}
|
||||
this.initialized = true;
|
||||
} catch (err) {
|
||||
console.error('Error loading database file, initializing empty:', err);
|
||||
this.cache = { students: {} };
|
||||
}
|
||||
}
|
||||
|
||||
private save(): void {
|
||||
try {
|
||||
const tmpFile = `${DB_FILE}.tmp`;
|
||||
fs.writeFileSync(tmpFile, JSON.stringify(this.cache, null, 2), 'utf-8');
|
||||
fs.renameSync(tmpFile, DB_FILE);
|
||||
} catch (err) {
|
||||
console.error('Error saving database:', err);
|
||||
}
|
||||
}
|
||||
|
||||
public getStudentStore(studentId: string): StudentStore {
|
||||
if (!this.cache.students[studentId]) {
|
||||
this.cache.students[studentId] = {
|
||||
studentId,
|
||||
lastSynced: null,
|
||||
assignments: {}
|
||||
};
|
||||
this.save();
|
||||
}
|
||||
return this.cache.students[studentId];
|
||||
}
|
||||
|
||||
public mergeAssignments(studentId: string, newAssignments: Assignment[]): StudentStore {
|
||||
const student = this.getStudentStore(studentId);
|
||||
student.lastSynced = new Date().toISOString();
|
||||
|
||||
for (const item of newAssignments) {
|
||||
const existing = student.assignments[item.id];
|
||||
if (existing) {
|
||||
// Preserve user completion status and completion timestamp
|
||||
student.assignments[item.id] = {
|
||||
...item,
|
||||
completed: existing.completed,
|
||||
completedAt: existing.completedAt
|
||||
};
|
||||
} else {
|
||||
student.assignments[item.id] = item;
|
||||
}
|
||||
}
|
||||
|
||||
this.save();
|
||||
return student;
|
||||
}
|
||||
|
||||
public toggleAssignment(studentId: string, assignmentId: string, completed?: boolean): Assignment | null {
|
||||
const student = this.getStudentStore(studentId);
|
||||
const assignment = student.assignments[assignmentId];
|
||||
if (!assignment) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const newCompleted = completed !== undefined ? completed : !assignment.completed;
|
||||
assignment.completed = newCompleted;
|
||||
assignment.completedAt = newCompleted ? new Date().toISOString() : null;
|
||||
assignment.status = newCompleted ? 'completed' : assignment.status;
|
||||
|
||||
this.save();
|
||||
return assignment;
|
||||
}
|
||||
|
||||
public saveCredentials(studentId: string, password: string): void {
|
||||
const student = this.getStudentStore(studentId);
|
||||
student.rememberedCredentials = { studentId, password };
|
||||
this.save();
|
||||
}
|
||||
|
||||
public clearCredentials(studentId: string): void {
|
||||
const student = this.getStudentStore(studentId);
|
||||
delete student.rememberedCredentials;
|
||||
this.save();
|
||||
}
|
||||
|
||||
public getRememberedCredentials(studentId: string): { studentId: string; password: string } | null {
|
||||
const student = this.cache.students[studentId];
|
||||
return student?.rememberedCredentials || null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue