feat: add marque registration

This commit is contained in:
Alessandre Laguierce 2024-10-26 17:58:23 +02:00
parent 3626f92d2d
commit c0a5b036da
5 changed files with 123 additions and 1 deletions

View File

@ -0,0 +1,57 @@
import { Request, Response } from 'express';
import { new_client } from '../db/db_client';
import { Marque } from '../types/marque';
import { Either, eitherLeft, eitherRight } from '../utils/utils';
const getMarque = async (idOrName: number | string): Promise<Either<Marque, string>> => {
const client = new_client();
await client.connect();
let res;
if (typeof idOrName !== 'number') {
res = await client.query("SELECT * FROM marques WHERE nom_marque=$1;", [`${idOrName}`]);
} else {
res = await client.query("SELECT * FROM marques WHERE id_marque=$1;", [idOrName]);
}
if (res.rows.length === 0) {
await client.end();
return eitherRight<Marque, string>("Does not exist.");
}
const marque: Marque = { id_marque: res.rows[0].id_marque, name: res.rows[0].nom_marque };
await client.end();
return eitherLeft<Marque, string>(marque);
}
const createMarque = async (name: string): Promise<Either<Marque, string>> => {
const gettingMarque = await getMarque(name);
if (!gettingMarque.hasRight) {
return eitherRight<Marque, string>("Already in database.");
}
const client = new_client();
await client.connect();
const res = await client.query("INSERT INTO marques (nom_membre) VALUES ($1) RETURNING *;", [`${name}`]);
if (res.rows.length === 0) {
await client.end();
return eitherRight<Marque, string>("Something went wrong");
}
const marque: Marque = { id_marque: res.rows[0].id_marque, name: res.rows[0].nom_marque };
await client.end();
return eitherLeft<Marque, string>(marque);
};
const register = async (req: Request, res: Response) => {
if (!req || !req.body || !req.body.name) {
res.status(400).send();
return;
}
const name: string = req.body.name;
const marque: Either<Marque, string> = await createMarque(name);
if (marque.hasRight) {
res.status(403).send(marque.right);
return;
}
res.status(200).send(marque);
}
export { register };

View File

@ -0,0 +1,48 @@
import { Request, Response } from 'express';
import { jwtVerify } from 'jose';
import { JWT_SECRET_KEY } from '../config/auth.config';
import { getMember } from '../controllers/member.controller';
import { Member } from '../types/member';
import { Either } from '../utils/utils';
const extractBearerToken = (headerValue: string) => {
const matches = headerValue.match(/(bearer)\s+(\S+)/i);
return matches && matches[2];
}
const getUsername = async (req: Request, res: Response, next: () => void) => {
const token = req.headers.authorization && extractBearerToken(req.headers.authorization);
if (!token) {
next();
return;
}
try {
const { payload } = await jwtVerify(token, JWT_SECRET_KEY);
const name: string = payload.name as string;
const member: Either<Member, string> = await getMember(name);
if (!member.hasRight) {
res.locals.user = {
id_member: member.left.id_member,
name: member.left.name
}
next()
} else {
res.status(401).send(member.right)
}
} catch (e) {
res.status(401).send();
}
}
function verifyAuthentication(req: Request, res: Response, next: () => void) {
if (!res.locals.user) {
res.status(401).send();
return;
}
next();
}
export { getUsername, verifyAuthentication };

View File

@ -1,6 +1,8 @@
import express from 'express';
import authRoute from './auth.route'
import db from '../db';
import { getUsername, verifyAuthentication } from '../middlewares/auth.middleware'
import authRoute from './auth.route'
import marquesRoute from './marque.route'
const routes = express.Router();
@ -15,5 +17,6 @@ routes.get("/message", async (_req, res) => {
});
routes.use("/auth", authRoute);
routes.use("/marques", getUsername, verifyAuthentication, marquesRoute);
export default routes;

View File

@ -0,0 +1,8 @@
import express from 'express';
import { register } from '../controllers/marque.controller';
const router = express.Router();
router.post('/register', register);
export default router;

View File

@ -0,0 +1,6 @@
type Marque = {
id_marque: number,
name: string
};
export { Marque };