60 lines
1.3 KiB
JavaScript
60 lines
1.3 KiB
JavaScript
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,
|
|
});
|
|
}
|