mirror of
https://github.com/ZeroCatDev/ClassworksKV.git
synced 2025-09-03 16:19:24 +00:00
1
This commit is contained in:
parent
1a03634abf
commit
f0f9568eb6
106
app.js
106
app.js
@ -7,6 +7,7 @@ const bodyParser = require("body-parser");
|
||||
|
||||
var indexRouter = require("./routes/index");
|
||||
var usersRouter = require("./routes/users");
|
||||
var homeworkRouter = require("./routes/homework");
|
||||
|
||||
var app = express();
|
||||
|
||||
@ -32,112 +33,13 @@ const { PrismaClient } = require("@prisma/client");
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
// 上传作业数据
|
||||
app.post("/upload", async (req, res) => {
|
||||
try {
|
||||
const date = new Date().toISOString().split("T")[0];
|
||||
const data = req.body;
|
||||
// 使用路由
|
||||
app.use("/", homeworkRouter);
|
||||
app.use("/users", usersRouter);
|
||||
|
||||
// 使用 upsert 来确保每个日期只有一条记录
|
||||
await prisma.homework.upsert({
|
||||
where: {
|
||||
date: date,
|
||||
},
|
||||
update: {
|
||||
data: data,
|
||||
},
|
||||
create: {
|
||||
date: date,
|
||||
data: data,
|
||||
},
|
||||
});
|
||||
|
||||
res.json({
|
||||
status: true,
|
||||
msg: "上传成功",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Upload error:", error);
|
||||
res.json({
|
||||
status: false,
|
||||
msg: "上传失败:" + error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 下载作业数据
|
||||
app.get("/download", async (req, res) => {
|
||||
try {
|
||||
const date = req.query.date;
|
||||
|
||||
const homework = await prisma.homework.findFirst({
|
||||
where: {
|
||||
date: date,
|
||||
},
|
||||
});
|
||||
|
||||
if (!homework) {
|
||||
throw new Error("该日期未上传数据");
|
||||
}
|
||||
|
||||
res.json({
|
||||
status: true,
|
||||
msg: "下载成功",
|
||||
...homework.data,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Download error:", error);
|
||||
res.json({
|
||||
status: false,
|
||||
msg: "下载失败:" + error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
app.get("/config.json", async (req, res) => {
|
||||
var studentList=[];
|
||||
await prisma.config
|
||||
.findFirst({ where: { id: 1 } })
|
||||
.then((config) => {
|
||||
console.log(config)
|
||||
studentList = (config.student).split(",");
|
||||
res.json({
|
||||
"//": "学生名字列表(推荐按学号顺序排列)除最后一项外,每个学生姓名后面必须加一个逗号",
|
||||
"//": "如果不需要“出勤”功能,请把下面“:”后面的“[]”中的内容删除即可",
|
||||
studentList: studentList,
|
||||
"//": "作业框排版(前面的中括号中的科目显示在分割线左侧,后面在右侧)除每个中括号中的最后一项外,每个科目后面必须加一个逗号",
|
||||
homeworkArrange: [
|
||||
["语文", "数学", "英语"],
|
||||
["物理", "化学", "生物"],
|
||||
["政治", "历史", "地理"],
|
||||
],
|
||||
"//": "这里需填入部署ServerAPI的服务器url",
|
||||
"//": "端口默认17312无需改动(除非更改Python代码)",
|
||||
"//": "127.0.0.1仅适用于本地测试,实际部署请使用公网/内网ip地址或域名",
|
||||
url: "http://localhost:3030",
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
app.post("/setstudentlist", async (req, res) => {
|
||||
var studentList = req.body.studentList.join(",");
|
||||
await prisma.config.upsert({
|
||||
where: {
|
||||
id: req.body.id,
|
||||
},
|
||||
update: {
|
||||
student: studentList,
|
||||
},
|
||||
create: {
|
||||
id: req.body.id,
|
||||
student: studentList,
|
||||
},
|
||||
});
|
||||
});
|
||||
app.get("/test", async (req, res) => {
|
||||
res.render("test.ejs");
|
||||
});
|
||||
app.use("/", indexRouter);
|
||||
app.use("/users", usersRouter);
|
||||
|
||||
// catch 404 and forward to error handler
|
||||
app.use(function (req, res, next) {
|
||||
|
@ -8,13 +8,19 @@ datasource db {
|
||||
}
|
||||
|
||||
model homework {
|
||||
date String @id @db.VarChar(10)
|
||||
class String @db.VarChar(45)
|
||||
date String @db.VarChar(10)
|
||||
data Json
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @default(now())
|
||||
|
||||
@@id([date, class])
|
||||
}
|
||||
|
||||
model config {
|
||||
id Int @id @default(autoincrement())
|
||||
id Int @default(autoincrement())
|
||||
class String @db.VarChar(45)
|
||||
student String @db.MediumText
|
||||
|
||||
@@id([id, class])
|
||||
}
|
||||
|
144
routes/homework.js
Normal file
144
routes/homework.js
Normal file
@ -0,0 +1,144 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { PrismaClient } = require("@prisma/client");
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
// 上传/更新作业数据
|
||||
router.post("/:classId/homework", async (req, res) => {
|
||||
try {
|
||||
const date = new Date().toISOString().split("T")[0];
|
||||
const data = req.body;
|
||||
const className = req.params.classId;
|
||||
|
||||
await prisma.homework.upsert({
|
||||
where: {
|
||||
date_class: {
|
||||
date: date,
|
||||
class: className
|
||||
}
|
||||
},
|
||||
update: {
|
||||
data: data,
|
||||
},
|
||||
create: {
|
||||
date: date,
|
||||
class: className,
|
||||
data: data,
|
||||
},
|
||||
});
|
||||
|
||||
res.json({
|
||||
status: true,
|
||||
msg: "上传成功",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Upload error:", error);
|
||||
res.json({
|
||||
status: false,
|
||||
msg: "上传失败:" + error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 获取作业数据
|
||||
router.get("/:classId/homework", async (req, res) => {
|
||||
try {
|
||||
const date = req.query.date;
|
||||
const className = req.params.classId;
|
||||
|
||||
const homework = await prisma.homework.findFirst({
|
||||
where: {
|
||||
date: date,
|
||||
class: className
|
||||
},
|
||||
});
|
||||
|
||||
if (!homework) {
|
||||
throw new Error("该日期未上传数据");
|
||||
}
|
||||
|
||||
res.json({
|
||||
status: true,
|
||||
msg: "获取成功",
|
||||
...homework.data,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Download error:", error);
|
||||
res.json({
|
||||
status: false,
|
||||
msg: "获取失败:" + error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 获取班级配置信息
|
||||
router.get("/:classId/config", async (req, res) => {
|
||||
const className = req.params.classId;
|
||||
var studentList = [];
|
||||
|
||||
try {
|
||||
const config = await prisma.config.findFirst({
|
||||
where: {
|
||||
id: 1,
|
||||
class: className
|
||||
}
|
||||
});
|
||||
|
||||
if (config) {
|
||||
studentList = config.student.split(",");
|
||||
}
|
||||
|
||||
res.json({
|
||||
studentList: studentList,
|
||||
homeworkArrange: [
|
||||
["语文", "数学", "英语"],
|
||||
["物理", "化学", "生物"],
|
||||
["政治", "历史", "地理"],
|
||||
],
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Config error:", error);
|
||||
res.json({
|
||||
status: false,
|
||||
msg: "获取配置失败:" + error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 更新班级学生列表
|
||||
router.put("/:classId/students", async (req, res) => {
|
||||
try {
|
||||
const className = req.params.classId;
|
||||
const studentList = req.body.studentList.join(",");
|
||||
|
||||
await prisma.config.upsert({
|
||||
where: {
|
||||
id_class: {
|
||||
id: req.body.id,
|
||||
class: className
|
||||
}
|
||||
},
|
||||
update: {
|
||||
student: studentList,
|
||||
},
|
||||
create: {
|
||||
id: req.body.id,
|
||||
class: className,
|
||||
student: studentList,
|
||||
},
|
||||
});
|
||||
|
||||
res.json({
|
||||
status: true,
|
||||
msg: "更新成功"
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("SetStudentList error:", error);
|
||||
res.json({
|
||||
status: false,
|
||||
msg: "更新失败:" + error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
Loading…
x
Reference in New Issue
Block a user