Merge branch 'refactor-marque'
This commit is contained in:
commit
4799d2cd34
@ -1,43 +1,7 @@
|
||||
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);
|
||||
};
|
||||
import marqueService from '../services/marque.service';
|
||||
import { Either } from '../utils/utils';
|
||||
|
||||
const register = async (req: Request, res: Response) => {
|
||||
if (!req || !req.body || !req.body.name) {
|
||||
@ -45,7 +9,7 @@ const register = async (req: Request, res: Response) => {
|
||||
return;
|
||||
}
|
||||
const name: string = req.body.name;
|
||||
const marque: Either<Marque, string> = await createMarque(name);
|
||||
const marque: Either<Marque, string> = await marqueService.createMarque(name);
|
||||
if (marque.hasRight) {
|
||||
res.status(403).send(marque.right);
|
||||
return;
|
||||
@ -54,4 +18,8 @@ const register = async (req: Request, res: Response) => {
|
||||
res.status(200).send(marque);
|
||||
}
|
||||
|
||||
export { register };
|
||||
const marqueController = {
|
||||
register
|
||||
};
|
||||
|
||||
export default marqueController;
|
||||
|
@ -1,8 +1,8 @@
|
||||
import express from 'express';
|
||||
import { register } from '../controllers/marque.controller';
|
||||
import marqueController from '../controllers/marque.controller';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.post('/register', register);
|
||||
router.post('/register', marqueController.register);
|
||||
|
||||
export default router;
|
||||
|
47
back-end/src/services/marque.service.ts
Normal file
47
back-end/src/services/marque.service.ts
Normal file
@ -0,0 +1,47 @@
|
||||
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 marqueService = {
|
||||
getMarque,
|
||||
createMarque
|
||||
};
|
||||
|
||||
export default marqueService;
|
Loading…
x
Reference in New Issue
Block a user