Skip to content

Commit

Permalink
fix: restful; delete record
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftpsh committed Jul 13, 2024
1 parent 3581653 commit 34f9cc1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/api/record/append.ts → src/api/record/$put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { GameInputGuard } from "src/utils/guard";

const Body = GameInputGuard;

const append: RequestHandler = async (req, res) => {
const handler: RequestHandler = async (req, res) => {
const body = Body.check(req.body);

if (!req.user) {
Expand All @@ -20,4 +20,4 @@ const append: RequestHandler = async (req, res) => {
res.sendStatus(200);
};

export default append;
export default handler;
25 changes: 25 additions & 0 deletions src/api/record/:recordId/$delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { RequestHandler } from "express";
import { Record } from "runtypes";
import db from "src/database/db";
import { IntegerString } from "src/utils/guard";

const Params = Record({
recordId: IntegerString,
});

const handler: RequestHandler = async (req, res) => {
const { recordId } = Params.check(req.params);

if (!req.user) {
res.sendStatus(401);
return;
}

await db.gameRecord.delete({
where: { gameId: +recordId },
});

res.sendStatus(200);
};

export default handler;
9 changes: 9 additions & 0 deletions src/api/record/:recordId/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Router } from "express";
import { wrap } from "src/utils/asyncWrapper";
import $delete from "./$delete";

const router = Router();

router.delete("/", wrap($delete));

export default router;
6 changes: 4 additions & 2 deletions src/api/record/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Router } from "express";
import { wrap } from "src/utils/asyncWrapper";
import append from "./append";
import $put from "./$put";
import recordId from "./:recordId";

const router = Router();
router.use("/:recordId", recordId);

router.put("/append", wrap(append));
router.put("/", wrap($put));

export default router;
4 changes: 4 additions & 0 deletions src/utils/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export const YakumanGuard = StringType.withConstraint<Yakuman>((x) =>

export const Integer = NumberType.withConstraint((x) => Number.isInteger(x));

export const IntegerString = StringType.withConstraint((x) =>
Number.isInteger(Number(x))
);

export const GameInputGuard = RecordType({
gameType: GameTypeGuard,
userScores: ArrayType(
Expand Down

0 comments on commit 34f9cc1

Please sign in to comment.