fix: rubric:storeData 空保护不覆盖缓存 & getData 回退 DB
This commit is contained in:
+21
-17
@@ -255,11 +255,7 @@ export function setupIpc(win: BrowserWindow): void {
|
||||
ipcMain.handle('rubric:storeData', (_event, data: any) => {
|
||||
const d = getDb()
|
||||
const questions = data?.questions || []
|
||||
// Don't clear rubric data when saving empty data (preserve existing)
|
||||
if (questions.length === 0) {
|
||||
rubricPayload = data
|
||||
return
|
||||
}
|
||||
if (questions.length === 0) return
|
||||
d.prepare('DELETE FROM rubric').run()
|
||||
const insert = d.prepare('INSERT INTO rubric (question_title, max_score, reference_answer, rubric, sort_order) VALUES (?, ?, ?, ?, ?)')
|
||||
for (let i = 0; i < questions.length; i++) {
|
||||
@@ -270,19 +266,27 @@ export function setupIpc(win: BrowserWindow): void {
|
||||
})
|
||||
|
||||
ipcMain.handle('rubric:getData', () => {
|
||||
if (rubricPayload) return rubricPayload
|
||||
const d = getDb()
|
||||
const rows: any[] = d.prepare('SELECT * FROM rubric ORDER BY sort_order').all()
|
||||
if (rows.length) {
|
||||
return {
|
||||
questions: rows.map(r => ({
|
||||
questionTitle: r.question_title,
|
||||
maxScore: r.max_score,
|
||||
referenceAnswer: r.reference_answer,
|
||||
rubric: r.rubric
|
||||
})),
|
||||
currentIndex: 0
|
||||
function fromDb() {
|
||||
const d = getDb()
|
||||
const rows: any[] = d.prepare('SELECT * FROM rubric ORDER BY sort_order').all()
|
||||
if (rows.length) {
|
||||
return {
|
||||
questions: rows.map(r => ({
|
||||
questionTitle: r.question_title,
|
||||
maxScore: r.max_score,
|
||||
referenceAnswer: r.reference_answer,
|
||||
rubric: r.rubric
|
||||
})),
|
||||
currentIndex: 0
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
if (rubricPayload && rubricPayload.questions?.length) return rubricPayload
|
||||
const dbResult = fromDb()
|
||||
if (dbResult) {
|
||||
rubricPayload = dbResult
|
||||
return dbResult
|
||||
}
|
||||
return null
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user