177 lines
3.8 KiB
JavaScript
177 lines
3.8 KiB
JavaScript
const DS = require('@seald-io/nedb');
|
|
const express = require('express');
|
|
const { create } = require('express-handlebars');
|
|
const path = require('path');
|
|
|
|
const app = express();
|
|
const PORT = 3000;
|
|
|
|
const DB = new DS({
|
|
filename: 'data/cars.db',
|
|
autoload: true
|
|
});
|
|
|
|
const hbs = create({
|
|
defaultLayout: 'main.hbs',
|
|
extname: '.hbs',
|
|
helpers: {
|
|
equ(p, q) {
|
|
return p == q;
|
|
}
|
|
}
|
|
});
|
|
|
|
app.engine('.hbs', hbs.engine);
|
|
app.set('view engine', '.hbs');
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
|
|
app.use(express.urlencoded({ extended: true }));
|
|
app.use(express.json());
|
|
|
|
const model = [
|
|
"Ford Fiesta",
|
|
"Fiat Siesta",
|
|
"Volkswagen Polo",
|
|
"Volvo V70",
|
|
"Volvo S60",
|
|
"Newag Impuls"
|
|
];
|
|
|
|
/// routingi
|
|
|
|
app.get("/", function (req, res) {
|
|
res.render('index.hbs');
|
|
});
|
|
|
|
app.post("/add", function (req, res) {
|
|
let doc = {
|
|
ubezpieczony: req.body.ubezpieczony ? "TAK" : "NIE",
|
|
benzyna: req.body.benzyna ? "TAK" : "NIE",
|
|
uszkodzony: req.body.uszkodzony ? "TAK" : "NIE",
|
|
naped4x4: req.body.naped4x4 ? "TAK" : "NIE",
|
|
model: req.body.model
|
|
};
|
|
|
|
DB.insert(doc, function (err, new_doc) {
|
|
// ok
|
|
res.redirect(`/add?model=${doc.model}&id=${new_doc._id}`);
|
|
});
|
|
});
|
|
|
|
app.get("/add", function (req, res) {
|
|
let r_model = model[Math.floor(Math.random() * model.length)]
|
|
|
|
let ctx = {
|
|
"model": r_model
|
|
};
|
|
|
|
if (req.query.model && req.query.id) {
|
|
ctx.added_model = req.query.model
|
|
ctx.added_id = req.query.id
|
|
}
|
|
|
|
res.render("add.hbs", ctx);
|
|
});
|
|
|
|
app.get("/list", function (req, res) {
|
|
DB.find({}, function (err, docs) {
|
|
let ctx = {
|
|
car: docs
|
|
};
|
|
res.render('list.hbs', ctx);
|
|
});
|
|
});
|
|
|
|
app.post("/delete", function (req, res) {
|
|
console.log(req.body);
|
|
|
|
if (req.body.deletions) {
|
|
const deletions = [req.body.deletions].flat();
|
|
|
|
console.log(deletions);
|
|
|
|
deletions.forEach(deletion_id => {
|
|
DB.remove({ _id: deletion_id }, {}, function (err, numRemoved) {
|
|
console.log("Usunięto " + numRemoved + " dokument.");
|
|
});
|
|
});
|
|
}
|
|
|
|
res.redirect("/delete");
|
|
});
|
|
|
|
app.get("/delete", function (req, res) {
|
|
DB.find({}, function (err, docs) {
|
|
let ctx = {
|
|
car: docs
|
|
};
|
|
res.render('delete.hbs', ctx);
|
|
});
|
|
});
|
|
|
|
app.get("/deleteSpecific", function (req, res) {
|
|
if (req.query.id) {
|
|
DB.remove({ _id: req.query.id }, {}, function (err, numRemoved) {
|
|
console.log("Usunięto " + numRemoved + " dokument.");
|
|
});
|
|
}
|
|
|
|
res.redirect("/delete");
|
|
});
|
|
|
|
app.get("/deleteAll", function (req, res) {
|
|
DB.remove({}, { multi: true }, function (err, numRemoved) {
|
|
console.log("usunięto dokumentów: " + numRemoved);
|
|
});
|
|
|
|
res.redirect("/delete");
|
|
});
|
|
|
|
app.post("/edit", function (req, res) {
|
|
DB.update({ _id: req.body._id }, { $set: req.body }, {}, function (err, numUpdated) {
|
|
console.log('Zaktualizowano ' + numUpdated + ' dokumentów.');
|
|
});
|
|
|
|
res.redirect("/edit");
|
|
});
|
|
|
|
app.get("/edit", function (req, res) {
|
|
DB.find({}, function (err, docs) {
|
|
let ctx = {
|
|
car: []
|
|
};
|
|
|
|
docs.forEach(doc => {
|
|
if (doc._id == req.query.id) {
|
|
doc.edycja = true;
|
|
}
|
|
ctx.car.push(doc);
|
|
})
|
|
|
|
res.render('edit.hbs', ctx);
|
|
});
|
|
});
|
|
|
|
app.post("/search", function (req, res) {
|
|
console.log(req.body);
|
|
|
|
// do dokończenia tutaj
|
|
});
|
|
|
|
app.get("/search", function (req, res) {
|
|
DB.find({}, function (err, docs) {
|
|
let ctx = {
|
|
car: docs
|
|
};
|
|
res.render('search.hbs', ctx);
|
|
});
|
|
})
|
|
|
|
/// --------
|
|
|
|
app.use(express.static('static'))
|
|
|
|
app.listen(PORT, function () {
|
|
console.log(`Listening on ${PORT}`)
|
|
})
|