150 lines
3.7 KiB
JavaScript
150 lines
3.7 KiB
JavaScript
import {
|
||
createExam,
|
||
getAllExams,
|
||
getExamById,
|
||
updateExam,
|
||
deleteExam,
|
||
getLastExam // 添加这一行
|
||
} from '../db/exam.js';
|
||
|
||
/**
|
||
* 服务层:创建考试
|
||
* @param {Object} examData 考试数据
|
||
* @returns {Promise<Object>} 创建的考试
|
||
*/
|
||
export async function createNewExam(examData) {
|
||
try {
|
||
// 数据验证 - 修改为exam_minutes和exam_minutes_min必填
|
||
if (!examData.exam_minutes || !examData.exam_minutes_min) {
|
||
throw new Error('考试时长和最少考试时间为必填项');
|
||
}
|
||
|
||
// 移除默认值设置,因为现在是必填项
|
||
if (typeof examData.exam_minutes_min !== 'number') {
|
||
throw new Error('最少考试时间必须是数字');
|
||
}
|
||
|
||
// 确保最少考试时间不大于考试时长
|
||
if (examData.exam_minutes_min > examData.exam_minutes) {
|
||
throw new Error('最少考试时间不能大于考试时长');
|
||
}
|
||
|
||
return await createExam(examData);
|
||
} catch (error) {
|
||
console.error('服务层: 创建考试失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:查询所有考试
|
||
* @returns {Promise<Array>} 考试列表
|
||
*/
|
||
export async function fetchAllExams() {
|
||
try {
|
||
return await getAllExams();
|
||
} catch (error) {
|
||
console.error('服务层: 查询所有考试失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:根据ID查询考试
|
||
* @param {number} id 考试ID
|
||
* @returns {Promise<Object|null>} 考试数据
|
||
*/
|
||
export async function fetchExamById(id) {
|
||
try {
|
||
if (!id) {
|
||
throw new Error('考试ID不能为空');
|
||
}
|
||
|
||
const exam = await getExamById(id);
|
||
if (!exam) {
|
||
throw new Error('未找到指定考试');
|
||
}
|
||
|
||
return exam;
|
||
} catch (error) {
|
||
console.error('服务层: 根据ID查询考试失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:更新考试
|
||
* @param {number} id 考试ID
|
||
* @param {Object} examData 更新的数据
|
||
* @returns {Promise<boolean>} 是否更新成功
|
||
*/
|
||
export async function modifyExam(id, examData) {
|
||
try {
|
||
if (!id) {
|
||
throw new Error('考试ID不能为空');
|
||
}
|
||
|
||
// 验证考试是否存在
|
||
const existingExam = await getExamById(id);
|
||
if (!existingExam) {
|
||
throw new Error('未找到指定考试');
|
||
}
|
||
|
||
// 数据验证 - 修改为exam_minutes和exam_minutes_min必填
|
||
if (!examData.exam_minutes || !examData.exam_minutes_min) {
|
||
throw new Error('考试时长和最少考试时间为必填项');
|
||
}
|
||
|
||
// 移除默认值设置,因为现在是必填项
|
||
if (typeof examData.exam_minutes_min !== 'number') {
|
||
throw new Error('最少考试时间必须是数字');
|
||
}
|
||
|
||
// 确保最少考试时间不大于考试时长
|
||
if (examData.exam_minutes_min > examData.exam_minutes) {
|
||
throw new Error('最少考试时间不能大于考试时长');
|
||
}
|
||
|
||
return await updateExam(id, examData);
|
||
} catch (error) {
|
||
console.error('服务层: 更新考试失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:删除考试
|
||
* @param {number} id 考试ID
|
||
* @returns {Promise<boolean>} 是否删除成功
|
||
*/
|
||
export async function removeExam(id) {
|
||
try {
|
||
if (!id) {
|
||
throw new Error('考试ID不能为空');
|
||
}
|
||
|
||
// 验证考试是否存在
|
||
const existingExam = await getExamById(id);
|
||
if (!existingExam) {
|
||
throw new Error('未找到指定考试');
|
||
}
|
||
|
||
return await deleteExam(id);
|
||
} catch (error) {
|
||
console.error('服务层: 删除考试失败', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 服务层:查询ID最大的考试记录
|
||
* @returns {Promise<Object|null>} 考试数据
|
||
*/
|
||
export async function fetchLastExam() {
|
||
try {
|
||
return await getLastExam();
|
||
} catch (error) {
|
||
console.error('服务层: 查询ID最大的考试失败', error);
|
||
throw error;
|
||
}
|
||
} |