feat: add boxes service, controllers and routes
This commit is contained in:
parent
bc5ccfe221
commit
952fc1d630
55
back-end/src/controllers/box.controller.ts
Normal file
55
back-end/src/controllers/box.controller.ts
Normal 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;
|
14
back-end/src/routes/box.route.ts
Normal file
14
back-end/src/routes/box.route.ts
Normal 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;
|
@ -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) => {
|
||||
|
68
back-end/src/services/box.service.ts
Normal file
68
back-end/src/services/box.service.ts
Normal 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;
|
7
back-end/src/types/box.ts
Normal file
7
back-end/src/types/box.ts
Normal file
@ -0,0 +1,7 @@
|
||||
type Box = {
|
||||
id: number;
|
||||
title: string;
|
||||
date: Date;
|
||||
};
|
||||
|
||||
export { Box };
|
Loading…
x
Reference in New Issue
Block a user