From 1a585721486e05f2d33ae938546943741f8d777f Mon Sep 17 00:00:00 2001 From: mathieu-chouchou Date: Tue, 22 Oct 2019 16:10:12 +0200 Subject: [PATCH 1/8] WIP ad creation action --- routes/ads.js | 12 ++++++++++++ views/ad_create.hbs | 46 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 routes/ads.js create mode 100644 views/ad_create.hbs diff --git a/routes/ads.js b/routes/ads.js new file mode 100644 index 0000000..05598b5 --- /dev/null +++ b/routes/ads.js @@ -0,0 +1,12 @@ +var express = require('express'); +var router = express.Router(); + +/* GET to get to get to the ad creation form */ +router.get('/ads/create', function(req, res, next) { + res.render('ad_create'); +}) +.post('/ads/create', function(req, res, next) { + +}); + +module.exports = router; diff --git a/views/ad_create.hbs b/views/ad_create.hbs new file mode 100644 index 0000000..0517de9 --- /dev/null +++ b/views/ad_create.hbs @@ -0,0 +1,46 @@ +

Créer une annonce immobilière

+ +

Créez une annonce immobilière qui pourra être consultée par les visiteurs

+ +
+ + + + +
+ Informations sur l'annonce + + +
    + A la vente + A la location +
+ + +
    + Disponible + Louée + Vendue +
