87 lines
1.8 KiB
JavaScript
87 lines
1.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'
|
|
});
|
|
|
|
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.use(express.static('static'))
|
|
|
|
app.listen(PORT, function () {
|
|
console.log(`Listening on ${PORT}`)
|
|
})
|