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