196 lines
5.4 KiB
TypeScript
196 lines
5.4 KiB
TypeScript
import { RequestHandler } from "express";
|
|
import { getPiece } from "../services/pieces.service";
|
|
import boxService from "../services/box.service";
|
|
import { Either, eitherLeft, eitherRight, eitherFormatError, Pair, createPair } from '../utils/utils';
|
|
import { Box } from '../types/box'
|
|
import { Piece } from '../types/piece'
|
|
import memberService from "../services/member.service";
|
|
import modelService from "../services/model.service";
|
|
import { getBuyedPiecesFromMember } from "../services/pieces.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 boxAddPieces: RequestHandler<{ title: string; }> = async (req, res) => {
|
|
const boxEither: Either<Box, string> = await boxService.getBox(req.params.title);
|
|
|
|
if (boxEither.hasRight) {
|
|
res.sendStatus(404);
|
|
return;
|
|
}
|
|
|
|
const piece: Either<Piece, string> = await getPiece(req.body.numero_piece);
|
|
if (piece.hasRight) {
|
|
res.status(404).send(eitherFormatError(piece));
|
|
return;
|
|
}
|
|
|
|
if (boxEither.left.pieces.map(p => p.first.id_piece).includes(piece.left.id_piece)) {
|
|
res.status(401).send({});
|
|
return;
|
|
}
|
|
|
|
await boxService.addPiecesToBox(boxEither.left.id, piece.left.id_piece, req.body.quantite);
|
|
|
|
res.redirect(`/boxes/${req.params.title}`);
|
|
};
|
|
|
|
const boxAddPiecesById: RequestHandler<{ id_box: string; }> = async (req, res) => {
|
|
if (isNaN(parseInt(req.params.id_box))) {
|
|
res.sendStatus(400);
|
|
return;
|
|
}
|
|
const boxEither: Either<Box, string> = await boxService.getBox(parseInt(req.params.id_box));
|
|
|
|
if (boxEither.hasRight) {
|
|
res.sendStatus(404);
|
|
return;
|
|
}
|
|
|
|
const piece: Either<Piece, string> = await getPiece(req.body.numero_piece);
|
|
if (piece.hasRight) {
|
|
res.status(404).send(eitherFormatError(piece));
|
|
return;
|
|
}
|
|
|
|
if (boxEither.left.pieces.map(p => p.first.id_piece).includes(piece.left.id_piece)) {
|
|
res.status(401).send({});
|
|
return;
|
|
}
|
|
|
|
await boxService.addPiecesToBox(boxEither.left.id, piece.left.id_piece, req.body.quantite);
|
|
|
|
res.redirect(`/boxes/byid/${req.params.id_box}`);
|
|
};
|
|
|
|
const boxRemovePiece: RequestHandler<{ title: string; }> = async (req, res) => {
|
|
const boxEither: Either<Box, string> = await boxService.getBox(req.params.title);
|
|
|
|
if (boxEither.hasRight) {
|
|
res.sendStatus(404);
|
|
return;
|
|
}
|
|
|
|
const id: any = req.query.id_piece;
|
|
const piece: Either<Piece, string> = await getPiece(id as number);
|
|
if (piece.hasRight) {
|
|
res.status(404).send(eitherFormatError(piece));
|
|
return;
|
|
}
|
|
if (!boxEither.left.pieces.map(p => p.first.id_piece).includes(piece.left.id_piece)) {
|
|
res.status(401).send({});
|
|
return;
|
|
}
|
|
|
|
await boxService.removePieceFromBox(boxEither.left.id, piece.left.id_piece);
|
|
|
|
res.status(200).send();
|
|
};
|
|
|
|
const boxRemovePieceById: RequestHandler<{ id_box: string; }> = async (req, res) => {
|
|
if (isNaN(parseInt(req.params.id_box))) {
|
|
res.sendStatus(400);
|
|
return;
|
|
}
|
|
const boxEither: Either<Box, string> = await boxService.getBox(parseInt(req.params.id_box));
|
|
|
|
if (boxEither.hasRight) {
|
|
res.sendStatus(404);
|
|
return;
|
|
}
|
|
|
|
const id: any = req.query.id_piece;
|
|
const piece: Either<Piece, string> = await getPiece(id as number);
|
|
if (piece.hasRight) {
|
|
res.status(404).send(eitherFormatError(piece));
|
|
return;
|
|
}
|
|
if (!boxEither.left.pieces.map(p => p.first.id_piece).includes(piece.left.id_piece)) {
|
|
res.status(401).send({});
|
|
return;
|
|
}
|
|
|
|
await boxService.removePieceFromBox(boxEither.left.id, piece.left.id_piece);
|
|
|
|
res.status(200).send();
|
|
};
|
|
|
|
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 registerBox: RequestHandler = async (req, res, next) => {
|
|
const id_box = parseInt(req.body.id_box);
|
|
const id_member = parseInt(req.body.id_member);
|
|
const quantity = parseInt(req.body.quantity);
|
|
|
|
await boxService.registerBox(id_box, id_member, quantity);
|
|
|
|
const memberEither = await memberService.getMember(id_member);
|
|
|
|
if (memberEither.hasRight) {
|
|
console.error(memberEither.right);
|
|
next();
|
|
return;
|
|
}
|
|
|
|
const member = memberEither.left;
|
|
const models = await modelService.getAllBuildableModels(member.id_member);
|
|
const boxes = await boxService.getBoxesFromMember(member.id_member);
|
|
const pieces = await getBuyedPiecesFromMember(member.id_member);
|
|
const allboxes = await boxService.getAllBoxes();
|
|
|
|
res.render('member.ejs', { member, models, boxes, pieces, allboxes });
|
|
};
|
|
|
|
const boxController = {
|
|
boxAddPieces,
|
|
boxAddPiecesById,
|
|
boxRemovePiece,
|
|
boxByTitle,
|
|
allBoxes,
|
|
allBoxesFromDate,
|
|
boxById,
|
|
registerBox,
|
|
boxRemovePieceById
|
|
};
|
|
|
|
export default boxController;
|