feat: add services, controller, routes, types and templates for models

This commit is contained in:
Nemo D'ACREMONT 2024-11-17 09:19:51 +01:00
parent a9fbd381cd
commit 72d04bdab1
No known key found for this signature in database
GPG Key ID: 6E5BCE8022FA8276
6 changed files with 202 additions and 0 deletions

View File

@ -0,0 +1,42 @@
import { RequestHandler } from "express";
import modelService from "../services/model.service";
const modelByName: RequestHandler<{ name: string; }> = async (req, res) => {
const modelEither = await modelService.getModel(req.params.name);
if (modelEither.hasRight) {
res.sendStatus(404);
return;
}
const model = modelEither.left;
res.render('model.ejs', { model });
};
const allModels: RequestHandler<{ name: string; }> = async (req, res) => {
const models = await modelService.getAllModels();
res.render('models.ejs', { models });
};
const modelById: RequestHandler<{ id: string; }> = async (req, res) => {
const id = parseInt(req.params.id);
const modelEither = await modelService.getModel(id);
if (modelEither.hasRight) {
res.sendStatus(404);
return;
}
const model = modelEither.left;
res.render('model.ejs', { model });
};
const modelController = {
modelByName,
allModels,
modelById,
};
export default modelController;

View File

@ -0,0 +1,12 @@
import express from 'express';
import modelController from '../controllers/model.controller';
const modelRouter = express.Router();
modelRouter.get("/", modelController.allModels);
modelRouter.get("/:name", modelController.modelByName);
modelRouter.get("/byid/:id", modelController.modelById);
export default modelRouter;

View File

@ -0,0 +1,81 @@
import { new_client } from '../db/db_client';
import { Model } from '../types/model';
import { Either, eitherLeft, eitherRight } from '../utils/utils';
type DBModel = {
id_modele: number;
nom_modele: string;
url_notice_modele: string;
id_membre: number;
id_modele_2: number;
};
function db2Model(data: DBModel) {
const model: Model = {
id: data.id_modele,
name: data.nom_modele,
url: data.url_notice_modele,
creator: data.id_membre,
inheritFrom: data.id_modele_2,
};
return model;
}
const getModel = async (idOrName: number | string): Promise<Either<Model, string>> => {
const client = new_client();
await client.connect();
let res;
if (typeof idOrName !== 'number') {
res = await client.query("SELECT * FROM modeles WHERE nom_modele=$1;", [`${idOrName}`]);
} else {
res = await client.query("SELECT * FROM modeles WHERE id_modele=$1;", [idOrName]);
}
if (res.rows.length === 0) {
await client.end();
return eitherRight<Model, string>("Does not exist.");
}
const model = db2Model(res.rows[0]);
await client.end();
return eitherLeft<Model, string>(model);
}
const getAllModels = async () => {
const client = new_client();
await client.connect();
const res = await client.query("SELECT * FROM modeles");
await client.end();
return res.rows.map(db2Model);
}
const createModel = async (name: string, url: string, creator: number, inheritFrom: number): Promise<Either<Model, string>> => {
const gettingModel = await getModel(name);
if (!gettingModel.hasRight) {
return eitherRight<Model, string>("Already in database.");
}
const client = new_client();
await client.connect();
const res = await client.query(
"INSERT INTO modeles \
(nom_modele, url_notice_modele, id_membre, id_modele_2) \
VALUES ($1, $2, $3, $4) \
RETURNING *;",
[`${name}`, `${url}`, `${creator}`, `${inheritFrom}`]);
if (res.rows.length === 0) {
await client.end();
return eitherRight<Model, string>("Something went wrong");
}
const model: Model = db2Model(res.rows[0]);
await client.end();
return eitherLeft<Model, string>(model);
}
const modelService = {
getModel,
getAllModels,
createModel,
};
export default modelService;

View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
<%= model.name %>
</title>
<%- include('partials/links.ejs') %>
</head>
<body>
<%- include('partials/header.ejs') %>
<h1>
<%= model.name %>
</h1>
<pre>
id : <%= model.id %>
url : <%= model.url %>
creator : <%= model.creator %>
inheritFrom : <%= model.inheritFrom %>
</pre>
</body>
</html>

View File

@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
Liste des modeles
</title>
<%- include('partials/links.ejs') %>
</head>
<body>
<%- include('partials/header.ejs') %>
<h1>
Liste des modeles
</h1>
<ul>
<% models.forEach(function(model) { %>
<li>
<a href="/models/<%=model.name%>">
<%=model.name%>
</a>
</li>
<% }); %>
</ul>
</body>
</html>

View File

@ -0,0 +1,9 @@
type Model = {
id: number;
name: string;
url: string;
creator: number;
inheritFrom: number;
};
export { Model };