diff --git a/back-end/src/services/marque.service.ts b/back-end/src/services/marque.service.ts index 47eaf48..9381364 100644 --- a/back-end/src/services/marque.service.ts +++ b/back-end/src/services/marque.service.ts @@ -4,39 +4,39 @@ import { Marque } from '../types/marque'; import { Either, eitherLeft, eitherRight } from '../utils/utils'; const getMarque = async (idOrName: number | string): Promise> => { - 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("Does not exist."); - } - const marque: Marque = { id_marque: res.rows[0].id_marque, name: res.rows[0].nom_marque }; + 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("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); + return eitherLeft(marque); } const createMarque = async (name: string): Promise> => { - const gettingMarque = await getMarque(name); - if (!gettingMarque.hasRight) { - return eitherRight("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("Something went wrong"); - } - const marque: Marque = { id_marque: res.rows[0].id_marque, name: res.rows[0].nom_marque }; + const gettingMarque = await getMarque(name); + if (!gettingMarque.hasRight) { + return eitherRight("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 eitherLeft(marque); + return eitherRight("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); }; const marqueService = {