+ + + € + + + + + + +
+ + +
+ Détails de l'annonce + + + + + + +
+
\ No newline at end of file -- GitLab From 11f0a972848c0e63a3e53b0c510ed58fc61cad8f Mon Sep 17 00:00:00 2001 From: mathieu-chouchou Date: Thu, 24 Oct 2019 19:43:01 +0200 Subject: [PATCH 2/8] Ad creation + ad viewing done, error handling todo --- README.md | 12 ++++++- app.js | 10 +++--- models/ad.js | 79 ++++++++++++++++++++++++++++++++++++--------- routes/ads.js | 62 ++++++++++++++++++++++++++++++++--- views/ad_create.hbs | 76 +++++++++++++++++++++++++++++-------------- views/ad_view.hbs | 16 +++++++++ 6 files changed, 204 insertions(+), 51 deletions(-) create mode 100644 views/ad_view.hbs diff --git a/README.md b/README.md index c8632ff..8efe1ad 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,13 @@ # ImmoWeb -ImmoWeb est un essai de site web classique pour gérer des annonces immobilières afin de tester entres autres la technologie Express \ No newline at end of file +ImmoWeb est un essai de site web classique pour gérer des annonces immobilières afin de tester entres autres la technologie Express + +## Installation + +run + +```shell +npm install +``` + +before anything else \ No newline at end of file diff --git a/app.js b/app.js index cfa87e3..1d17a3a 100644 --- a/app.js +++ b/app.js @@ -4,8 +4,7 @@ const path = require('path'); const cookieParser = require('cookie-parser'); const logger = require('morgan'); -const indexRouter = require('./routes/index'); -const usersRouter = require('./routes/users'); +const adsRouter = require('./routes/ads'); const app = express(); @@ -16,7 +15,9 @@ require('dotenv').config() mongoose.connect(process.env.DB_CONNECTION_STRING, { useNewUrlParser: true, useUnifiedTopology: true, -}); +}) +.catch(reason => console.log(reason)); + // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'hbs'); @@ -27,8 +28,7 @@ app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); -app.use('/', indexRouter); -app.use('/users', usersRouter); +app.use('/ads/', adsRouter) // catch 404 and forward to error handler app.use(function(req, res, next) { diff --git a/models/ad.js b/models/ad.js index 53ee85d..9de2656 100644 --- a/models/ad.js +++ b/models/ad.js @@ -8,24 +8,75 @@ const postTemplate = { } const AD_TYPE = { - SALE: "sale", - RENTAL: "rental" + VALUE: { + SALE: "sale", + RENTAL: "rental" + }, + LANG: { + SALE: "A la vente", + RENTAL: "A la location" + } }; const AD_TRANSACTION_STATUS = { - AVAILABLE: "available", - RENTED: "rented", - SOLD: "sold" + VALUE: { + AVAILABLE: "available", + RENTED: "rented", + SOLD: "sold" + }, + LANG: { + AVAILABLE: "Disponible", + RENTED: "Louée", + SOLD: "Vendue" + } }; const adSchema = new Schema({ - title: String, - type: String, - published: Boolean, - transactionStatus: String, + title: { + type: String, + required: [true, 'Le titre est obligatoire !'] + }, + type: { + type: String, + required: [true, 'Le type de bien est obligatoire !'], + enum: Object.values(AD_TYPE.VALUE), + get: l => AD_TYPE.LANG[l.toUpperCase()], + }, + published: { + type: Boolean, + required: [true, 'L\'état de publication est obligatoire !'] + }, + transactionStatus: { + type: String, + required: [true, 'Le statut de la transaction est obligatoire !'], + enum: Object.values(AD_TRANSACTION_STATUS.VALUE), + get: l => AD_TRANSACTION_STATUS.LANG[l.toUpperCase()], + validate: [ + _ => { + if(this.type === AD_TYPE.VALUE.RENTAL) { + return _ !== AD_TRANSACTION_STATUS.VALUE.SOLD + } else { + return _ !== AD_TRANSACTION_STATUS.VALUE.RENTED + } + }, + 'Votre statut de transaction doit correspondre à la transaction..' + ] + }, + price: { + type: Number, + required: [true, 'Le prix est obligatoire !'], + validate: { + validator: _ => { + return String(_).match(/^\d+(\.\d{2})?$/); + }, + msg: 'Votre prix doit ressembler à un prix !' + } + }, description: String, - price: Number, - availabilityDate: Date, + availabilityDate: { + type: Date, + get: d => d && d.toLocaleDateString() + }, pictures: [{ body: Buffer, title: String }], questions: [{ ...postTemplate, @@ -35,10 +86,8 @@ const adSchema = new Schema({ }] }); -const Ad = mongoose.model("Ad", adSchema); - module.exports = { AD_TYPE: AD_TYPE, - AD_TRANSACTIONSTATUS: AD_TRANSACTIONSTATUS, - Ad: Ad, + AD_TRANSACTIONSTATUS: AD_TRANSACTION_STATUS, + Ad: mongoose.model('Ad', adSchema) } \ No newline at end of file diff --git a/routes/ads.js b/routes/ads.js index 05598b5..01ce456 100644 --- a/routes/ads.js +++ b/routes/ads.js @@ -1,12 +1,64 @@ -var express = require('express'); -var router = express.Router(); +const express = require('express'); +const router = express.Router(); + +const adModel = require('../models/ad'); /* GET to get to get to the ad creation form */ -router.get('/ads/create', function(req, res, next) { +router.get('/create', function(req, res, next) { res.render('ad_create'); }) -.post('/ads/create', function(req, res, next) { - +.post('/create', function(req, res, next) { + console.log(req.body); + + //TODO : gérer l'upload de fichier (avec le middleware multiparty par exemple) + const newAd = new adModel.Ad({ + title: req.body.title, + type: req.body.type, + transactionStatus: req.body.transactionStatus, + price: req.body.price.replace(',', '.'), + published: req.body.published && true, + description: req.body.description, + availabilityDate: req.body.date === '' ? null : req.body.date, + //pictures: req.body.pictures === '' ? [] : req.body.pictures + }) + + newAd.save() + .then(value => res.redirect('/ads/view')) + .catch(reason => { + console.log(reason); + res.redirect('/create', reason); + }); +}) +.get('/view', function(req, res, next) { + adModel.Ad.find({ published: true }).exec() + .then(value => { + res.render('ad_view', { ads: value }); + }) + .catch(_ => { + console.log(_); + res.render('ad_view', { ads: [], errors: { load: 'Un problème est survenu lors du chargement des données' }}); + }); +}) +.get('/update/:id', function(req, res, next) { + const id = req.params.id; + const ad = new adModel.Ad.findOne({ _id: id }); + + res.render('ad_create', { ad: ad }); }); +/*adModelToAdViewModel = e => { + return { + _id: e._id, + title: e.title, + type: adModel.AD_TYPE.LANG[e.type.toUpperCase()], + transactionStatus: adModel.AD_TRANSACTIONSTATUS.LANG[e.transactionStatus.toUpperCase()], + price: e.price, + published: e.published, + description: e.description, + availabilityDate: e.availabilityDate.toLocaleDateString(), + pictures: e.pictures, + questions: e.questions, + } +}*/ + module.exports = router; diff --git a/views/ad_create.hbs b/views/ad_create.hbs index 0517de9..17e509a 100644 --- a/views/ad_create.hbs +++ b/views/ad_create.hbs @@ -3,44 +3,70 @@

