From 952fc1d6304f05b2d9236a9b0e6a7e9ad1b32470 Mon Sep 17 00:00:00 2001 From: Nemo D'ACREMONT Date: Mon, 18 Nov 2024 14:18:29 +0100 Subject: [PATCH] feat: add boxes service, controllers and routes --- back-end/src/controllers/box.controller.ts | 55 +++++++++++++++++ back-end/src/routes/box.route.ts | 14 +++++ back-end/src/routes/index.ts | 2 + back-end/src/services/box.service.ts | 68 ++++++++++++++++++++++ back-end/src/types/box.ts | 7 +++ 5 files changed, 146 insertions(+) create mode 100644 back-end/src/controllers/box.controller.ts create mode 100644 back-end/src/routes/box.route.ts create mode 100644 back-end/src/services/box.service.ts create mode 100644 back-end/src/types/box.ts diff --git a/back-end/src/controllers/box.controller.ts b/back-end/src/controllers/box.controller.ts new file mode 100644 index 0000000..aaa2b91 --- /dev/null +++ b/back-end/src/controllers/box.controller.ts @@ -0,0 +1,55 @@ +import { RequestHandler } from "express"; +import boxService from "../services/box.service"; + +const boxByTitle: RequestHandler<{ title: string; }> = async (req, res) => { + const boxEither = await boxService.getBox(req.params.title); + + if (boxEither.hasRight) { + res.sendStatus(404); + return; + } + + const box = boxEither.left; + + res.render('box.ejs', { box }); +}; + +const allBoxes: RequestHandler = async (_req, res) => { + const boxes = await boxService.getAllBoxes(); + res.render('boxes.ejs', { boxes }); +}; + +const allBoxesFromDate: RequestHandler<{ date: string; }> = async (req, res) => { + const date = new Date(req.params.date); + // If the input isn't a date + if (isNaN(date.getTime())) { + res.sendStatus(400); + return; + } + + const boxes = await boxService.getAllBoxesFromDate(date); + res.render('boxes.ejs', { boxes }); +}; + +const boxById: RequestHandler<{ id: string; }> = async (req, res) => { + const id = parseInt(req.params.id); + const boxEither = await boxService.getBox(id); + + if (boxEither.hasRight) { + res.sendStatus(404); + return; + } + + const box = boxEither.left; + + res.render('box.ejs', { box }); +}; + +const boxController = { + boxByTitle, + allBoxes, + allBoxesFromDate, + boxById, +}; + +export default boxController; diff --git a/back-end/src/routes/box.route.ts b/back-end/src/routes/box.route.ts new file mode 100644 index 0000000..2df615e --- /dev/null +++ b/back-end/src/routes/box.route.ts @@ -0,0 +1,14 @@ +import express from 'express'; +import boxController from '../controllers/box.controller'; + +const boxRouter = express.Router(); + +boxRouter.get("/", boxController.allBoxes); + +boxRouter.get("/from/:date", boxController.allBoxesFromDate); + +boxRouter.get("/:title", boxController.boxByTitle); + +boxRouter.get("/byid/:id", boxController.boxById); + +export default boxRouter; diff --git a/back-end/src/routes/index.ts b/back-end/src/routes/index.ts index ec1dd3b..c09be1f 100644 --- a/back-end/src/routes/index.ts +++ b/back-end/src/routes/index.ts @@ -6,6 +6,7 @@ import authRoute from './auth.route' import marquesRoute from './marque.route' import memberRouter from './member.route' import modelRouter from './model.route'; +import boxRouter from './box.route'; const routes = express.Router(); @@ -25,6 +26,7 @@ routes.use("/auth", authRoute); routes.use("/marques", getUsername, verifyAuthentication, marquesRoute); routes.use("/membres", memberRouter); routes.use("/models", modelRouter); +routes.use("/boxes", boxRouter); // 404 fallback routes.get("*", (_req, res) => { diff --git a/back-end/src/services/box.service.ts b/back-end/src/services/box.service.ts new file mode 100644 index 0000000..48db42b --- /dev/null +++ b/back-end/src/services/box.service.ts @@ -0,0 +1,68 @@ +import { new_client } from '../db/db_client'; +import { Box } from '../types/box'; +import { Either, eitherLeft, eitherRight } from '../utils/utils'; + +type DBBox = { + id_boite: number; + titre_boite: string; + date_boite: string; +}; + +function db2box(data: DBBox): Box { + const box: Box = { + id: data.id_boite, + title: data.titre_boite, + date: new Date(data.date_boite), + }; + + return box; +} + +const getBox = async (idOrTitle: number | string): Promise> => { + const client = new_client(); + await client.connect(); + let res; + if (typeof idOrTitle !== 'number') { + res = await client.query("SELECT * FROM boites WHERE titre_boite=$1;", [`${idOrTitle}`]); + } else { + res = await client.query("SELECT * FROM boites WHERE id_boite=$1;", [idOrTitle]); + } + if (res.rows.length === 0) { + await client.end(); + return eitherRight("Does not exist."); + } + const box = db2box(res.rows[0]); + await client.end(); + + return eitherLeft(box); +} + +const getAllBoxes = async () => { + const client = new_client(); + await client.connect(); + const res = await client.query("SELECT * FROM boites"); + await client.end(); + return res.rows.map(db2box); +} + +const getAllBoxesFromDate = async (date: Date) => { + const client = new_client(); + await client.connect(); + + const YYYY = date.getFullYear(); + const MM = date.getMonth() + 1; + const DD = date.getDate(); + const dateString = `${YYYY}-${MM}-${DD}`; + + const res = await client.query("SELECT * FROM boites WHERE date_boite > $1", [dateString]); + await client.end(); + return res.rows.map(db2box); +} + +const boxService = { + getBox, + getAllBoxes, + getAllBoxesFromDate, +}; + +export default boxService; diff --git a/back-end/src/types/box.ts b/back-end/src/types/box.ts new file mode 100644 index 0000000..32012fb --- /dev/null +++ b/back-end/src/types/box.ts @@ -0,0 +1,7 @@ +type Box = { + id: number; + title: string; + date: Date; +}; + +export { Box };