This commit is contained in:
2022-03-31 12:06:31 +08:00
parent 75a366ae11
commit 7e4c106683
10 changed files with 1905 additions and 459 deletions

30
pages/api/time/limit.js Normal file
View File

@@ -0,0 +1,30 @@
import { authenticate, setLimit } from "../../../libs/db";
export default function handler(req, res) {
// put method
if (req.method === "PUT") {
const { token, limit } = req.body;
// authenticate
if (!authenticate(token)) {
res.status(401).json({
error: "Unauthorized",
});
return;
}
// check type is integer
const limitInt = parseInt(limit);
if (!limitInt) {
res.status(400).json({
error: "limit must be integer",
});
return;
}
setLimit(limitInt);
res.status(200).send({ success: true });
} else {
res.status(405).send({ error: "method not allowed" });
}
}

29
pages/api/time/ranges.js Normal file
View File

@@ -0,0 +1,29 @@
import { authenticate, getTimeRanges, insertTimeRange } from "../../../libs/db";
export default function handler(req, res) {
// get method
if (req.method === "GET") {
res.setHeader("Cache-Control", "no-cache no-store must-revalidate");
res.status(200).json(getTimeRanges.all());
return;
} else if (req.method === "POST") {
// authenticate
const { token } = req.body;
if (!authenticate(token)) {
res.status(401).json({ error: "Unauthorized" });
return;
}
// jsonfiy
const { name, range } = req.body;
insertTimeRange.run(name, range);
res.status(200).json({
success: true,
});
return;
} else {
// 500 error
res.status(500).json({
error: "Method not allowed",
});
}
}

View File

@@ -0,0 +1,59 @@
import { deleteTimeRange, getLimit, authenticate, updateUsernameWithLimit } from "../../../../libs/db";
export default function handler(req, res) {
// check if id is valid
const { id } = req.query;
if (id === undefined) {
res.status(400).json({
error: "Missing id",
});
return;
}
// delete method
if (req.method === "DELETE") {
// authenticate
const { token } = req.body;
if (!authenticate(token)) {
console.log("[DELETE] Authentication failed");
res.status(401).json({
error: "Unauthenticated",
});
return;
}
deleteTimeRange.run(id);
// update username
} else if (req.method === "PUT") {
// check if id is valid
// check if username is valid
const { username } = req.body;
if (username === undefined) {
res.status(400).json({
error: "Missing username",
});
return;
}
try {
const limit = getLimit();
updateUsernameWithLimit(username, id, limit);
} catch (err) {
res.status(400).json({
error: err.message,
});
return;
}
// not allow
} else {
// 500 error
res.status(500).json({
error: "Method not allowed",
});
}
res.status(200).json({
success: true,
});
}