first
This commit is contained in:
29
pages/api/time/ranges.js
Normal file
29
pages/api/time/ranges.js
Normal 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",
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user