You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.6 KiB
42 lines
1.6 KiB
import express from 'express';
|
|
import dotenv from 'dotenv';
|
|
import bodyParser from 'body-parser';
|
|
dotenv.config();
|
|
|
|
import DbManager from './dbManager.js';
|
|
const database = new DbManager(process.env.DB_HOST, process.env.DB_USER, process.env.DB_PASSWORD, process.env.DB_PORT, process.env.DB_NAME)
|
|
|
|
|
|
const app = express();
|
|
app.use(bodyParser.json());
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
app.get('/api/get-clients', async function (req, res) {
|
|
const results = await database.getClients();
|
|
res.send(results);
|
|
});
|
|
|
|
app.post('/api/create-client', async function (req, res) {
|
|
if (!req.body.nom || !req.body.prenom || !req.body.email || !req.body.ville || !req.body.date_naissance || !req.body.code_postal || !req.body.pays) {
|
|
return res.status(400).send({ message: 'Veuillez remplir tout les champs' });
|
|
}
|
|
|
|
const result = await database.insertClient(req.body)
|
|
res.status(result ? 200 : 400).send({ message: result ? 'Le client à été ajouté.' : 'Impossible d\'ajouter ce client.' })
|
|
});
|
|
|
|
app.delete('/api/delete-client', async function (req, res) {
|
|
if (!req.query.id) {
|
|
return res.status(400).send({ message: 'Veuillez indiquer l\'id du client à supprimer' });
|
|
}
|
|
|
|
const result = await database.deleteClient(req.query.id)
|
|
res.status(result?.affectedRows === 1 ? 200 : 400).send({ message: result?.affectedRows === 1 ? 'Le client à été supprimé.' : 'Le client n\'existe pas.' })
|
|
});
|
|
|
|
const port = process.env.API_PORT || 8080
|
|
|
|
app.listen(port, async function () {
|
|
console.log(`server listening on ${port}`);
|
|
});
|