diff --git a/app/controllers/controllerAnnonce.js b/app/controllers/controllerAnnonce.js index d7b471847027382039ad68d1be8f7ee92758cc95..1386efb624b3e6320d22bfee97f1a9c39ef351e2 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 @@ -31,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." }); }); }; @@ -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,21 +70,49 @@ exports.findOne = (req, res) => { }; // Update an Annonce by the id in the request -/*exports.update = (req, res) => { - -}; - -// Delete an Annonce with the specified id in the request -exports.delete = (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; -// Delete all Annonces from the database. -exports.deleteAll = (req, res) => { + 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 Annonce with id=" + id + }); + }); }; -// Find all published Annonces -exports.findAllPublished = (req, res) => { +// Delete an Annonce with the specified id in the request +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 67ebbc2bc089e9d3695cd02169b7805c7ba62786..3e1b23b703fc10078e5803d67f90fe17f70732f3 100644 --- a/routes/listeAnnonces.js +++ b/routes/listeAnnonces.js @@ -1,30 +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) { + //res.render('modifierAnnonce', { idAnnonce: req.params.id }); + annonce.findOne(req, res) +}); + +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/afficherAnnonce.pug b/views/afficherAnnonce.pug deleted file mode 100644 index 5d1a8404557542471390987a4800a3134806cb3d..0000000000000000000000000000000000000000 --- 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/creerAnnonce.pug b/views/creerAnnonce.pug index 3216fbba3bfe357524460390b7eb61b7b856da3f..93b80c20551f9b1e30fa878272979c607d5b15c3 100644 --- a/views/creerAnnonce.pug +++ b/views/creerAnnonce.pug @@ -4,29 +4,49 @@ block content .annonce h1 Création d'une annonce : br - form(action="/ListeAnnonces/creer", method="post") - label(for="titre") Titre :   - input(name="titre" type="text") + form(method="post") + 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 diff --git a/views/listeAnnonces.pug b/views/listeAnnonces.pug index dd47778dc45b7f615f71e9ac10a634659baff6f9..38e637627f24ead67779ec3a753f6371981027bc 100644 --- a/views/listeAnnonces.pug +++ b/views/listeAnnonces.pug @@ -8,10 +8,12 @@ block content p #{annonce.titre} 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 new file mode 100644 index 0000000000000000000000000000000000000000..c3f959849d0c5435a773862c8490ca9d17653523 --- /dev/null +++ b/views/modifierAnnonce.pug @@ -0,0 +1,69 @@ +extends layout + +block content + .annonce + h1 Modification de l'annonce : + + br + + form(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 + +