Skip to content
Snippets Groups Projects
Commit 5256856d authored by Martin Lehoux's avatar Martin Lehoux
Browse files

ready with auth

parent 2b90a3ed
No related branches found
No related tags found
No related merge requests found
node_modules
.vscode
config.json
\ No newline at end of file
......@@ -48,7 +48,10 @@ app.use((req, res, next) => {
else res.redirect("/");
});
app.get("/clients", (req, res) => {
Client.find({}, (err, clients) => res.render("clients", { clients, loggedIn: Boolean(req.session.loggedIn) }));
const q = Client.find();
if (req.query.membre) q.find({ membre: true });
else if (req.query.search) q.find({ $text: { $search: req.query.search } });
q.exec().then(clients => res.render("clients", { clients, loggedIn: Boolean(req.session.loggedIn) }));
});
app.get("/clients/nouveau", (req, res) => res.render("nouveau-client", { loggedIn: Boolean(req.session.loggedIn) }));
app.post("/clients", (req, res) => {
......@@ -66,6 +69,21 @@ app.get("/clients/:id", (req, res) => {
])
.then(([client, produits]) => res.render("client", { client, produits, loggedIn: Boolean(req.session.loggedIn) }));
});
app.get("/clients/:id/modifier", (req, res) => {
Client
.findById(req.params.id).exec()
.then(client => res.render("modifier-client", { client }));
});
app.post("/clients/:id/modifier", (req, res) => {
if (req.body.membre) req.body.membre = true;
else req.body.membre = undefined;
Client.findOneAndUpdate({ _id: req.params.id }, { ...req.body }).exec();
res.redirect("/clients/"+req.params.id);
});
app.post("/clients/:id/supprimer", (req, res) => {
Client.findOneAndDelete({ _id: req.params.id }).exec()
res.redirect("/clients");
})
app.post("/clients/:id/operations", (req, res) => {
Client
.findOneAndUpdate({ _id: req.params.id }, { $push: { operations: { montant: req.body.montant } }, $inc: { solde: req.body.montant } }, (err, client) => console.log(err));
......@@ -117,7 +135,7 @@ app.post("/produits/:id/modifier", (req, res) => {
Produit.findOneAndUpdate({ _id: req.params.id }, { image: image.name }).exec();
}
Produit.findOneAndUpdate({ _id: req.params.id }, { ...req.body }).exec();
res.redirect("/produits");
res.redirect("/produits/"+req.params.id);
});
app.post("/produits/:id/supprimer", (req, res) => {
Produit.findOneAndDelete({ _id: req.params.id }).exec()
......
......@@ -13,4 +13,6 @@ const Client = new mongoose.Schema({
commandes: [Commande]
});
Client.index({ "$**": 'text' });
module.exports = mongoose.model('Client', Client);
\ No newline at end of file
......@@ -44,7 +44,7 @@ block content
.ten.wide.column
h3.ui.header Dernières opérations
table.ui.table
table.ui.table.compact
thead
tr
th Date
......@@ -59,7 +59,7 @@ block content
//- p= client.commandes
h3.ui.header Dernières commandes
table.ui.table
table.ui.table.compact
thead
tr
th Date
......
......@@ -3,8 +3,19 @@ extends layout.pug
block content
.ui.container
h1.ui.header Clients
p
a.ui.button(href="/clients/nouveau") Créer un nouveau client
form.ui.form(action="/clients" method="get")
.fields
.twelve.wide.field
input(type="text" name="search" placeholder="Recherche")
.two.wide.field
input.ui.button(type="submit" value="Chercher")
.two.wide.field
a.ui.button.yellow(href="/clients?membre=true") Toucan™
table.ui.table
tbody
each client in clients
......@@ -12,8 +23,8 @@ block content
td.selectable
a(href="/clients/"+client._id).class #{client.prenom} #{client.nom}
td.right.align.collapsing
.ui.label #{client.solde.toFixed(2)} €
.ui.label(class=client.solde<0 ? "red" : "green") #{client.solde.toFixed(2)} €
if client.membre
.ui.label
.ui.label.yellow
i.certificate.icon
| Toucan™
extends layout.pug
block content
.ui.container
h1.ui.header Modifier #{client.prenom} #{client.nom}
form.ui.form(action="/clients/"+client._id+"/modifier" method="post")
.ui.field
label Prénom
input(name="prenom" placeholder="Prénom" type="text" value=client.prenom)
.ui.field
label Nom
input(name="nom" placeholder="Nom" type="text" value=client.nom)
.ui.field
label Surnom
input(name="surnom" placeholder="Surnom (optionnel)" type="text" value=client.surnom)
.ui.field
label Promotion
input(name="promotion" placeholder="Promotion" type="text" value=client.promotion)
.ui.field
.ui.checkbox
input(name="membre" type="checkbox" checked=client.membre)
label Membre du Toucan
.ui.field
input.ui.button(type="submit" value="Modifier")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment