2016-07-02 16:04:03 +00:00
|
|
|
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 **//
|
2016-07-03 11:25:47 +00:00
|
|
|
var key = db.key(['User', Number(req.body.owner_id)]);
|
|
|
|
db.get(key, (err, entity) => {
|
|
|
|
if (err) {
|
|
|
|
utils.response_500(res, "DB Error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!entity) {
|
|
|
|
utils.response_400(res, null, "No User with that id!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var user = utils.convertToUserObj(entity);
|
|
|
|
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 || [],
|
|
|
|
avatar: user.avatar
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(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);
|
2016-07-02 16:04:03 +00:00
|
|
|
});
|
2016-07-03 11:25:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2016-07-02 16:04:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-07-03 10:38:29 +00:00
|
|
|
router.post('/getPublic', (req, res) => {
|
2016-07-02 16:04:03 +00:00
|
|
|
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 {
|
2016-07-02 16:08:27 +00:00
|
|
|
var query = db.createQuery('Note')
|
|
|
|
.filter('is_public', true);
|
2016-07-02 16:04:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-07-03 10:38:29 +00:00
|
|
|
router.post('/delete', (req, res) => {
|
|
|
|
if (!req.body.uid || !req.body.nid) {
|
|
|
|
utils.response_400(res, {
|
|
|
|
uid: 123,
|
|
|
|
nid: 123
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var key = db.key(['Note', Number(req.body.nid)]);
|
|
|
|
db.get(key, (err, entity) => {
|
|
|
|
if (!entity || Number(utils.convertToNoteObj(entity)
|
|
|
|
.owner_id) != Number(req.body.uid)) {
|
|
|
|
utils.response_400(res, null, "The Note not exists OR he is not the owner of the note");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
db.delete(key, (err, apiResp) => {
|
|
|
|
if (!err) {
|
|
|
|
res.send({
|
|
|
|
status: "OK",
|
|
|
|
message: "Note Deleted"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
utils.response_500(res, "Error while Deleting Note");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-07-02 16:04:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|