electron-vue-exam-single/electron/service/examService.js
2025-08-11 23:02:15 +08:00

228 lines
6.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}
export async function initExamIpc(ipcMain) {
// 考试管理相关IPC
ipcMain.handle("exam-create", async (event, examData) => {
try {
// 确保exam_notice是序列化的JSON字符串
if (examData.exam_notice && typeof examData.exam_notice === "object") {
examData.exam_notice = JSON.stringify(examData.exam_notice);
}
return await createNewExam(examData);
} catch (error) {
console.error("Failed to create exam:", error);
throw error;
}
});
ipcMain.handle("exam-update", async (event, { id, examData }) => {
try {
// 确保exam_notice是序列化的JSON字符串
if (examData.exam_notice && typeof examData.exam_notice === "object") {
examData.exam_notice = JSON.stringify(examData.exam_notice);
}
return await modifyExam(id, examData);
} catch (error) {
console.error("Failed to update exam:", error);
throw error;
}
});
ipcMain.handle("exam-fetch-last", async () => {
try {
const exam = await fetchLastExam();
// 将exam_notice字符串解析为数组
if (exam && exam.exam_notice) {
try {
exam.exam_notice = JSON.parse(exam.exam_notice);
} catch (e) {
console.error("解析考试须知失败:", e);
exam.exam_notice = [];
}
}
return exam;
} catch (error) {
console.error("Failed to fetch last exam:", error);
throw error;
}
});
ipcMain.handle("exam-fetch-all", async () => {
try {
return { success: true, data: await fetchAllExams() };
} catch (error) {
console.error("Failed to fetch all exams:", error);
return { success: false, error: error.message };
}
});
ipcMain.handle("exam-fetch-by-id", async (event, id) => {
try {
return { success: true, data: await fetchExamById(id) };
} catch (error) {
console.error(`Failed to fetch exam by id ${id}:`, error);
return { success: false, error: error.message };
}
});
ipcMain.handle("exam-delete", async (event, id) => {
try {
const result = await removeExam(id);
return { success: result, data: { id } };
} catch (error) {
console.error(`Failed to delete exam ${id}:`, error);
return { success: false, error: error.message };
}
});
}