Browse Source

update note implemented

master
Christian Müller 10 years ago
parent
commit
8786543ec2
  1. 5
      resources/edit.html
  2. 16
      server.js
  3. 5
      src/page.js
  4. 10
      src/storage.js

5
resources/edit.html

@ -16,8 +16,9 @@ @@ -16,8 +16,9 @@
<div class="hidden" id="dashed-line"></div>
<div class="central-element helvetica" style="margin-bottom: 3em">
<form action="/note" autocomplete="off" method="POST">
<input id="action" value="%METHOD%" type="hidden">
<input id="password" name="password" type="hidden">
<input id="action" name="action" value="%ACTION%" type="hidden" />
<input id="id" name="id" value="%ID%" type="hidden" />
<input id="password" name="password" type="hidden" />
<input id="session" name="session" type="hidden" value="%SESSION%" />
<input id="signature" name="signature" type="hidden" />
<textarea id="note" name="note">%CONTENT%</textarea>

16
server.js

@ -24,13 +24,25 @@ app.get('/new', function (req, res) { @@ -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) {

5
src/page.js

@ -11,11 +11,12 @@ var buildPage = (id, title, content) => pageTemplate @@ -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);

10
src/storage.js

@ -47,4 +47,12 @@ module.exports.addNote = (note, password) => getFreeId().then(id => Note.create( @@ -47,4 +47,12 @@ module.exports.addNote = (note, password) => getFreeId().then(id => Note.create(
id: id,
text: note,
password: password
}));
}));
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();
});
Loading…
Cancel
Save