init
This commit is contained in:
57
pages/api/record.ts
Normal file
57
pages/api/record.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import store from "@/store";
|
||||
import config from "@/config";
|
||||
|
||||
export default function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse,
|
||||
) {
|
||||
if (req.method === "POST") {
|
||||
if (!config.begin) {
|
||||
res.status(400).json({
|
||||
error: "还没到开时间哦",
|
||||
});
|
||||
return;
|
||||
}
|
||||
const json = JSON.parse(req.body);
|
||||
console.log("request", json);
|
||||
if (json.checked) {
|
||||
let count = 0;
|
||||
for (const name in store) {
|
||||
if (store[name] == json.user) {
|
||||
count += 1;
|
||||
if (count >= config.limit) {
|
||||
res.status(403).json({
|
||||
error: `超过选择数量限制: ${config.limit}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
// check whether it is alreadly occupied
|
||||
if (store[json.name] !== undefined) {
|
||||
res.status(403).json({
|
||||
error: `当前位置已被他人占用`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
store[json.name] = json.user;
|
||||
} else {
|
||||
delete store[json.name];
|
||||
}
|
||||
}
|
||||
console.log("query", req.query);
|
||||
const resp = {
|
||||
occupied: [],
|
||||
myselect: [],
|
||||
};
|
||||
for (const key in store) {
|
||||
if (store[key] !== req.query.name) {
|
||||
resp.occupied.push(key);
|
||||
} else {
|
||||
resp.myselect.push(key);
|
||||
}
|
||||
}
|
||||
res.status(200).json(resp);
|
||||
}
|
||||
Reference in New Issue
Block a user