feat: add boxes service, controllers and routes

This commit is contained in:
Nemo D'ACREMONT 2024-11-18 14:18:29 +01:00
parent bc5ccfe221
commit 952fc1d630
No known key found for this signature in database
GPG Key ID: 6E5BCE8022FA8276
5 changed files with 146 additions and 0 deletions

View File

@ -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;

View File

@ -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;

View File

@ -6,6 +6,7 @@ import authRoute from './auth.route'
import marquesRoute from './marque.route' import marquesRoute from './marque.route'
import memberRouter from './member.route' import memberRouter from './member.route'
import modelRouter from './model.route'; import modelRouter from './model.route';
import boxRouter from './box.route';
const routes = express.Router(); const routes = express.Router();
@ -25,6 +26,7 @@ routes.use("/auth", authRoute);
routes.use("/marques", getUsername, verifyAuthentication, marquesRoute); routes.use("/marques", getUsername, verifyAuthentication, marquesRoute);
routes.use("/membres", memberRouter); routes.use("/membres", memberRouter);
routes.use("/models", modelRouter); routes.use("/models", modelRouter);
routes.use("/boxes", boxRouter);
// 404 fallback // 404 fallback
routes.get("*", (_req, res) => { routes.get("*", (_req, res) => {

View File

@ -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<Either<Box, string>> => {
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<Box, string>("Does not exist.");
}
const box = db2box(res.rows[0]);
await client.end();
return eitherLeft<Box, string>(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;

View File

@ -0,0 +1,7 @@
type Box = {
id: number;
title: string;
date: Date;
};
export { Box };