389 lines
11 KiB
JavaScript
389 lines
11 KiB
JavaScript
import {
|
||
addQuestion,
|
||
getAllQuestions,
|
||
getQuestionById,
|
||
updateQuestion,
|
||
deleteQuestion,
|
||
getAllQuestionsWithRelations,
|
||
updateQuestionDescription,
|
||
addChoiceQuestion,
|
||
updateChoiceQuestion, // 添加这一行
|
||
addFillBlankQuestion,
|
||
updateFillBlankQuestion,
|
||
deleteFillBlankQuestion,
|
||
getFillBlankQuestionsByQuestionId,
|
||
getQuestionsCountAndScore
|
||
} from '../db/question.js';
|
||
|
||
// 导入configService中的increaseQuestionBankVersion方法
|
||
import {
|
||
increaseQuestionBankVersion
|
||
} from './configService.js';
|
||
|
||
/**
|
||
* 服务层:添加题干
|
||
* @param {Object} questionData - 题干数据
|
||
* @returns {Promise<number>} 新创建的题干ID
|
||
*/
|
||
export async function createQuestion(questionData) {
|
||
try {
|
||
const result = await addQuestion(questionData);
|
||
// 调用增加题库版本号的方法
|
||
await increaseQuestionBankVersion();
|
||
return result;
|
||
} catch (error) {
|
||
console.error('服务层: 添加题干失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:获取所有题干
|
||
* @returns {Promise<Array>} 题干列表
|
||
*/
|
||
export async function fetchAllQuestions() {
|
||
try {
|
||
return await getAllQuestions();
|
||
} catch (error) {
|
||
console.error('服务层: 获取所有题干失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:获取所有题干及其关联信息
|
||
* @returns {Promise<Array>} 包含关联信息的题干列表
|
||
*/
|
||
export async function fetchAllQuestionsWithRelations() {
|
||
try {
|
||
return await getAllQuestionsWithRelations();
|
||
} catch (error) {
|
||
console.error('服务层: 获取所有题干及其关联信息失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:根据ID获取题干
|
||
* @param {number} id - 题干ID
|
||
* @returns {Promise<Object|null>} 题干详情
|
||
*/
|
||
export async function fetchQuestionById(id) {
|
||
try {
|
||
return await getQuestionById(id);
|
||
} catch (error) {
|
||
console.error('服务层: 根据ID获取题干失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:更新题干
|
||
* @param {number} id - 题干ID
|
||
* @param {Object} questionData - 题干数据
|
||
* @returns {Promise<boolean>} 是否更新成功
|
||
*/
|
||
export async function modifyQuestion(id, questionData) {
|
||
try {
|
||
const result = await updateQuestion(id, questionData);
|
||
// 调用增加题库版本号的方法
|
||
await increaseQuestionBankVersion();
|
||
return result;
|
||
} catch (error) {
|
||
console.error('服务层: 更新题干失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:更新题干描述
|
||
* @param {number} id - 题干ID
|
||
* @param {string} questionDescription - 新的题干描述
|
||
* @returns {Promise<boolean>} 是否更新成功
|
||
*/
|
||
export async function modifyQuestionDescription(id, questionDescription) {
|
||
try {
|
||
const result = await updateQuestionDescription(id, questionDescription);
|
||
// 调用增加题库版本号的方法
|
||
await increaseQuestionBankVersion();
|
||
return result;
|
||
} catch (error) {
|
||
console.error('服务层: 更新题干描述失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:删除题干
|
||
* @param {number} id - 题干ID
|
||
* @returns {Promise<boolean>} 是否删除成功
|
||
*/
|
||
export async function removeQuestion(id) {
|
||
try {
|
||
const result = await deleteQuestion(id);
|
||
// 调用增加题库版本号的方法
|
||
await increaseQuestionBankVersion();
|
||
return result;
|
||
} catch (error) {
|
||
console.error('服务层: 删除题干失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:添加选择题问题
|
||
* @param {Object} choiceData - 选择题数据
|
||
* @returns {Promise<number>} 新创建的选择题ID
|
||
*/
|
||
export async function createChoiceQuestion(choiceData) {
|
||
try {
|
||
const result = await addChoiceQuestion(choiceData);
|
||
// 调用增加题库版本号的方法
|
||
await increaseQuestionBankVersion();
|
||
return result;
|
||
} catch (error) {
|
||
console.error('服务层: 添加选择题失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:添加选择题问题
|
||
* @param {number} id - 选择题ID
|
||
* @param {Object} choiceData - 选择题数据
|
||
* @returns {Promise<boolean>} 是否更新成功
|
||
*/
|
||
export async function modifyChoiceQuestion(id, choiceData) {
|
||
try {
|
||
const result = await updateChoiceQuestion(id, choiceData);
|
||
// 调用增加题库版本号的方法
|
||
await increaseQuestionBankVersion();
|
||
return result;
|
||
} catch (error) {
|
||
console.error('服务层: 更新选择题失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:添加填空题问题
|
||
* @param {Object} fillBlankData - 填空题数据
|
||
* @returns {Promise<number>} 新创建的填空题ID
|
||
*/
|
||
export async function createFillBlankQuestion(fillBlankData) {
|
||
try {
|
||
const result = await addFillBlankQuestion(fillBlankData);
|
||
// 调用增加题库版本号的方法
|
||
await increaseQuestionBankVersion();
|
||
return result;
|
||
} catch (error) {
|
||
console.error('服务层: 添加填空题失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:更新填空题问题
|
||
* @param {number} id - 填空题ID
|
||
* @param {Object} fillBlankData - 填空题数据
|
||
* @returns {Promise<boolean>} 是否更新成功
|
||
*/
|
||
export async function modifyFillBlankQuestion(id, fillBlankData) {
|
||
try {
|
||
const result = await updateFillBlankQuestion(id, fillBlankData);
|
||
// 调用增加题库版本号的方法
|
||
await increaseQuestionBankVersion();
|
||
return result;
|
||
} catch (error) {
|
||
console.error('服务层: 更新填空题失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:删除填空题问题
|
||
* @param {number} id - 填空题ID
|
||
* @returns {Promise<boolean>} 是否删除成功
|
||
*/
|
||
export async function removeFillBlankQuestion(id) {
|
||
try {
|
||
const result = await deleteFillBlankQuestion(id);
|
||
// 调用增加题库版本号的方法
|
||
await increaseQuestionBankVersion();
|
||
return result;
|
||
} catch (error) {
|
||
console.error('服务层: 删除填空题失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:根据题干ID查询填空题问题
|
||
* @param {number} questionId - 题干ID
|
||
* @returns {Promise<Array>} 填空题列表
|
||
*/
|
||
export async function fetchFillBlankQuestionsByQuestionId(questionId) {
|
||
try {
|
||
return await getFillBlankQuestionsByQuestionId(questionId);
|
||
} catch (error) {
|
||
console.error('服务层: 根据题干ID查询填空题失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:查询试题总数和总分
|
||
* @returns {Promise<{totalQuestions: number, totalScore: number}>} 包含试题总数和总分的对象
|
||
*/
|
||
export async function fetchQuestionsCountAndScore() {
|
||
try {
|
||
return await getQuestionsCountAndScore();
|
||
} catch (error) {
|
||
console.error('服务层: 查询试题总数和总分失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 初始化试题相关IPC通信
|
||
* @param {Electron.IpcMain} ipcMain - IPC主进程实例
|
||
*/
|
||
export async function initQuestionIpc(ipcMain) {
|
||
// 题干管理相关IPC
|
||
ipcMain.handle("question-create", async (event, questionData) => {
|
||
try {
|
||
return await createQuestion(questionData);
|
||
} catch (error) {
|
||
console.error("Failed to create question:", error);
|
||
throw error;
|
||
}
|
||
});
|
||
|
||
ipcMain.handle("question-fetch-all", async () => {
|
||
try {
|
||
return await fetchAllQuestions();
|
||
} catch (error) {
|
||
console.error("Failed to fetch questions:", error);
|
||
throw error;
|
||
}
|
||
});
|
||
|
||
ipcMain.handle("question-fetch-by-id", async (event, id) => {
|
||
try {
|
||
return await fetchQuestionById(id);
|
||
} catch (error) {
|
||
console.error(`Failed to fetch question by id ${id}:`, error);
|
||
throw error;
|
||
}
|
||
});
|
||
|
||
ipcMain.handle("question-update", async (event, id, questionData) => {
|
||
try {
|
||
return await modifyQuestion(id, questionData);
|
||
} catch (error) {
|
||
console.error("Failed to update question:", error);
|
||
throw error;
|
||
}
|
||
});
|
||
|
||
ipcMain.handle("question-delete", async (event, id) => {
|
||
try {
|
||
return await removeQuestion(id);
|
||
} catch (error) {
|
||
console.error(`Failed to delete question ${id}:`, error);
|
||
throw error;
|
||
}
|
||
});
|
||
|
||
// 在已有的 question 相关 IPC 处理程序区域添加
|
||
ipcMain.handle("question-fetch-all-with-relations", async (event) => {
|
||
try {
|
||
return await fetchAllQuestionsWithRelations();
|
||
} catch (error) {
|
||
console.error("Failed to fetch all questions with relations:", error);
|
||
throw error;
|
||
}
|
||
});
|
||
|
||
// 添加更新题干描述的 IPC 处理程序
|
||
ipcMain.handle("question-update-description", async (event, id, questionDescription) => {
|
||
try {
|
||
return await modifyQuestionDescription(id, questionDescription);
|
||
} catch (error) {
|
||
console.error("Failed to update question description:", error);
|
||
throw error;
|
||
}
|
||
}
|
||
);
|
||
|
||
// 添加选择题问题的IPC处理程序
|
||
ipcMain.handle('question-update-choice', async (event, id, choiceData) => {
|
||
try {
|
||
return await modifyChoiceQuestion(id, choiceData);
|
||
} catch (error) {
|
||
console.error('Failed to update choice question:', error);
|
||
throw error;
|
||
}
|
||
});
|
||
|
||
// 添加选择题问题的IPC处理程序
|
||
ipcMain.handle("question-create-choice", async (event, choiceData) => {
|
||
try {
|
||
return await createChoiceQuestion(choiceData);
|
||
} catch (error) {
|
||
console.error("Failed to create choice question:", error);
|
||
throw error;
|
||
}
|
||
});
|
||
|
||
// 添加填空题问题的IPC处理程序
|
||
ipcMain.handle('question-create-fill-blank', async (event, fillBlankData) => {
|
||
try {
|
||
return await createFillBlankQuestion(fillBlankData);
|
||
} catch (error) {
|
||
console.error('Failed to create fill blank question:', error);
|
||
throw error;
|
||
}
|
||
});
|
||
|
||
// 更新填空题问题的IPC处理程序
|
||
ipcMain.handle('question-update-fill-blank', async (event, id, fillBlankData) => {
|
||
try {
|
||
return await modifyFillBlankQuestion(id, fillBlankData);
|
||
} catch (error) {
|
||
console.error('Failed to update fill blank question:', error);
|
||
throw error;
|
||
}
|
||
});
|
||
|
||
// 删除填空题问题的IPC处理程序
|
||
ipcMain.handle('question-delete-fill-blank', async (event, id) => {
|
||
try {
|
||
return await removeFillBlankQuestion(id);
|
||
} catch (error) {
|
||
console.error(`Failed to delete fill blank question ${id}:`, error);
|
||
throw error;
|
||
}
|
||
});
|
||
|
||
// 根据题干ID查询填空题问题的IPC处理程序
|
||
ipcMain.handle('question-fetch-fill-blank-by-question-id', async (event, questionId) => {
|
||
try {
|
||
return await fetchFillBlankQuestionsByQuestionId(questionId);
|
||
} catch (error) {
|
||
console.error(`Failed to fetch fill blank questions by question id ${questionId}:`, error);
|
||
throw error;
|
||
}
|
||
});
|
||
|
||
// 注册查询试题总数和总分的IPC处理程序
|
||
ipcMain.handle('question-get-count-and-score', async () => {
|
||
try {
|
||
return await fetchQuestionsCountAndScore();
|
||
} catch (error) {
|
||
console.error('Failed to fetch questions count and score:', error);
|
||
throw error;
|
||
}
|
||
});
|
||
}
|