mirror of
https://github.com/SECTL/SecScore.git
synced 2026-07-21 18:19:03 +08:00
feat:修复了亿点点bug
This commit is contained in:
@@ -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)
|
||||
})()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user