231 lines
6.1 KiB
JavaScript
231 lines
6.1 KiB
JavaScript
import {
|
||
addQuestion,
|
||
getAllQuestions,
|
||
getQuestionById,
|
||
updateQuestion,
|
||
deleteQuestion,
|
||
getAllQuestionsWithRelations,
|
||
updateQuestionDescription,
|
||
addChoiceQuestion,
|
||
updateChoiceQuestion, // 添加这一行
|
||
addFillBlankQuestion,
|
||
updateFillBlankQuestion,
|
||
deleteFillBlankQuestion,
|
||
getFillBlankQuestionsByQuestionId
|
||
} 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;
|
||
}
|
||
} |