From e4d2327f533ae6fac7fca4d9520c7bd76b34281d Mon Sep 17 00:00:00 2001 From: florian-boubou Date: Fri, 1 Nov 2019 11:31:31 +0100 Subject: [PATCH 1/3] Ask questions on ad page --- models/ad.js | 1 - routes/ads.js | 122 +++++++++++++++++++++++++++------------------ views/ads/show.hbs | 29 +++++++++++ 3 files changed, 103 insertions(+), 49 deletions(-) diff --git a/models/ad.js b/models/ad.js index 7ba7976..cd5374a 100644 --- a/models/ad.js +++ b/models/ad.js @@ -37,7 +37,6 @@ const postTemplate = { }, date: { type: Date, - get: (d) => d && d.toLocaleDateString(), }, }; diff --git a/routes/ads.js b/routes/ads.js index d2149d9..734e1d7 100644 --- a/routes/ads.js +++ b/routes/ads.js @@ -14,54 +14,53 @@ router .get('/create', authorize('agent'), function(req, res) { res.render('ads/create'); }) - .post( - '/create', - authorize('agent'), - upload.array('pictures', 3), - function(req, res, next) { - const body = req.body; - const id = body.id; - - const formData = { - title: body.title, - type: body.type, - transactionStatus: body.transactionStatus, - price: body.price.replace(',', '.'), - published: body.published === 'on', - description: body.description, - availabilityDate: - body.availabilityDate === '' ? null : body.availabilityDate, - }; - - if (req.files.length) { - formData.pictures = req.files.map((f) => ({ - name: f.fieldName, - body: f.buffer, - })); - } - - if (id) { - // Peut-être charger l'objet en amont et le retourner si erreur ? - - adModel.Ad.updateOne({_id: id}, {$set: formData}) - .exec() - .then((value) => res.redirect('/ads/')) - .catch((reason) => { - res.render('ads/create', reason); - }); - } else { - const newAd = new adModel.Ad(formData); - - newAd - .save() - .then((value) => res.redirect('/ads/')) - .catch((reason) => { - console.log(reason); - res.render('ads/create', reason); - }); - } - }, - ) + .post('/create', authorize('agent'), upload.array('pictures', 3), function( + req, + res, + next, + ) { + const body = req.body; + const id = body.id; + + const formData = { + title: body.title, + type: body.type, + transactionStatus: body.transactionStatus, + price: body.price.replace(',', '.'), + published: body.published === 'on', + description: body.description, + availabilityDate: + body.availabilityDate === '' ? null : body.availabilityDate, + }; + + if (req.files.length) { + formData.pictures = req.files.map((f) => ({ + name: f.fieldName, + body: f.buffer, + })); + } + + if (id) { + // Peut-être charger l'objet en amont et le retourner si erreur ? + + adModel.Ad.updateOne({_id: id}, {$set: formData}) + .exec() + .then((value) => res.redirect('/ads/')) + .catch((reason) => { + res.render('ads/create', reason); + }); + } else { + const newAd = new adModel.Ad(formData); + + newAd + .save() + .then((value) => res.redirect('/ads/')) + .catch((reason) => { + console.log(reason); + res.render('ads/create', reason); + }); + } + }) .get('/update/:id', authorize('agent'), function(req, res, next) { const id = req.params.id; @@ -79,6 +78,33 @@ router }) .get('/delete/:id', authorize('agent'), deleteAdAction) .delete('/delete/:id', authorize('agent'), deleteAdAction) + .post('/:id/questions/create', + authorize('client'), + function(req, res, next) { + const question = { + author: req.user.name, + body: req.body.body, + date: new Date(), + }; + + adModel.Ad.findByIdAndUpdate(req.params.id, { + $push: { + questions: question, + }, + }) + .then((ad) => { + res.status(201); + res.redirect('/ads/' + ad.id); + }) + .catch((err) => { + console.error(err); + req.flash( + 'info', + 'Une erreur est survenue lors du chargement des données.', + ); + res.redirect('/'); + }); + }) .get('/:id/picture/:index', function(req, res, next) { adModel.Ad.findOne({_id: req.params.id}) .then((ad) => { diff --git a/views/ads/show.hbs b/views/ads/show.hbs index aa87507..9c3d1fd 100644 --- a/views/ads/show.hbs +++ b/views/ads/show.hbs @@ -20,6 +20,35 @@

Description

{{ad.description}}

+
+
+

Questions

+
+ {{#authorize "client"}} +
+

Poser une question

+
+
+ +
+
+ {{/authorize}} +
+ {{#with ad as |ad|}} + {{#each ad.questions}} +
+
+

{{body}}

+
+ +
+ {{else}} +

Aucune question.

+ {{/each}} + {{/with}} +
+
{{#authorize "agent"}}