Metzploreur/routes/posts.js
2023-11-02 01:37:15 +01:00

96 lines
No EOL
2.7 KiB
JavaScript

const express = require('express');
const mongodb = require('mongodb')
const bcrypt = require('bcryptjs');
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});
const db = require('../data/database');
const router = express.Router();
router.post('/creerPost', async function (req,res) {
const postData = req.body;
const enteredTitre = postData.titre;
const enteredCommentaire = postData.commentairePost;
const enterdRecompense = postData.recompense;
const post ={
titre: enteredTitre,
commentaire: enteredCommentaire,
recompense: enterdRecompense,
isFinish: false,
}
await db.getDb().collection('posts').insertOne(post);
return res.redirect('/admin');
})
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();
res.render('hotLine', {postData: postData})
})
router.post('/commandeCrepe', async function (req, res) {
const crepeData = req.body;
const enteredCommentaire = crepeData.commentaire;
const enteredGarniture = crepeData.garniture;
const crepeCommande = {
garniture: enteredGarniture,
commentaire: enteredCommentaire,
finish: false
}
await db.getDb().collection('commande').insertOne(crepeCommande);
res.redirect("/hotLine");
})
router.post('/submitResolution/:postId', upload.single('image'), async function (req, res) {
const postId = req.params.postId; // Assuming postId is obtained from the request
const resolutionData = req.body;
const enteredDescription = resolutionData.description;
const user = req.session.user;
const nom = user.nom;
const prenom = user.prenom;
const file = req.file;
const path = file.path;
const resolution = {
postId: postId,
description: enteredDescription,
nom: nom,
prenom: prenom,
imagePath: path
}
const filter = {id: postId};
const updateDoc = {
$set: {[`resolutions.${postId}`]: resolution}
};
const result = await db.getDb().collection('res').insertOne(resolution);
return res.redirect('/hotLine')
})
router.post('/accepter/:id', async function (req, res) {
const postId = req.params.id;
const winner = req.body.utilisateur;
await db.getDb().collection('post').updateOne({_id: postId}, {isFinish: true});
await db.getDb().collection('res').updateOne({_id: postId}, {winner: winner});
res.redirect('/admin')
})
module.exports = router;