diff --git a/Lekcja_08/data/cars.db b/Lekcja_08/data/cars.db index 9f376c2..1919ecf 100644 --- a/Lekcja_08/data/cars.db +++ b/Lekcja_08/data/cars.db @@ -1,13 +1,10 @@ -{"ubezpieczony":"NIE","benzyna":"NIE","uszkodzony":"TAK","naped4x4":"NIE","model":"Fiat Siesta","_id":"3RzJJX56VlHImvio"} -{"ubezpieczony":"NIE","benzyna":"NIE","uszkodzony":"NIE","naped4x4":"TAK","model":"Newag Impuls","_id":"6PK2fLdLhPZEeDPY"} -{"ubezpieczony":"NIE","benzyna":"NIE","uszkodzony":"NIE","naped4x4":"TAK","model":"Volvo V70","_id":"6jnh5hrirnvSKuHr"} -{"ubezpieczony":"TAK","benzyna":"NIE","uszkodzony":"NIE","naped4x4":"NIE","model":"Newag Impuls","_id":"DaNohMWhUwKNZCVi"} -{"ubezpieczony":"TAK","benzyna":"NIE","uszkodzony":"TAK","naped4x4":"NIE","model":"Volvo V70","_id":"IaFTfZzBsgwAkVcr"} -{"ubezpieczony":"NIE","benzyna":"NIE","uszkodzony":"TAK","naped4x4":"NIE","model":"Volvo V70","_id":"Kp8Yecf7rZBgzl19"} -{"ubezpieczony":"NIE","benzyna":"NIE","uszkodzony":"TAK","naped4x4":"NIE","model":"Newag Impuls","_id":"PydJXj7NnHLfaJDh"} -{"ubezpieczony":"NIE","benzyna":"NIE","uszkodzony":"TAK","naped4x4":"NIE","model":"Ford Fiesta","_id":"TyGi23AD9aqtLhyw"} -{"ubezpieczony":"NIE","benzyna":"TAK","uszkodzony":"NIE","naped4x4":"TAK","model":"Ford Fiesta","_id":"byQKYXe8ZDubEmnQ"} -{"ubezpieczony":"NIE","benzyna":"TAK","uszkodzony":"TAK","naped4x4":"NIE","model":"Volvo S60","_id":"dza3RMhhHMcwW7Fo"} -{"ubezpieczony":"NIE","benzyna":"NIE","uszkodzony":"TAK","naped4x4":"NIE","model":"Volkswagen Polo","_id":"ehZZRhPjKzFH8t2u"} -{"ubezpieczony":"NIE","benzyna":"NIE","uszkodzony":"NIE","naped4x4":"NIE","model":"Volkswagen Polo","_id":"ph8rQnhL6m2xq61A"} -{"ubezpieczony":"NIE","benzyna":"NIE","uszkodzony":"TAK","naped4x4":"NIE","model":"Fiat Siesta","_id":"ynPUlOeBPPntM60s"} +{"ubezpieczony":"NIE","benzyna":"NIE","uszkodzony":"BRAK DANYCH","naped4x4":"NIE","model":"EN57","_id":"3WYiUbXyKdNCora2"} +{"ubezpieczony":"NIE","benzyna":"BRAK DANYCH","uszkodzony":"TAK","naped4x4":"TAK","model":"Newag Impuls","_id":"Njg77UGgaUwa7BNO"} +{"ubezpieczony":"NIE","benzyna":"NIE","uszkodzony":"NIE","naped4x4":"NIE","model":"Volvo S60","_id":"Ybsk8Tp8XVEx9YCI"} +{"ubezpieczony":"NIE","benzyna":"NIE","uszkodzony":"NIE","naped4x4":"TAK","model":"Ford Fiesta","_id":"cTR4SUI0UrZM1MzX"} +{"ubezpieczony":"TAK","benzyna":"TAK","uszkodzony":"TAK","naped4x4":"NIE","model":"Volkswagen Polo","_id":"eSeokpNe3Hr5xM92"} +{"ubezpieczony":"NIE","benzyna":"TAK","uszkodzony":"TAK","naped4x4":"NIE","model":"Volvo S60","_id":"kpIYNiYxGkR0Pjeg"} +{"ubezpieczony":"NIE","benzyna":"NIE","uszkodzony":"NIE","naped4x4":"NIE","model":"Volvo V70","_id":"la6azGJJdcMGsuiq"} +{"ubezpieczony":"NIE","benzyna":"TAK","uszkodzony":"TAK","naped4x4":"TAK","model":"Newag Impuls","_id":"q1epOgXNeFQCF5wb"} +{"ubezpieczony":"TAK","benzyna":"NIE","uszkodzony":"TAK","naped4x4":"NIE","model":"Ford Fiesta","_id":"qfpGtQuIRrCFPkJb"} +{"ubezpieczony":"TAK","benzyna":"NIE","uszkodzony":"NIE","naped4x4":"TAK","model":"Fiat Siesta","_id":"qiboa7ygHrMvH6I6"} diff --git a/Lekcja_08/index.js b/Lekcja_08/index.js index 1533c2a..8f7e7dd 100644 --- a/Lekcja_08/index.js +++ b/Lekcja_08/index.js @@ -13,7 +13,12 @@ const DB = new DS({ const hbs = create({ defaultLayout: 'main.hbs', - extname: '.hbs' + extname: '.hbs', + helpers: { + equ(p, q) { + return p == q; + } + } }); app.engine('.hbs', hbs.engine); @@ -74,9 +79,94 @@ app.get("/list", function (req, res) { 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')) diff --git a/Lekcja_08/node_modules/router/lib/layer.js b/Lekcja_08/node_modules/router/lib/layer.js index 6a4408f..ea8fe9e 100644 --- a/Lekcja_08/node_modules/router/lib/layer.js +++ b/Lekcja_08/node_modules/router/lib/layer.js @@ -31,7 +31,7 @@ const MATCHING_GROUP_REGEXP = /\((?:\?<(.*?)>)?(?!\?)/g module.exports = Layer -function Layer (path, options, fn) { +function Layer(path, options, fn) { if (!(this instanceof Layer)) { return new Layer(path, options, fn) } @@ -46,7 +46,7 @@ function Layer (path, options, fn) { this.path = undefined this.slash = path === '/' && opts.end === false - function matcher (_path) { + function matcher(_path) { if (_path instanceof RegExp) { const keys = [] let name = 0 @@ -59,7 +59,7 @@ function Layer (path, options, fn) { }) } - return function regexpMatcher (p) { + return function regexpMatcher(p) { const match = _path.exec(p) if (!match) { return false @@ -103,7 +103,7 @@ function Layer (path, options, fn) { * @api private */ -Layer.prototype.handleError = function handleError (error, req, res, next) { +Layer.prototype.handleError = function handleError(error, req, res, next) { const fn = this.handle if (fn.length !== 4) { @@ -139,7 +139,7 @@ Layer.prototype.handleError = function handleError (error, req, res, next) { * @api private */ -Layer.prototype.handleRequest = function handleRequest (req, res, next) { +Layer.prototype.handleRequest = function handleRequest(req, res, next) { const fn = this.handle if (fn.length > 3) { @@ -175,7 +175,7 @@ Layer.prototype.handleRequest = function handleRequest (req, res, next) { * @api private */ -Layer.prototype.match = function match (path) { +Layer.prototype.match = function match(path) { let match if (path != null) { @@ -216,7 +216,7 @@ Layer.prototype.match = function match (path) { * @private */ -function decodeParam (val) { +function decodeParam(val) { if (typeof val !== 'string' || val.length === 0) { return val } @@ -236,7 +236,7 @@ function decodeParam (val) { /** * Loosens the given path for path-to-regexp matching. */ -function loosen (path) { +function loosen(path) { if (path instanceof RegExp || path === '/') { return path } diff --git a/Lekcja_08/static/css/style.css b/Lekcja_08/static/css/style.css index e6b4860..0e23071 100644 --- a/Lekcja_08/static/css/style.css +++ b/Lekcja_08/static/css/style.css @@ -148,4 +148,21 @@ button { button:hover { background-color: purple; +} + +select { + border: none; + background-color: cornflowerblue; + color: white; + padding: .5rem; + width: 100%; + box-shadow: 1px 1px 2px gray; +} + +input[type='text'] { + border: none; + background-color: lightgray; + padding: .5rem; + width: 100%; + box-shadow: 1px 1px 2px gray; } \ No newline at end of file diff --git a/Lekcja_08/views/advanced_search.hbs b/Lekcja_08/views/advanced_search.hbs new file mode 100644 index 0000000..e69de29 diff --git a/Lekcja_08/views/delete.hbs b/Lekcja_08/views/delete.hbs index e69de29..e3820b4 100644 --- a/Lekcja_08/views/delete.hbs +++ b/Lekcja_08/views/delete.hbs @@ -0,0 +1,40 @@ +

delete cars

+
+
+

+ + + + + + + + + + + + {{#each car}} + + + + + + + + + + + {{/each}} +
_idmodelubezpieczonybenzynauszkodzonynaped4x4
{{_id}}{{model}}{{ubezpieczony}}{{benzyna}}{{uszkodzony}}{{naped4x4}} + + + +
+
+
\ No newline at end of file diff --git a/Lekcja_08/views/edit.hbs b/Lekcja_08/views/edit.hbs index e69de29..2313712 100644 --- a/Lekcja_08/views/edit.hbs +++ b/Lekcja_08/views/edit.hbs @@ -0,0 +1,62 @@ +

edit cars

+
+ + + + + + + + + + + + {{#each car}} + {{#if edycja}} + + + + + + + + + + + + + {{else}} + + + + + + + + + + + {{/if}} + {{/each}} +
_idmodelubezpieczonybenzynauszkodzonynaped4x4
{{_id}}
{{_id}}{{model}}{{ubezpieczony}}{{benzyna}}{{uszkodzony}}{{naped4x4}} + +
+
\ No newline at end of file diff --git a/Lekcja_08/views/layouts/main.hbs b/Lekcja_08/views/layouts/main.hbs index 7658006..dd0c8c4 100644 --- a/Lekcja_08/views/layouts/main.hbs +++ b/Lekcja_08/views/layouts/main.hbs @@ -16,6 +16,7 @@ Delete Edit Search + Advanced search {{{ body }}} diff --git a/Lekcja_08/views/search.hbs b/Lekcja_08/views/search.hbs index e69de29..9164d71 100644 --- a/Lekcja_08/views/search.hbs +++ b/Lekcja_08/views/search.hbs @@ -0,0 +1,35 @@ +

search cars

+
+
+
+
+

Wybierz cechę

+ +
+
+ +
+
+
+
+

Wybierz wartość

+
+
+ +
+
+
+
+
+ + Reset +
+
+
\ No newline at end of file