From 7ff35d2da34b8a9bf046510be65d48cd1b82a08b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCller?= Date: Sat, 17 Oct 2015 22:39:35 +0200 Subject: [PATCH] note creation implemented --- server.js | 2 +- src/storage.js | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index f570187..60509fd 100644 --- a/server.js +++ b/server.js @@ -30,7 +30,7 @@ app.post('/note', function (req, res) { var expectedSignature = md5(session + note.replace(/[\n\r]/g, "")); if (expectedSignature != body.signature) return sendResponse(res, 400, "Signature mismatch"); - sendResponse(res, 200, JSON.stringify(body)); + storage.addNote(note, body.password).then(note => res.redirect("/" + note.id)); }); app.get("/:year/:month/:day/:title", function (req, res) { diff --git a/src/storage.js b/src/storage.js index 4395336..5a208d5 100644 --- a/src/storage.js +++ b/src/storage.js @@ -16,7 +16,7 @@ var Note = sequelize.define('Note', { published: { type: Sequelize.DATE, defaultValue: Sequelize.NOW }, edited: { type: Sequelize.DATE, allowNull: true, defaultValue: null }, password: Sequelize.STRING(16), - views: Sequelize.INTEGER, + views: { type: Sequelize.INTEGER, defaultValue: 0 } }); module.exports.getNote = id => { @@ -30,3 +30,21 @@ module.exports.getNoteId = deprecatedId => { where: { deprecatedId: deprecatedId } }).then(note => note.id); } + +var generateId = () => [1, 1, 1, 1, 1] + .map(() => { + var code = Math.floor(Math.random() * 36); + return String.fromCharCode(code + (code < 10 ? 48 : 87)); + }) + .join(""); + +var getFreeId = () => { + var id = generateId(); + return Note.findById(id).then(result => result ? getFreeId() : id); +}; + +module.exports.addNote = (note, password) => getFreeId().then(id => Note.create({ + id: id, + text: note, + password: password +})); \ No newline at end of file