Notes stream - with optional filtering

This commit is contained in:
Sagi Dayan 2016-07-02 19:04:03 +03:00
parent d0a9eaf9e2
commit 9da2dbde1d
5 changed files with 288 additions and 149 deletions

View file

@ -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"
}
}

View file

@ -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;

228
server/noteRouter.js Normal file
View file

@ -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;

47
server/userRouter.js Normal file
View file

@ -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;

View file

@ -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) => {