feat:修复了亿点点bug

This commit is contained in:
Fox_block
2026-01-14 19:27:51 +08:00
parent ae08ec45b0
commit f27e2f21bb
33 changed files with 3161 additions and 935 deletions
+28 -20
View File
@@ -1,43 +1,51 @@
import { Database } from 'better-sqlite3';
import { Database } from 'better-sqlite3'
export interface Student {
id: number;
name: string;
score: number;
extra_json?: string;
id: number
name: string
score: number
extra_json?: string
}
export class StudentRepository {
constructor(private db: Database) {}
findAll() {
return this.db.prepare('SELECT * FROM students ORDER BY score DESC, name ASC').all() as Student[];
return this.db
.prepare('SELECT * FROM students ORDER BY score DESC, name ASC')
.all() as Student[]
}
create(student: { name: string }) {
const info = this.db.prepare(
'INSERT INTO students (name) VALUES (?)'
).run(student.name);
return info.lastInsertRowid as number;
const info = this.db.prepare('INSERT INTO students (name) VALUES (?)').run(student.name)
return info.lastInsertRowid as number
}
update(id: number, student: Partial<Student>) {
const sets: string[] = [];
const vals: any[] = [];
const sets: string[] = []
const vals: any[] = []
Object.entries(student).forEach(([key, val]) => {
if (key !== 'id') {
sets.push(`${key} = ?`);
vals.push(val);
sets.push(`${key} = ?`)
vals.push(val)
}
});
vals.push(id);
this.db.prepare(`UPDATE students SET ${sets.join(', ')}, updated_at = CURRENT_TIMESTAMP WHERE id = ?`).run(...vals);
})
vals.push(id)
this.db
.prepare(
`UPDATE students SET ${sets.join(', ')}, updated_at = CURRENT_TIMESTAMP WHERE id = ?`
)
.run(...vals)
}
delete(id: number) {
this.db.transaction(() => {
this.db.prepare('DELETE FROM score_events WHERE student_id = ?').run(id);
this.db.prepare('DELETE FROM students WHERE id = ?').run(id);
})();
this.db
.prepare(
'DELETE FROM score_events WHERE student_name IN (SELECT name FROM students WHERE id = ?)'
)
.run(id)
this.db.prepare('DELETE FROM students WHERE id = ?').run(id)
})()
}
}