Browse Source

note creation implemented

master
Christian Müller 10 years ago
parent
commit
7ff35d2da3
  1. 2
      server.js
  2. 20
      src/storage.js

2
server.js

@ -30,7 +30,7 @@ app.post('/note', function (req, res) {
var expectedSignature = md5(session + note.replace(/[\n\r]/g, "")); var expectedSignature = md5(session + note.replace(/[\n\r]/g, ""));
if (expectedSignature != body.signature) if (expectedSignature != body.signature)
return sendResponse(res, 400, "Signature mismatch"); 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) { app.get("/:year/:month/:day/:title", function (req, res) {

20
src/storage.js

@ -16,7 +16,7 @@ var Note = sequelize.define('Note', {
published: { type: Sequelize.DATE, defaultValue: Sequelize.NOW }, published: { type: Sequelize.DATE, defaultValue: Sequelize.NOW },
edited: { type: Sequelize.DATE, allowNull: true, defaultValue: null }, edited: { type: Sequelize.DATE, allowNull: true, defaultValue: null },
password: Sequelize.STRING(16), password: Sequelize.STRING(16),
views: Sequelize.INTEGER, views: { type: Sequelize.INTEGER, defaultValue: 0 }
}); });
module.exports.getNote = id => { module.exports.getNote = id => {
@ -30,3 +30,21 @@ module.exports.getNoteId = deprecatedId => {
where: { deprecatedId: deprecatedId } where: { deprecatedId: deprecatedId }
}).then(note => note.id); }).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
}));
Loading…
Cancel
Save