This commit is contained in:
2022-03-31 12:25:54 +08:00
parent 7e4c106683
commit a67715a2a0
7 changed files with 38 additions and 37 deletions

View File

@@ -41,10 +41,10 @@ const updateUsernameWithLimit = db.transaction((username, id, limit) => {
const count = countUser.get(username).count; const count = countUser.get(username).count;
const existingUsername = getUsername.get(id).username; const existingUsername = getUsername.get(id).username;
if (existingUsername !== "") { if (existingUsername !== "") {
throw new Error("Username already exists"); throw new Error("这个时间段已经有人了喔");
} }
if (count >= limit) { if (count >= limit) {
throw new Error("Limit reached"); throw new Error("达到数量上限啦");
} }
updateUsername.run(username, id); updateUsername.run(username, id);
}); });

View File

@@ -39,7 +39,7 @@ function MyApp({ Component, pageProps }) {
}} }}
> >
<Typography variant="h5"> <Typography variant="h5">
<Link href="/">ITSC Tool</Link> <Link href="/"> </Link>
</Typography> </Typography>
{username && ( {username && (
<Button <Button
@@ -50,7 +50,7 @@ function MyApp({ Component, pageProps }) {
setUsername(""); setUsername("");
}} }}
> >
{username} (Logout) {username} (点击登出)
</Button> </Button>
)} )}
</Toolbar> </Toolbar>

View File

@@ -8,7 +8,7 @@ export default function handler(req, res) {
// authenticate // authenticate
if (!authenticate(token)) { if (!authenticate(token)) {
res.status(401).json({ res.status(401).json({
error: "Unauthorized", error: `token ${token} 验证失败`,
}); });
return; return;
} }
@@ -17,7 +17,7 @@ export default function handler(req, res) {
const limitInt = parseInt(limit); const limitInt = parseInt(limit);
if (!limitInt) { if (!limitInt) {
res.status(400).json({ res.status(400).json({
error: "limit must be integer", error: `数量限制必须是整数,但是传入了 ${limit}`,
}); });
return; return;
} }
@@ -25,6 +25,6 @@ export default function handler(req, res) {
setLimit(limitInt); setLimit(limitInt);
res.status(200).send({ success: true }); res.status(200).send({ success: true });
} else { } else {
res.status(405).send({ error: "method not allowed" }); res.status(405).send({ error: "方法" + req.method + "不被允许" });
} }
} }

View File

@@ -10,7 +10,9 @@ export default function handler(req, res) {
// authenticate // authenticate
const { token } = req.body; const { token } = req.body;
if (!authenticate(token)) { if (!authenticate(token)) {
res.status(401).json({ error: "Unauthorized" }); res.status(401).json({
error: `token ${token} 验证失败`,
});
return; return;
} }
// jsonfiy // jsonfiy
@@ -21,9 +23,6 @@ export default function handler(req, res) {
}); });
return; return;
} else { } else {
// 500 error res.status(405).send({ error: "方法" + req.method + "不被允许" });
res.status(500).json({
error: "Method not allowed",
});
} }
} }

View File

