Metzploreur/routes/posts.js

113 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-11-01 02:01:18 +00:00
const express = require('express');
const mongodb = require('mongodb')
const bcrypt = require('bcryptjs');
2023-11-01 21:50:28 +00:00
const multer = require('multer')
const storageConfig = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'images');
},
filename: function (req, file, cb) {
cb(null, Date.now() + '-' + file.originalname);
}
});
const upload = multer({storage: storageConfig});
2023-11-01 02:01:18 +00:00
const db = require('../data/database');
const router = express.Router();
2023-11-01 20:03:07 +00:00
2023-11-01 21:50:28 +00:00
router.post('/creerPost', async function (req,res) {
2023-11-01 02:01:18 +00:00
const postData = req.body;
const enteredTitre = postData.titre;
const enteredCommentaire = postData.commentairePost;
2023-11-01 20:03:07 +00:00
const enterdRecompense = postData.recompense;
2023-11-01 21:50:28 +00:00
2023-11-01 02:01:18 +00:00
const post ={
titre: enteredTitre,
commentaire: enteredCommentaire,
2023-11-01 20:03:07 +00:00
recompense: enterdRecompense,
2023-11-01 02:01:18 +00:00
isFinish: false,
}
await db.getDb().collection('posts').insertOne(post);
2023-11-01 20:03:07 +00:00
return res.redirect('/admin');
2023-11-01 02:01:18 +00:00
})
2023-11-01 16:33:25 +00:00
router.get('/hotLine', async function (req,res) {
if (!req.session.isAuthenticated) {
return res.status(401).render('401');
}
const postData = await db.getDb().collection('posts').find().toArray();
2023-11-02 02:53:46 +00:00
const commandeData = await db.getDb().collection('commandes').find().toArray();
res.render('hotLine', {postData: postData, commandeData: commandeData})
2023-11-01 02:01:18 +00:00
})
router.post('/commandeCrepe', async function (req, res) {
const crepeData = req.body;
const enteredCommentaire = crepeData.commentaire;
const enteredGarniture = crepeData.garniture;
2023-11-02 02:53:46 +00:00
const nom = req.session.user.nom;
const prenom = req.session.user.prenom;
2023-11-01 02:01:18 +00:00
const crepeCommande = {
garniture: enteredGarniture,
commentaire: enteredCommentaire,
2023-11-02 02:53:46 +00:00
nom: nom,
prenom: prenom,
2023-11-01 02:01:18 +00:00
finish: false
}
2023-11-02 02:53:46 +00:00
await db.getDb().collection('commandes').insertOne(crepeCommande);
2023-11-01 02:01:18 +00:00
res.redirect("/hotLine");
})
2023-11-01 21:50:28 +00:00
router.post('/submitResolution/:postId', upload.single('image'), async function (req, res) {
2023-11-01 23:03:46 +00:00
const postId = req.params.postId; // Assuming postId is obtained from the request
2023-11-01 20:03:07 +00:00
const resolutionData = req.body;
const enteredDescription = resolutionData.description;
const user = req.session.user;
const nom = user.nom;
const prenom = user.prenom;
2023-11-01 21:50:28 +00:00
const file = req.file;
const path = file.path;
2023-11-01 20:03:07 +00:00
const resolution = {
2023-11-01 23:03:46 +00:00
postId: postId,
description: enteredDescription,
nom: nom,
prenom: prenom,
imagePath: path
}
const result = await db.getDb().collection('res').insertOne(resolution);
return res.redirect('/hotLine')
})
2023-11-01 20:03:07 +00:00
2023-11-02 19:15:23 +00:00
router.post('/accepterPost/:id', async function (req, res) {
2023-11-02 00:37:15 +00:00
const postId = req.params.id;
const winner = req.body.utilisateur;
2023-11-02 19:15:23 +00:00
const ObjectID = mongodb.ObjectId;
const postObjectId = new ObjectID(postId);
await db.getDb().collection('posts').updateOne({ _id: postObjectId }, { $set: { isFinish: true, winner: winner } });
await db.getDb().collection('res').updateOne({ _id: postObjectId }, { $set: { winner: winner } });
return res.redirect('/admin');
});
router.post('/supprimerPost/:id', async function (req, res) {
const postId = req.params.id;
const ObjectID = mongodb.ObjectId;
const postObjectId = new ObjectID(postId);
await db.getDb().collection('posts').deleteOne({ _id: postObjectId });
return res.redirect('/admin');
});
2023-11-02 02:53:46 +00:00
2023-11-01 02:01:18 +00:00
module.exports = router;