From 50b5c50270be8e1927ae2653ec28d30c69c0c74e Mon Sep 17 00:00:00 2001 From: Jerome Laurent Date: Sun, 16 Oct 2022 12:19:14 +0200 Subject: [PATCH 1/5] =?UTF-8?q?Modifications=20de=20l'affichage=20de=20la?= =?UTF-8?q?=20cr=C3=A9ation=20d'une=20annonce?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- views/creerAnnonce.pug | 50 +++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/views/creerAnnonce.pug b/views/creerAnnonce.pug index 3216fbb..5af3f0f 100644 --- a/views/creerAnnonce.pug +++ b/views/creerAnnonce.pug @@ -5,28 +5,48 @@ block content h1 Création d'une annonce : br form(action="/ListeAnnonces/creer", method="post") - label(for="titre") Titre :   - input(name="titre" type="text") + label Titre :   + input(name="titre" type="text" required) + br - label(for="typeBien") Type de bien (vente / location) :   - input(name="typeBien" type="text") + + label Type de bien : + input(type="radio" name="typeBien" value="vente" checked) + label Vente + input(type="radio" name="typeBien" value="location") + label Location + br - label(for="statusPub") Status de publication (publiée / non publiée) :   - input(name="statusPublication" type="text") + + label Status de publication : + input(type="radio" name="statusPublication" value="publiée" checked) + label Publiée + input(type="radio" name="statusPublication" value="non publiée") + label Non publiée + br - label(for="statusBien") Status de bien (disponible / loué / vendu) :   - input(name="statusBien" type="text") + + input(type="text" name="statusBien" value="disponible" hidden) + + label Description : br - label(for="description") Description :   - input(name="description" type="text") + textarea(name="description" required) + br - label(for="prix") Prix :   - input(name="prixBien" type="number") + + label Prix :   + input(name="prixBien" type="number" min="1" required) + br - label(for="dateDispo") Date de disponibilité :   - input(name="dateDisponibilite" type="date") + + label Date de disponibilité :   + input(name="dateDisponibilite" type="date" required) + br - label(for="photos") Photos :   + + label Photos :   input(name="photos" type="text") + br + button(type="submit", name="Ajouter") Ajouter -- GitLab From 09f320c4b66dac99fb508ec3e3fb722d74bf1162 Mon Sep 17 00:00:00 2001 From: Jerome Laurent Date: Sun, 16 Oct 2022 14:23:28 +0200 Subject: [PATCH 2/5] Petites modifications creerAnnonce.pug --- views/creerAnnonce.pug | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/views/creerAnnonce.pug b/views/creerAnnonce.pug index 5af3f0f..2d01b39 100644 --- a/views/creerAnnonce.pug +++ b/views/creerAnnonce.pug @@ -12,17 +12,17 @@ block content label Type de bien : input(type="radio" name="typeBien" value="vente" checked) - label Vente + label vente input(type="radio" name="typeBien" value="location") - label Location + label location br label Status de publication : input(type="radio" name="statusPublication" value="publiée" checked) - label Publiée + label publiée input(type="radio" name="statusPublication" value="non publiée") - label Non publiée + label non publiée br -- GitLab From cfaf9216412a98dfcdbd638a9516926a791deed2 Mon Sep 17 00:00:00 2001 From: Jerome Laurent Date: Sun, 16 Oct 2022 14:26:43 +0200 Subject: [PATCH 3/5] Modification fonction findOne + ajout fonction update dans controllerAnnonce --- app/controllers/controllerAnnonce.js | 30 +++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/app/controllers/controllerAnnonce.js b/app/controllers/controllerAnnonce.js index d7b4718..5b87f25 100644 --- a/app/controllers/controllerAnnonce.js +++ b/app/controllers/controllerAnnonce.js @@ -1,3 +1,5 @@ +const moment = require('moment') + const Annonce = require("../models/annonce") // Create and Save a new Annonce @@ -58,7 +60,7 @@ exports.findOne = (req, res) => { .then(data => { if (!data) res.status(404).send({ message: "Not found Annnonce with id " + id }); - else res.send(data); + else res.render('modifierAnnonce', { data: data, date:moment(data.dateDisponibilite).format("YYYY-MM-DD") }); }) .catch(err => { res @@ -68,12 +70,34 @@ exports.findOne = (req, res) => { }; // Update an Annonce by the id in the request -/*exports.update = (req, res) => { +exports.update = (req, res) => { + if (!req.body) { + return res.status(400).send({ + message: "Data to update can not be empty!" + }); + } + + const id = req.params.id; + + console.log(id); + Annonce.findByIdAndUpdate(id, req.body, { useFindAndModify: false }) + .then(data => { + if (!data) { + res.status(404).send({ + message: `Cannot update Annonce with id=${id}. Maybe Annonce was not found!` + }); + } else res.redirect('/listeAnnonces'); + }) + .catch(err => { + res.status(500).send({ + message: "Error updating Tutorial with id=" + id + }); + }); }; // Delete an Annonce with the specified id in the request -exports.delete = (req, res) => { +/*exports.delete = (req, res) => { }; -- GitLab From 5283c8a3b7ae8b6bf5c8d550319d1a3db4585c88 Mon Sep 17 00:00:00 2001 From: Jerome Laurent Date: Sun, 16 Oct 2022 14:27:45 +0200 Subject: [PATCH 4/5] Ajout page de modification d'une annonce (modifications possibles) --- routes/listeAnnonces.js | 14 ++++++-- views/afficherAnnonce.pug | 17 ---------- views/listeAnnonces.pug | 1 + views/modifierAnnonce.pug | 69 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 19 deletions(-) delete mode 100644 views/afficherAnnonce.pug create mode 100644 views/modifierAnnonce.pug diff --git a/routes/listeAnnonces.js b/routes/listeAnnonces.js index 67ebbc2..8b453a3 100644 --- a/routes/listeAnnonces.js +++ b/routes/listeAnnonces.js @@ -12,6 +12,16 @@ router.post('/creer', function(req, res, next) { annonce.create(req, res); }); +/* Modifier une annonce*/ +router.get('/modifier/:id', function(req, res, next) { + //res.render('modifierAnnonce', { idAnnonce: req.params.id }); + annonce.findOne(req, res) +}); + +router.post('/modifier/:id', function(req, res, next) { + annonce.update(req, res); +}); + /* Redirection sur la premiere page */ router.get('/', function(req, res, next) { res.redirect('/listeAnnonces/1'); @@ -23,8 +33,8 @@ router.get('/:numPage?', function(req, res, next) { }); /* Affiche une annonce à partir de l'id */ -router.get('/annonce/:id?', function(req, res){ +/*router.get('/annonce/:id?', function(req, res){ res.render('afficherAnnonce', { idAnnonce: req.params.id }); -}); +});*/ module.exports = router; diff --git a/views/afficherAnnonce.pug b/views/afficherAnnonce.pug deleted file mode 100644 index 5d1a840..0000000 --- a/views/afficherAnnonce.pug +++ /dev/null @@ -1,17 +0,0 @@ -extends layout - -block content - a.btn(href='/listeAnnonces/1') Liste des annonces - .annonce - h1 Annonce n°#{idAnnonce} - h2 titre de l'annonce - p type de bien - p status de publication - - .photo image annonce - .desPrix - .description description de l'annonce - .prix prix - p date de dispo - - diff --git a/views/listeAnnonces.pug b/views/listeAnnonces.pug index dd47778..61ce668 100644 --- a/views/listeAnnonces.pug +++ b/views/listeAnnonces.pug @@ -8,6 +8,7 @@ block content p #{annonce.titre} p #{annonce.prixBien} p #{annonce.dateDisponibilite} + a.btn(href='/listeAnnonces/modifier/' + annonce._id) Modifier - tmp = numPage diff --git a/views/modifierAnnonce.pug b/views/modifierAnnonce.pug new file mode 100644 index 0000000..f3249d8 --- /dev/null +++ b/views/modifierAnnonce.pug @@ -0,0 +1,69 @@ +extends layout + +block content + .annonce + h1 Modification de l'annonce : + + br + + form(action="", method="post") + + label Titre :   + input(name="titre" type="text" value=data.titre required) + + br + + label Type de bien : + each typeBien in ["vente", "location"] + if data.typeBien == typeBien + input(type="radio" name="typeBien" value=typeBien checked) + else + input(type="radio" name="typeBien" value=typeBien) + label #{typeBien} + + br + + label Status de publication : + each statusPublication in ["publiée", "non publiée"] + if data.statusPublication == statusPublication + input(type="radio" name="statusPublication" value=statusPublication checked) + else + input(type="radio" name="statusPublication" value=statusPublication) + label #{statusPublication} + + br + + label Status du bien : + each statusBien in ["disponible", "loué", "vendu"] + if data.statusBien == statusBien + input(type="radio" name="statusBien" value=statusBien checked) + else + input(type="radio" name="statusBien" value=statusBien) + label #{statusBien} + + br + + label Description : + br + textarea(name="description" required) #{data.description} + + br + + label Prix :   + input(name="prixBien" type="number" min="1" value=data.prixBien required) + + br + + label Date de disponibilité :   + input(name="dateDisponibilite" type="date" value=date required) + + br + + label Photos :   + input(name="photos" type="text") + + br + + button(type="submit", name="Modifier") Modifier + + -- GitLab From 31e5cc7fe1c21538ab27ceb191f3e782d8f0de8f Mon Sep 17 00:00:00 2001 From: Jerome Laurent Date: Sun, 16 Oct 2022 15:36:43 +0200 Subject: [PATCH 5/5] =?UTF-8?q?Ajout=20possibilit=C3=A9=20de=20supprimer?= =?UTF-8?q?=20une=20annonce?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/controllerAnnonce.js | 34 ++++++++++++++++------------ routes/listeAnnonces.js | 27 +++++++++++----------- views/creerAnnonce.pug | 2 +- views/listeAnnonces.pug | 3 ++- views/modifierAnnonce.pug | 8 +++---- 5 files changed, 40 insertions(+), 34 deletions(-) diff --git a/app/controllers/controllerAnnonce.js b/app/controllers/controllerAnnonce.js index 5b87f25..1386efb 100644 --- a/app/controllers/controllerAnnonce.js +++ b/app/controllers/controllerAnnonce.js @@ -33,7 +33,7 @@ exports.create = (req, res) => { .catch(err => { res.status(500).send({ message: - err.message || "Some error occurred while creating the Tutorial." + err.message || "Some error occurred while creating the Annonce." }); }); }; @@ -91,22 +91,28 @@ exports.update = (req, res) => { }) .catch(err => { res.status(500).send({ - message: "Error updating Tutorial with id=" + id + message: "Error updating Annonce with id=" + id }); }); }; // Delete an Annonce with the specified id in the request -/*exports.delete = (req, res) => { - -}; - -// Delete all Annonces from the database. -exports.deleteAll = (req, res) => { - -}; - -// Find all published Annonces -exports.findAllPublished = (req, res) => { +exports.delete = (req, res) => { + const id = req.params.id; -};*/ \ No newline at end of file + Annonce.findByIdAndRemove(id) + .then(data => { + if (!data) { + res.status(404).send({ + message: `Cannot delete Annonce with id=${id}. Maybe Annonce was not found!` + }); + } else { + res.redirect('/listeAnnonces'); + } + }) + .catch(err => { + res.status(500).send({ + message: "Could not delete Annonce with id=" + id + }); + }); +}; \ No newline at end of file diff --git a/routes/listeAnnonces.js b/routes/listeAnnonces.js index 8b453a3..3e1b23b 100644 --- a/routes/listeAnnonces.js +++ b/routes/listeAnnonces.js @@ -1,40 +1,39 @@ var express = require('express'); var router = express.Router(); -const db = require("./../app/db") const annonce = require("./../app/controllers/controllerAnnonce") -/* Crée une annonce*/ -router.get('/creer', function(req, res, next) { +/* Crée une annonce */ +router.get('/creer', function(req, res) { res.render("creerAnnonce"); }); -router.post('/creer', function(req, res, next) { +router.post('/creer', function(req, res) { annonce.create(req, res); }); -/* Modifier une annonce*/ -router.get('/modifier/:id', function(req, res, next) { +/* Modifier une annonce */ +router.get('/modifier/:id', function(req, res) { //res.render('modifierAnnonce', { idAnnonce: req.params.id }); annonce.findOne(req, res) }); -router.post('/modifier/:id', function(req, res, next) { +router.post('/modifier/:id', function(req, res) { annonce.update(req, res); }); +/* Supprimer une annonce */ +router.get("/supprimer/:id", function(req, res) { + annonce.delete(req, res); +}); + /* Redirection sur la premiere page */ -router.get('/', function(req, res, next) { +router.get('/', function(req, res) { res.redirect('/listeAnnonces/1'); }); /* Affiche les annonces*/ -router.get('/:numPage?', function(req, res, next) { +router.get('/:numPage', function(req, res) { annonce.findAll(req, res) }); -/* Affiche une annonce à partir de l'id */ -/*router.get('/annonce/:id?', function(req, res){ - res.render('afficherAnnonce', { idAnnonce: req.params.id }); -});*/ - module.exports = router; diff --git a/views/creerAnnonce.pug b/views/creerAnnonce.pug index 2d01b39..93b80c2 100644 --- a/views/creerAnnonce.pug +++ b/views/creerAnnonce.pug @@ -4,7 +4,7 @@ block content .annonce h1 Création d'une annonce : br - form(action="/ListeAnnonces/creer", method="post") + form(method="post") label Titre :   input(name="titre" type="text" required) diff --git a/views/listeAnnonces.pug b/views/listeAnnonces.pug index 61ce668..38e6376 100644 --- a/views/listeAnnonces.pug +++ b/views/listeAnnonces.pug @@ -9,10 +9,11 @@ block content p #{annonce.prixBien} p #{annonce.dateDisponibilite} a.btn(href='/listeAnnonces/modifier/' + annonce._id) Modifier + a.btn(href='/listeAnnonces/supprimer/' + annonce._id) Supprimer - tmp = numPage - if numPage != 1 + if numPage !== 1 a.btn(href='/listeAnnonces/' + --tmp) page précédente a.btn(href='/listeAnnonces/'+ ++numPage) page suivante diff --git a/views/modifierAnnonce.pug b/views/modifierAnnonce.pug index f3249d8..c3f9598 100644 --- a/views/modifierAnnonce.pug +++ b/views/modifierAnnonce.pug @@ -6,7 +6,7 @@ block content br - form(action="", method="post") + form(method="post") label Titre :   input(name="titre" type="text" value=data.titre required) @@ -15,7 +15,7 @@ block content label Type de bien : each typeBien in ["vente", "location"] - if data.typeBien == typeBien + if data.typeBien === typeBien input(type="radio" name="typeBien" value=typeBien checked) else input(type="radio" name="typeBien" value=typeBien) @@ -25,7 +25,7 @@ block content label Status de publication : each statusPublication in ["publiée", "non publiée"] - if data.statusPublication == statusPublication + if data.statusPublication === statusPublication input(type="radio" name="statusPublication" value=statusPublication checked) else input(type="radio" name="statusPublication" value=statusPublication) @@ -35,7 +35,7 @@ block content label Status du bien : each statusBien in ["disponible", "loué", "vendu"] - if data.statusBien == statusBien + if data.statusBien === statusBien input(type="radio" name="statusBien" value=statusBien checked) else input(type="radio" name="statusBien" value=statusBien) -- GitLab