From 8786543ec2b43073517d772cd0d27a8eb6e668c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCller?= Date: Sun, 18 Oct 2015 09:42:27 +0200 Subject: [PATCH] update note implemented --- resources/edit.html | 5 +++-- server.js | 16 ++++++++++++++-- src/page.js | 5 +++-- src/storage.js | 10 +++++++++- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/resources/edit.html b/resources/edit.html index ef0b928..37a049d 100644 --- a/resources/edit.html +++ b/resources/edit.html @@ -16,8 +16,9 @@
- - + + + diff --git a/server.js b/server.js index 4e6c0bb..cb2466e 100644 --- a/server.js +++ b/server.js @@ -24,13 +24,25 @@ app.get('/new', function (req, res) { }); app.post('/note', function (req, res) { - var body = req.body, session = body.session, note = body.note; + var body = req.body, + session = body.session, + note = body.note, + password = md5(body.password); + var goToNote = note => res.redirect("/" + note.id); if (session.indexOf(getTimeStamp()) != 0) return res.status(400).send("Session expired"); var expectedSignature = md5(session + note.replace(/[\n\r]/g, "")); if (expectedSignature != body.signature) return res.status(400).send("Signature mismatch"); - storage.addNote(note, body.password).then(note => res.redirect("/" + note.id)); + console.log(body) + if (body.action == "POST") + storage.addNote(note, password).then(goToNote); + else + storage.updateNote(body.id, password, note).then(note => { + CACHE.del(note.id); + goToNote(note); + }, + error => res.status(403).send(error.message)) }); app.get("/:year/:month/:day/:title", function (req, res) { diff --git a/src/page.js b/src/page.js index 286acb1..bd6df51 100644 --- a/src/page.js +++ b/src/page.js @@ -11,11 +11,12 @@ var buildPage = (id, title, content) => pageTemplate module.exports.buildNote = note => buildPage(note.id, note.title, marked(note.text)); module.exports.newNotePage = session => editTemplate - .replace("%METHOD%", "POST") + .replace("%ACTION%", "POST") .replace("%SESSION%", session) .replace("%CONTENT%", "Loading..."); module.exports.editNotePage = (session, note) => editTemplate - .replace("%METHOD%", "UPDATE") + .replace("%ACTION%", "UPDATE") .replace("%SESSION%", session) + .replace("%ID%", note.id) .replace("%CONTENT%", note.text); \ No newline at end of file diff --git a/src/storage.js b/src/storage.js index 5a208d5..6be8dc0 100644 --- a/src/storage.js +++ b/src/storage.js @@ -47,4 +47,12 @@ module.exports.addNote = (note, password) => getFreeId().then(id => Note.create( id: id, text: note, password: password -})); \ No newline at end of file +})); + +module.exports.updateNote = (id, password, text) => Note.findById(id).then(note => { + if (!note || note.password !== password) return new Promise((resolve, reject) => { + reject({ message: "Password is wrong" }); + }); + note.text = text; + return note.save(); +}); \ No newline at end of file