feat: založení základní stránky příjem/výdej + import a statistiky
This commit is contained in:
commit
a5711f3e1e
98 files changed
+15505
No files matched your search
@@ -0,0 +1,59 @@
|
||||
import express, { Request, Response, NextFunction } from "express";
|
||||
import { addMeal, deleteMeal, getDay, getRange, updateMeal } from "../meals";
|
||||
import { getLogin } from "../auth";
|
||||
import { formatDate, getToday, parseToken, requireIsoDate } from "../utils";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
/**
|
||||
* Vrátí jídla přihlášeného uživatele za jeden den. Bez parametru vrací dnešek.
|
||||
*/
|
||||
router.get("/day", async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const login = getLogin(parseToken(req));
|
||||
const date = req.query.date == null
|
||||
? formatDate(getToday())
|
||||
: requireIsoDate(req.query.date, 'date');
|
||||
res.status(200).json(await getDay(login, date));
|
||||
} catch (e) { next(e); }
|
||||
});
|
||||
|
||||
/**
|
||||
* Vrátí dny přihlášeného uživatele v rozsahu, které obsahují alespoň jeden záznam.
|
||||
*/
|
||||
router.get("/range", async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const login = getLogin(parseToken(req));
|
||||
const from = requireIsoDate(req.query.from, 'from');
|
||||
const to = requireIsoDate(req.query.to, 'to');
|
||||
res.status(200).json(await getRange(login, from, to));
|
||||
} catch (e) { next(e); }
|
||||
});
|
||||
|
||||
/** Přidá jídlo do zvoleného dne. */
|
||||
router.post("/add", async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const login = getLogin(parseToken(req));
|
||||
res.status(200).json(await addMeal(login, req.body));
|
||||
} catch (e) { next(e); }
|
||||
});
|
||||
|
||||
/** Upraví existující záznam o jídle. */
|
||||
router.post("/update", async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const login = getLogin(parseToken(req));
|
||||
const { id, ...input } = req.body ?? {};
|
||||
res.status(200).json(await updateMeal(login, id, input));
|
||||
} catch (e) { next(e); }
|
||||
});
|
||||
|
||||
/** Smaže záznam o jídle. */
|
||||
router.post("/delete", async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const login = getLogin(parseToken(req));
|
||||
const { id, date } = req.body ?? {};
|
||||
res.status(200).json(await deleteMeal(login, id, date));
|
||||
} catch (e) { next(e); }
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in new issue
Block a user