From 9da2dbde1d3b3be6c378411c88c79ca4b68f01c9 Mon Sep 17 00:00:00 2001 From: Sagi Dayan Date: Sat, 2 Jul 2016 19:04:03 +0300 Subject: [PATCH] Notes stream - with optional filtering --- package.json | 3 +- server/api.js | 156 ++--------------------------- server/noteRouter.js | 228 +++++++++++++++++++++++++++++++++++++++++++ server/userRouter.js | 47 +++++++++ server/utils.js | 3 +- 5 files changed, 288 insertions(+), 149 deletions(-) create mode 100644 server/noteRouter.js create mode 100644 server/userRouter.js diff --git a/package.json b/package.json index 56e3591..d9a9582 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "dependencies": { "body-parser": "^1.15.2", "express": "^4.14.0", - "gcloud": "^0.36.0" + "gcloud": "^0.36.0", + "geolib": "^2.0.21" } } diff --git a/server/api.js b/server/api.js index 2e5a871..3187f1a 100644 --- a/server/api.js +++ b/server/api.js @@ -2,6 +2,9 @@ var express = require('express'); var router = express.Router(); var utils = require('./utils'); var db = require('./DBManager'); +var noteRouter = require('./noteRouter'); +var userRouter = require('./userRouter'); + router.get('/status', (req, res) => { var d = new Date(); @@ -12,6 +15,9 @@ router.get('/status', (req, res) => { }); }); +router.use('/note', noteRouter); +router.use('/user', userRouter); + router.post('/login', (req, res) => { if (!req.body.username || !req.body.password) { utils.response_400(res, { @@ -64,8 +70,9 @@ router.post('/register', (req, res) => { username: req.body.username, password: req.body.password, email: req.body.email, - creation_time: new Date(), - avatar: 'http://www.aljazeera.com/mritems/images/site/DefaultAvatar.jpg' + creation_time: Date.now(), + avatar: 'http://www.aljazeera.com/mritems/images/site/DefaultAvatar.jpg', + liked_notes_id: [] } }, function (err, user) { if (err) { @@ -83,153 +90,8 @@ router.post('/register', (req, res) => { } }); -router.post('/user/upsert', (req, res) => { - if (!req.body || !req.body.id) { - utils.response_400(res, { - username: "JhonSnow", - password: "TheKingOfTheN0rth_65bc", - email: "jhon@nights_watch.com", - avatar: "path/to/avatar", - id: 123 - }); - } else { - var key = db.key(['User', Number(req.body.id)]); - db.get(key, (err, entity) => { - if (err) { - utils.response_500(res, "DB Error"); - } else { - entity.data.email = req.body.email || entity.data.email; - entity.data.avatar = req.body.avatar || entity.data.avatar; - entity.data.password = req.body.password || entity.data.password; - db.save(entity, (err, _resp) => { - if (err) { - utils.response_500(res, "DB Error"); - } else { - res.send({ - status: "OK", - user: utils.convertToUserObj(entity) - }); - } - }); - } - }); - } -}); -router.post('/note/upsert', (req, res) => { - if (!req.body || !req.body.title || !req.body.body || !req.body.owner_id || !req.body.address) { - utils.response_400(res, { - title: "Note To Self", - body: "I know nothing!!!", - lat: 35.01, - lng: 51.2, - address: "Some Where over the rainbow", - tags: ['nofilter', 'testing', 'education'], - owner_id: 123, - is_public: true - }); - } else { - if (req.body.id) { //Update a Note - var key = db.key(['Note', Number(req.body.id)]); - db.get(key, (err, entity) => { - if (err) { - utils.response_500(res, "DB Error"); - } else { - if (!entity) { - utils.response_400(res, { - title: "Note To Self", - body: "I know nothing!!!", - lat: 35.01, - lng: 51.2, - address: "Some Where over the rainbow", - tags: ['nofilter', 'testing', 'education'], - owner_id: 123, - is_public: true - }, "id not valid - No Notes with that ID"); - } else { - entity.data.title = req.body.title || entity.data.title; - entity.data.body = req.body.body || entity.data.body; - entity.data.lat = req.body.lat || entity.data.lat; - entity.data.lng = req.body.lng || entity.data.lng; - entity.data.address = req.body.address || entity.data.address; - entity.data.tags = req.body.tags || entity.data.tags; - entity.data.is_public = req.body.is_public || entity.data.is_public; - db.save(entity, (err, _resp) => { - if (err) { - utils.response_500(res, "DB Error"); - } else { - res.send({ - status: "OK", - note: utils.convertToNoteObj(entity) - }); - } - }); - } - } - }); - } else { //** Create a new Note **// - db.save({ - key: db.key('Note'), - data: { - title: req.body.title, - lat: req.body.lat, - lng: req.body.lng, - address: req.body.address, - body: req.body.body, - owner_id: req.body.owner_id, - is_public: req.body.is_public || false, - created_at: new Date(), - likes: 0, - tags: req.body.tags || [], - } - }, - (err, response) => { - if (err) { - utils.response_500(res, "DB Error"); - } else { - res.statusCode = 201; - var id = Number(response.mutationResults[0].key.path[0].id); - var key = db.key(['Note', id]); - db.get(key, (err, entity) => { - console.log(JSON.stringify(entity, null, 2)); - if (!err) res.send({ - status: "OK", - note: utils.convertToNoteObj(entity) - }); - else utils.response_500(res, "DB Error->" + err); - }); - } - }); - } - } -}); -router.get('/note/all', (req, res) => { - if (!req.query.uid) { - utils.response_400(res, "uid=123", "Missing uid in query string"); - } else { - var query = db.createQuery('Note') - .filter('owner_id', req.query.uid); - // .order('creation_time', { - // descending: true - // }); - db.runQuery(query, (err, entities) => { - if (err || entities.length < 1) { - res.send({ - status: (err ? "Error" : "OK"), - message: err ? "DB error, " + err : "No Notes", - notes: (err ? null : []) - }); - } else { - entities = entities.map(utils.convertToNoteObj); - res.send({ - status: "OK", - notes: entities - }); - } - }); - } -}); module.exports = router; diff --git a/server/noteRouter.js b/server/noteRouter.js new file mode 100644 index 0000000..b3ef723 --- /dev/null +++ b/server/noteRouter.js @@ -0,0 +1,228 @@ +var express = require('express'); +var router = express.Router(); +var utils = require('./utils'); +var db = require('./DBManager'); +var geolib = require('geolib'); + +router.post('/upsert', (req, res) => { + if (!req.body || !req.body.title || !req.body.body || !req.body.owner_id || !req.body.address) { + utils.response_400(res, { + title: "Note To Self", + body: "I know nothing!!!", + lat: 35.01, + lng: 51.2, + address: "Some Where over the rainbow", + tags: ['nofilter', 'testing', 'education'], + owner_id: 123, + is_public: true + }); + } else { + if (req.body.id) { //Update a Note + var key = db.key(['Note', Number(req.body.id)]); + db.get(key, (err, entity) => { + if (err) { + utils.response_500(res, "DB Error"); + } else { + if (!entity) { + utils.response_400(res, { + title: "Note To Self", + body: "I know nothing!!!", + lat: 35.01, + lng: 51.2, + address: "Some Where over the rainbow", + tags: ['nofilter', 'testing', 'education'], + owner_id: 123, + is_public: true + }, "id not valid - No Notes with that ID"); + } else { + entity.data.title = req.body.title || entity.data.title; + entity.data.body = req.body.body || entity.data.body; + entity.data.lat = req.body.lat || entity.data.lat; + entity.data.lng = req.body.lng || entity.data.lng; + entity.data.address = req.body.address || entity.data.address; + entity.data.tags = req.body.tags || entity.data.tags; + entity.data.is_public = req.body.is_public || false; + db.save(entity, (err, _resp) => { + if (err) { + utils.response_500(res, "DB Error"); + } else { + res.send({ + status: "OK", + note: utils.convertToNoteObj(entity) + }); + } + }); + + } + } + }); + } else { //** Create a new Note **// + db.save({ + key: db.key('Note'), + data: { + title: req.body.title, + lat: req.body.lat, + lng: req.body.lng, + address: req.body.address, + body: req.body.body, + owner_id: req.body.owner_id, + is_public: req.body.is_public || false, + created_at: Date.now(), + likes: 0, + tags: req.body.tags || [], + } + }, + (err, response) => { + if (err) { + utils.response_500(res, "DB Error"); + } else { + res.statusCode = 201; + var id = Number(response.mutationResults[0].key.path[0].id); + var key = db.key(['Note', id]); + db.get(key, (err, entity) => { + if (!err) res.send({ + status: "OK", + note: utils.convertToNoteObj(entity) + }); + else utils.response_500(res, "DB Error->" + err); + }); + } + + }); + } + } +}); +router.get('/all', (req, res) => { + if (!req.query.uid) { + utils.response_400(res, "uid=123", "Missing uid in query string"); + } else { + var query = db.createQuery('Note') + .filter('owner_id', req.query.uid); + // .order('creation_time', { + // descending: true + // }); + db.runQuery(query, (err, entities) => { + if (err || entities.length < 1) { + res.send({ + status: (err ? "Error" : "OK"), + message: err ? "DB error, " + err : "No Notes", + notes: (err ? null : []) + }); + } else { + entities = entities.map(utils.convertToNoteObj); + res.send({ + status: "OK", + notes: entities + }); + } + }); + } +}); + + +router.post('/like', (req, res) => { + if (!req.body.uid || !req.body.nid) { + utils.response_400(res, { + uid: 123, + nid: 555 + }); + } else { + var userKey = db.key(['User', Number(req.body.uid)]); + var noteKey = db.key(['Note', Number(req.body.nid)]); + db.get(userKey, (err, entity) => { + var user; + if (err || !entity) { + utils.response_500(res, "No User OR DB Error"); + } else { + user = utils.convertToUserObj(entity); + db.get(noteKey, (err, _entity) => { + if (err || !_entity) { + utils.response_500(res, "No Note OR DB Error"); + } else { + if (user.liked_notes_id.indexOf(Number(req.body.nid)) < 0) { + entity.data.liked_notes_id.push(Number(req.body.nid)); + _entity.data.likes++; + } + db.save(entity, (err) => {}); + db.save(_entity, (err) => {}); + res.send({ + status: "OK", + user: utils.convertToUserObj(entity), + note: utils.convertToNoteObj(_entity) + }); + } + }); + } + }); + } +}); + +router.post('/stream', (req, res) => { + if (!req.body.id) { + utils.response_400(res, { + id: 123, + filter: { + location: { + distance: 5, + lat: 32.555, + lng: 35.222 + }, + tags: ['bestTag'], + since: 1487362 + } + }, "filter is optional"); + } else { + var query = db.createQuery('Note'); + + db.runQuery(query, (err, entities) => { + if (!entities) { + res.send({ + status: "OK", + notes: [] + }); + return; + } + var notes = entities.map(utils.convertToNoteObj); + notes = notes.filter((n) => { + if (Number(n.owner_id) !== Number(req.body.id)) return true; + return false; + }); + if (req.body.filter) { + if (req.body.filter.location) { + notes = notes.filter((note) => { + if (geolib.getDistance({ + latitude: note.location.lat, + longitude: note.location.lng + }, { + latitude: req.body.filter.location.lat, + longitude: req.body.filter.location.lng + }) <= req.body.filter.location.distance) return true; + return false; + }); + } + if (req.body.filter.tags) { + notes = notes.filter((note) => { + for (var i = 0; i < req.body.filter.tags.length; i++) { + if (note.tags.indexOf(req.body.filter.tags[i]) >= 0) return true; + } + return false; + }); + } + if (req.body.filter.since) { + notes = notes.filter((note) => { + if (note.created_at > req.body.filter.since) return true; + return false; + }); + } + } + res.send({ + status: "OK", + notes: notes + }); + }); + } +}); + + + +module.exports = router; diff --git a/server/userRouter.js b/server/userRouter.js new file mode 100644 index 0000000..679196a --- /dev/null +++ b/server/userRouter.js @@ -0,0 +1,47 @@ +var express = require('express'); +var router = express.Router(); +var utils = require('./utils'); +var db = require('./DBManager'); + +router.post('/upsert', (req, res) => { + if (!req.body || !req.body.id) { + utils.response_400(res, { + password: "TheKingOfTheN0rth_65bc", + email: "jhon@nights_watch.com", + avatar: "path/to/avatar", + id: 123 + }); + } else { + var key = db.key(['User', Number(req.body.id)]); + db.get(key, (err, entity) => { + if (err) { + utils.response_500(res, "DB Error"); + } else { + entity.data.email = req.body.email || entity.data.email; + entity.data.avatar = req.body.avatar || entity.data.avatar; + entity.data.password = req.body.password || entity.data.password; + db.save(entity, (err, _resp) => { + if (err) { + utils.response_500(res, "DB Error"); + } else { + if (!entity) { + utils.response_400(res, { + password: "TheKingOfTheN0rth_65bc", + email: "jhon@nights_watch.com", + avatar: "path/to/avatar", + id: 123 + }, "id not valid - No User with that ID"); + } else { + res.send({ + status: "OK", + user: utils.convertToUserObj(entity) + }); + } + } + }); + } + }); + } +}); + +module.exports = router; diff --git a/server/utils.js b/server/utils.js index 54b4bfd..9c40f52 100644 --- a/server/utils.js +++ b/server/utils.js @@ -25,7 +25,8 @@ module.exports = { password: user.data.password, email: user.data.email, creation_time: user.data.creation_time, - avatar: user.data.avatar + avatar: user.data.avatar, + liked_notes_id: user.data.liked_notes_id }; }, convertToNoteObj: (note) => {