@@ -1,11 +1,16 @@
import { deleteTimeRange, getLimit, authenticate, updateUsernameWithLimit } from "../../../../libs/db"; import {
deleteTimeRange,
getLimit,
authenticate,
updateUsernameWithLimit,
} from "../../../../libs/db";
export default function handler(req, res) { export default function handler(req, res) {
// check if id is valid // check if id is valid
const { id } = req.query; const { id } = req.query;
if (id === undefined) { if (id === undefined) {
res.status(400).json({ res.status(400).json({
error: "Missing id", error: `缺少参数 id`,
}); });
return; return;
} }
@@ -17,7 +22,7 @@ export default function handler(req, res) {
if (!authenticate(token)) { if (!authenticate(token)) {
console.log("[DELETE] Authentication failed"); console.log("[DELETE] Authentication failed");
res.status(401).json({ res.status(401).json({
error: "Unauthenticated", error: `token ${token} 验证失败`,
}); });
return; return;
} }
@@ -30,7 +35,7 @@ export default function handler(req, res) {
const { username } = req.body; const { username } = req.body;
if (username === undefined) { if (username === undefined) {
res.status(400).json({ res.status(400).json({
error: "Missing username", error: `缺少参数 username`,
}); });
return; return;
} }
@@ -45,12 +50,9 @@ export default function handler(req, res) {
return; return;
} }
// not allow // not allow
} else { } else {
// 500 error res.status(405).send({ error: "方法" + req.method + "不被允许" });
res.status(500).json({
error: "Method not allowed",
});
} }
res.status(200).json({ res.status(200).json({

View File

@@ -46,7 +46,7 @@ export default function Index(props) {
<Container> <Container>
<Stack direction="row" spacing={2}> <Stack direction="row" spacing={2}>
<TextField <TextField
label="Username" label="您的大名?"
value={username} value={username}
onChange={(e) => setUsername(e.target.value)} onChange={(e) => setUsername(e.target.value)}
onKeyUp={(e) => { onKeyUp={(e) => {
@@ -64,7 +64,7 @@ export default function Index(props) {
login(); login();
}} }}
> >
Login 登入
</Button> </Button>
</Link> </Link>
</Stack> </Stack>
@@ -74,7 +74,7 @@ export default function Index(props) {
onClose={() => setSnackbarOpen(false)} onClose={() => setSnackbarOpen(false)}
> >
<Alert variant="filled" severity="error"> <Alert variant="filled" severity="error">
Username can{"'"}t be empty 请输入您的大名
</Alert> </Alert>
</Snackbar> </Snackbar>
</Container> </Container>

View File

@@ -172,12 +172,12 @@ export default function Time(props) {
}} }}
> >
<TextField <TextField
label="Name" label="名称"
value={newName} value={newName}
onChange={(e) => setNewName(e.target.value)} onChange={(e) => setNewName(e.target.value)}
/> />
<TextField <TextField
label="Range" label="时间段"
value={range} value={range}
onChange={(e) => setRange(e.target.value)} onChange={(e) => setRange(e.target.value)}
placeholder="2022-01-01 00:00:00" placeholder="2022-01-01 00:00:00"
@@ -188,7 +188,7 @@ export default function Time(props) {
color="primary" color="primary"
onClick={() => addRange()} onClick={() => addRange()}
> >
Add 添加
</Button> </Button>
<Box <Box
sx={{ sx={{
@@ -196,7 +196,7 @@ export default function Time(props) {
}} }}
> >
<TextField <TextField
label="Limit" label="每人数量上限"
value={limit} value={limit}
onChange={(e) => setLimit(e.target.value)} onChange={(e) => setLimit(e.target.value)}
/> />
@@ -207,7 +207,7 @@ export default function Time(props) {
updateLimit(limit); updateLimit(limit);
}} }}
> >
Update Limit 修改上限
</Button> </Button>
</Box> </Box>
</Box> </Box>
@@ -224,10 +224,10 @@ export default function Time(props) {
<Table> <Table>
<TableHead> <TableHead>
<TableRow> <TableRow>
<TableCell>Name</TableCell> <TableCell>名称</TableCell>
<TableCell>Range</TableCell> <TableCell>时间段</TableCell>
<TableCell>Taken</TableCell> <TableCell>姓名</TableCell>
<TableCell>Action</TableCell> <TableCell>操作</TableCell>
</TableRow> </TableRow>
</TableHead> </TableHead>
<TableBody> <TableBody>
@@ -246,7 +246,7 @@ export default function Time(props) {
color="primary" color="primary"
onClick={() => updateUsername(range.id, props.username)} onClick={() => updateUsername(range.id, props.username)}
> >
Take
</Button> </Button>
{isAdmin() && ( {isAdmin() && (
<Button <Button
@@ -257,7 +257,7 @@ export default function Time(props) {
color="secondary" color="secondary"
onClick={() => deleteRange(range.id)} onClick={() => deleteRange(range.id)}
> >
Delete 删除
</Button> </Button>
)} )}
</TableCell> </TableCell>
@@ -289,7 +289,7 @@ export default function Time(props) {
onClose={() => setSnackbarSuccess(false)} onClose={() => setSnackbarSuccess(false)}
severity="success" severity="success"
> >
Success! 操作成功
</Alert> </Alert>
</Snackbar> </Snackbar>
</Container> </Container>