Créez une annonce immobilière qui pourra être consultée par les visiteurs

- - - +
+ + + {{errors.title.message}} +
+
Informations sur l'annonce - -
    - A la vente - A la location -
- - -
    - Disponible - Louée - Vendue -
+
+ +
    + A la vente + A la location +
+ {{errors.type.message}} +
- - € +
+ +
    + Disponible + Louée + Vendue +
+ {{errors.transactionStatus.message}} +
- - +
+ + € + {{errors.price.message}} +
- - +
+ + + {{errors.published.message}} +
Détails de l'annonce - - +
+ + + {{errors.description.message}} +
- - +
+ + + {{errors.date.message}} +
+ +
+ + + {{errors.pictures.message}} +
+ +
\ No newline at end of file diff --git a/views/ad_view.hbs b/views/ad_view.hbs new file mode 100644 index 0000000..4394f46 --- /dev/null +++ b/views/ad_view.hbs @@ -0,0 +1,16 @@ +

Liste des annonces immobilières

+ +{{#each ads}} +
+

{{title}}

+

{{availabilityDate}}

+ +
    +
  • {{type}}
  • +
  • {{transactionStatus}}
  • +
  • {{price}}
  • +
+ + +
+{{/each}} \ No newline at end of file -- GitLab From 4bf338a8991b0326d95e04b45507be314fb996fb Mon Sep 17 00:00:00 2001 From: mathieu-chouchou Date: Thu, 24 Oct 2019 21:51:18 +0200 Subject: [PATCH 3/8] added tweaks to validation of ad model cleaned ads view view --- models/ad.js | 42 +++++++++++++++++++++++------------------- routes/ads.js | 2 +- views/ad_create.hbs | 2 +- views/ad_view.hbs | 4 +++- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/models/ad.js b/models/ad.js index 9de2656..c8f7e2e 100644 --- a/models/ad.js +++ b/models/ad.js @@ -1,12 +1,6 @@ const mongoose = require('mongoose'); const Schema = mongoose.Schema; -const postTemplate = { - author: String, - body: String, - date: Date, -} - const AD_TYPE = { VALUE: { SALE: "sale", @@ -31,10 +25,27 @@ const AD_TRANSACTION_STATUS = { } }; +const postTemplate = { + author: { + type: String, + trim: true + }, + body: { + type: String, + trim: true, + required: [true, 'Le corps du message ne peut être vide !'] + }, + date: { + type: Date, + get: d => d && d.toLocaleDateString() + }, +} + const adSchema = new Schema({ title: { type: String, - required: [true, 'Le titre est obligatoire !'] + required: [true, 'Le titre est obligatoire !'], + trim: true }, type: { type: String, @@ -50,17 +61,7 @@ const adSchema = new Schema({ type: String, required: [true, 'Le statut de la transaction est obligatoire !'], enum: Object.values(AD_TRANSACTION_STATUS.VALUE), - get: l => AD_TRANSACTION_STATUS.LANG[l.toUpperCase()], - validate: [ - _ => { - if(this.type === AD_TYPE.VALUE.RENTAL) { - return _ !== AD_TRANSACTION_STATUS.VALUE.SOLD - } else { - return _ !== AD_TRANSACTION_STATUS.VALUE.RENTED - } - }, - 'Votre statut de transaction doit correspondre à la transaction..' - ] + get: l => AD_TRANSACTION_STATUS.LANG[l.toUpperCase()] }, price: { type: Number, @@ -72,7 +73,10 @@ const adSchema = new Schema({ msg: 'Votre prix doit ressembler à un prix !' } }, - description: String, + description: { + type: String, + trim: true + }, availabilityDate: { type: Date, get: d => d && d.toLocaleDateString() diff --git a/routes/ads.js b/routes/ads.js index 01ce456..66ba162 100644 --- a/routes/ads.js +++ b/routes/ads.js @@ -26,7 +26,7 @@ router.get('/create', function(req, res, next) { .then(value => res.redirect('/ads/view')) .catch(reason => { console.log(reason); - res.redirect('/create', reason); + res.render('ad_create', reason); }); }) .get('/view', function(req, res, next) { diff --git a/views/ad_create.hbs b/views/ad_create.hbs index 17e509a..6ffc0ac 100644 --- a/views/ad_create.hbs +++ b/views/ad_create.hbs @@ -17,7 +17,7 @@
    A la vente - A la location + A la location
{{errors.type.message}} diff --git a/views/ad_view.hbs b/views/ad_view.hbs index 4394f46..634d98d 100644 --- a/views/ad_view.hbs +++ b/views/ad_view.hbs @@ -11,6 +11,8 @@
  • {{price}}
  • - + {{#if description}} + + {{/if}} {{/each}} \ No newline at end of file -- GitLab From bffd690c61b816018485e3cea881ce25d968c2ec Mon Sep 17 00:00:00 2001 From: mathieu-chouchou Date: Thu, 24 Oct 2019 21:51:46 +0200 Subject: [PATCH 4/8] a bit of coloriage for ad creation form --- public/stylesheets/style.css | 17 +++++++++++++++++ views/ad_create.hbs | 14 ++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 9453385..d98c0d5 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -6,3 +6,20 @@ body { a { color: #00B7FF; } + +#create-form { + overflow: hidden; + width: min-content; +} + +form > fieldset, form > button { + margin-top: 5%; +} + +div.form-input { + display: inline; +} + +span.error-message { + color: red; +} diff --git a/views/ad_create.hbs b/views/ad_create.hbs index 6ffc0ac..df4074d 100644 --- a/views/ad_create.hbs +++ b/views/ad_create.hbs @@ -1,8 +1,6 @@

    Créer une annonce immobilière

    -

    Créez une annonce immobilière qui pourra être consultée par les visiteurs

    - -
    +
    @@ -34,13 +32,17 @@
    - € +
    + € +
    {{errors.price.message}}
    - - +
    + + +
    {{errors.published.message}}
    -- GitLab From a93644cc04397a74415de64e3163e327ca410abb Mon Sep 17 00:00:00 2001 From: mathieu-chouchou Date: Thu, 24 Oct 2019 22:02:11 +0200 Subject: [PATCH 5/8] Censured my creative self to please my comrade --- models/ad.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/models/ad.js b/models/ad.js index c8f7e2e..aff0641 100644 --- a/models/ad.js +++ b/models/ad.js @@ -44,33 +44,33 @@ const postTemplate = { const adSchema = new Schema({ title: { type: String, - required: [true, 'Le titre est obligatoire !'], + required: [true, 'Titre requis'], trim: true }, type: { type: String, - required: [true, 'Le type de bien est obligatoire !'], + required: [true, 'Type de bien requis'], enum: Object.values(AD_TYPE.VALUE), get: l => AD_TYPE.LANG[l.toUpperCase()], }, published: { type: Boolean, - required: [true, 'L\'état de publication est obligatoire !'] + required: [true, 'État de publication requis'] }, transactionStatus: { type: String, - required: [true, 'Le statut de la transaction est obligatoire !'], + required: [true, 'Statut de transaction requis'], enum: Object.values(AD_TRANSACTION_STATUS.VALUE), get: l => AD_TRANSACTION_STATUS.LANG[l.toUpperCase()] }, price: { type: Number, - required: [true, 'Le prix est obligatoire !'], + required: [true, 'Prix requis'], validate: { validator: _ => { return String(_).match(/^\d+(\.\d{2})?$/); }, - msg: 'Votre prix doit ressembler à un prix !' + msg: 'Le prix doit avoir au maximum 2 chiffres de centimes' } }, description: { -- GitLab From e071588ff106e68aae177383d27462b2f4bf6c69 Mon Sep 17 00:00:00 2001 From: mathieu-chouchou Date: Tue, 29 Oct 2019 00:13:28 +0100 Subject: [PATCH 6/8] Ad update now possible, to add: role permission --- app.js | 1 + models/ad.js | 9 +++---- routes/ads.js | 59 +++++++++++++++++++++++++++++---------------- src/helpers.js | 36 +++++++++++++++++++++++++++ views/ad_create.hbs | 35 ++++++++++++++++++--------- views/ad_view.hbs | 18 ++++++++------ 6 files changed, 113 insertions(+), 45 deletions(-) create mode 100644 src/helpers.js diff --git a/app.js b/app.js index 1d17a3a..a1c2f64 100644 --- a/app.js +++ b/app.js @@ -10,6 +10,7 @@ const app = express(); const mongoose = require("mongoose"); +require('./src/helpers').addHelpers(); require('dotenv').config() mongoose.connect(process.env.DB_CONNECTION_STRING, { diff --git a/models/ad.js b/models/ad.js index aff0641..31ff942 100644 --- a/models/ad.js +++ b/models/ad.js @@ -50,8 +50,7 @@ const adSchema = new Schema({ type: { type: String, required: [true, 'Type de bien requis'], - enum: Object.values(AD_TYPE.VALUE), - get: l => AD_TYPE.LANG[l.toUpperCase()], + enum: Object.values(AD_TYPE.VALUE) }, published: { type: Boolean, @@ -60,8 +59,7 @@ const adSchema = new Schema({ transactionStatus: { type: String, required: [true, 'Statut de transaction requis'], - enum: Object.values(AD_TRANSACTION_STATUS.VALUE), - get: l => AD_TRANSACTION_STATUS.LANG[l.toUpperCase()] + enum: Object.values(AD_TRANSACTION_STATUS.VALUE) }, price: { type: Number, @@ -78,8 +76,7 @@ const adSchema = new Schema({ trim: true }, availabilityDate: { - type: Date, - get: d => d && d.toLocaleDateString() + type: Date }, pictures: [{ body: Buffer, title: String }], questions: [{ diff --git a/routes/ads.js b/routes/ads.js index 66ba162..f4c752a 100644 --- a/routes/ads.js +++ b/routes/ads.js @@ -8,26 +8,38 @@ router.get('/create', function(req, res, next) { res.render('ad_create'); }) .post('/create', function(req, res, next) { - console.log(req.body); - //TODO : gérer l'upload de fichier (avec le middleware multiparty par exemple) - const newAd = new adModel.Ad({ - title: req.body.title, - type: req.body.type, - transactionStatus: req.body.transactionStatus, - price: req.body.price.replace(',', '.'), - published: req.body.published && true, - description: req.body.description, - availabilityDate: req.body.date === '' ? null : req.body.date, - //pictures: req.body.pictures === '' ? [] : req.body.pictures - }) + const body = req.body; + const id = body.id; - newAd.save() - .then(value => res.redirect('/ads/view')) - .catch(reason => { - console.log(reason); - res.render('ad_create', reason); - }); + const formData = { + title: body.title, + type: body.type, + transactionStatus: body.transactionStatus, + price: body.price.replace(',', '.'), + published: body.published && true, + description: body.description, + availabilityDate: body.availabilityDate === '' ? null : body.availabilityDate + }; + + 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/view')) + .catch(reason => { + res.render('ad_create', reason); + }); + } else { + const newAd = new adModel.Ad(formData); + + newAd.save() + .then(value => res.redirect('/ads/view')) + .catch(reason => { + console.log(reason); + res.render('ad_create', reason); + }); + } }) .get('/view', function(req, res, next) { adModel.Ad.find({ published: true }).exec() @@ -41,9 +53,14 @@ router.get('/create', function(req, res, next) { }) .get('/update/:id', function(req, res, next) { const id = req.params.id; - const ad = new adModel.Ad.findOne({ _id: id }); - - res.render('ad_create', { ad: ad }); + + adModel.Ad.findOne({ _id: id }, function (err, ad) { + const errors = []; + if (err) { errors.push(err.message) } + if (!ad) { errors.push("L'annonce cherchée n'a pas été trouvée") } + + res.render('ad_create', { ad: ad, errors_update: errors}); + }); }); /*adModelToAdViewModel = e => { diff --git a/src/helpers.js b/src/helpers.js new file mode 100644 index 0000000..f4389d6 --- /dev/null +++ b/src/helpers.js @@ -0,0 +1,36 @@ +const hbs = require('hbs'); + +const addHelpers = function() { + hbs.registerHelper('ifEquals', function(arg1, arg2, options) { + return (arg1 === arg2) ? options.fn(this) : options.inverse(this); + }); + + hbs.registerHelper('getEnumValue', function(enumName, key, options) { + const adModule = require('../models/ad'); + + switch(enumName) { + case "type": + return adModule.AD_TYPE.LANG[key.toUpperCase()]; + case "transactionStatus": + return adModule.AD_TRANSACTIONSTATUS.LANG[key.toUpperCase()]; + default: + return "Ressource inconnue" + }; + }); + + hbs.registerHelper('dateToLocaleString', function(date, options) { + return date && date.toLocaleDateString(); + }); + + hbs.registerHelper('dateToISO', function(date, options) { + return date && date.toISOString().split('T')[0]; + }); + + hbs.registerHelper('formatPrice', function(price, options) { + return price && price.toFixed(2).replace('.', ','); + }) +} + +module.exports = { + addHelpers: addHelpers +} \ No newline at end of file diff --git a/views/ad_create.hbs b/views/ad_create.hbs index df4074d..b594896 100644 --- a/views/ad_create.hbs +++ b/views/ad_create.hbs @@ -1,5 +1,11 @@

    Créer une annonce immobilière

    +
    + {{#each errors_update}} +

    {{this}}

    + {{/each}} +
    +
    @@ -12,10 +18,12 @@ Informations sur l'annonce
    - +
      - A la vente - A la location + A la vente + A la location
    {{errors.type.message}}
    @@ -23,9 +31,12 @@
      - Disponible - Louée - Vendue + Disponible + Louée + Vendue
    {{errors.transactionStatus.message}}
    @@ -33,7 +44,7 @@
    - € +
    {{errors.price.message}}
    @@ -41,7 +52,7 @@
    - +
    {{errors.published.message}}
    @@ -53,13 +64,13 @@
    - + {{errors.description.message}}
    - - + + {{errors.date.message}}
    @@ -70,5 +81,7 @@
    + + \ No newline at end of file diff --git a/views/ad_view.hbs b/views/ad_view.hbs index 634d98d..9865438 100644 --- a/views/ad_view.hbs +++ b/views/ad_view.hbs @@ -3,16 +3,20 @@ {{#each ads}}

    {{title}}

    -

    {{availabilityDate}}

    +

    {{dateToLocaleString availabilityDate}}

      -
    • {{type}}
    • -
    • {{transactionStatus}}
    • -
    • {{price}}
    • +
    • {{getEnumValue "type" type}}
    • +
    • {{getEnumValue "transactionStatus" transactionStatus}}
    • +
    • {{formatPrice price}}
    - {{#if description}} - - {{/if}} +
    + {{#if description}} + + {{/if}} +
    + + Mettre à jour
    {{/each}} \ No newline at end of file -- GitLab From 082723dc0b2bd033d01ea10ea9a0a50f30415b58 Mon Sep 17 00:00:00 2001 From: mathieu-chouchou Date: Tue, 29 Oct 2019 20:20:27 +0100 Subject: [PATCH 7/8] Mise en place de la suppression des annonces Closes #4 --- app.js | 2 -- public/javascript/adDelete.js | 17 +++++++++++++++++ routes/ads.js | 12 ++++++++++++ routes/index.js | 9 --------- views/ad_view.hbs | 5 ++++- views/index.hbs | 2 -- 6 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 public/javascript/adDelete.js delete mode 100644 routes/index.js delete mode 100644 views/index.hbs diff --git a/app.js b/app.js index a07cdc9..f838110 100644 --- a/app.js +++ b/app.js @@ -9,7 +9,6 @@ const flash = require("connect-flash"); const LocalStrategy = require("passport-local").Strategy; const authRouter = require("./routes/auth"); -const indexRouter = require('./routes/index'); const usersRouter = require('./routes/users'); const adsRouter = require('./routes/ads'); @@ -67,7 +66,6 @@ app.use(flash()); app.use(passport.initialize()); app.use(passport.session()); -app.use('/', indexRouter); app.use('/', authRouter); app.use('/users', usersRouter); app.use('/ads', adsRouter); diff --git a/public/javascript/adDelete.js b/public/javascript/adDelete.js new file mode 100644 index 0000000..5741ee7 --- /dev/null +++ b/public/javascript/adDelete.js @@ -0,0 +1,17 @@ +function deleteAd(id) { + const response = fetch(`delete/${id}`, { + method: 'DELETE', + mode: 'cors', + cache: 'no-cache', + credentials: 'same-origin', + headers: { + 'Content-Type': 'application/json' + }, + redirect: 'follow', + referrer: '' + }); + + response + .then(value => window.location = value.url) + .catch(reason => console.error(reason)); +} \ No newline at end of file diff --git a/routes/ads.js b/routes/ads.js index 076f424..67cdec5 100644 --- a/routes/ads.js +++ b/routes/ads.js @@ -62,6 +62,18 @@ router.get('/create', function(req, res, next) { res.render('ad_create', { ad: ad, errors_update: errors}); }); +}) +.delete('/delete/:id', function(req, res, next) { + const id = req.params.id; + + adModel.Ad.deleteOne({_id: id}).exec() + .then(value => { + req.flash('success', "L'annonce a bien été supprimée"); + }) + .catch(reason => { + req.flash('error', "L'annonce n'a pas pu être supprimée"); + }) + .finally(() => res.redirect(303, '/ads/')); }); /*adModelToAdViewModel = e => { diff --git a/routes/index.js b/routes/index.js deleted file mode 100644 index 1639e8e..0000000 --- a/routes/index.js +++ /dev/null @@ -1,9 +0,0 @@ -var express = require('express'); -var router = express.Router(); - -/* GET home page. */ -router.get('/', function(req, res, next) { - res.render('index', { title: 'Express', messages: req.flash("info") }); -}); - -module.exports = router; diff --git a/views/ad_view.hbs b/views/ad_view.hbs index 9865438..8a90b55 100644 --- a/views/ad_view.hbs +++ b/views/ad_view.hbs @@ -18,5 +18,8 @@
    Mettre à jour + -{{/each}} \ No newline at end of file +{{/each}} + + \ No newline at end of file diff --git a/views/index.hbs b/views/index.hbs deleted file mode 100644 index 950a37c..0000000 --- a/views/index.hbs +++ /dev/null @@ -1,2 +0,0 @@ -

    {{title}}

    -

    Welcome to {{title}}{{#if user}}, {{user.name}}{{/if}}

    -- GitLab From 413efd1707b3fefae96a8704952fecd1fb4821c2 Mon Sep 17 00:00:00 2001 From: mathieu-chouchou Date: Tue, 29 Oct 2019 20:37:42 +0100 Subject: [PATCH 8/8] Ajout de la redirection home vers vue des annonces --- app.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 7f2f8ad..5feccbd 100644 --- a/app.js +++ b/app.js @@ -80,7 +80,10 @@ app.use(passport.session()); app.use('/', authRouter); app.use('/users', usersRouter); -app.use('/ads', adsRouter); +app.use('/ads', adsRouter) +.get('/', function(req, res, next) { + res.redirect('/ads/'); +}); // catch 404 and forward to error handler app.use(function(req, res, next) { -- GitLab