diff --git a/package.json b/package.json index 127a58e..56e3591 100644 --- a/package.json +++ b/package.json @@ -2,11 +2,11 @@ "name": "the_social_notework", "version": "0.0.1", "description": "A sample Back ent for an Android app", - "main": "app.js", + "main": "server.js", "private": true, "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "start": "node app.js" + "start": "node server.js" }, "author": "", "license": "ISC", diff --git a/app.js b/server.js similarity index 88% rename from app.js rename to server.js index 41f7173..a9a091a 100644 --- a/app.js +++ b/server.js @@ -1,4 +1,3 @@ -'use strict'; var serverModule = require('./server/server'); var server = serverModule.getInstance(); diff --git a/server/DBManager.js b/server/DBManager.js index d973543..9a9b758 100644 --- a/server/DBManager.js +++ b/server/DBManager.js @@ -3,7 +3,7 @@ var gcloud = require('gcloud'); var dataset = gcloud.datastore({ // This environment variable is set by app.yaml when running on GAE, but will // need to be manually set when running locally. - projectId: 'thesocialnotework' + projectId: 'thesocialnotework-api' }); module.exports = dataset; diff --git a/server/api.js b/server/api.js index 89c2492..e06af54 100644 --- a/server/api.js +++ b/server/api.js @@ -65,7 +65,7 @@ router.post('/register', (req, res) => { password: req.body.password, email: req.body.email, creation_time: new Date(), - avatar: null + avatar: 'http://www.aljazeera.com/mritems/images/site/DefaultAvatar.jpg' } }, function (err, user) { if (err) { @@ -85,7 +85,7 @@ router.post('/register', (req, res) => { router.post('/user/upsert', (req, res) => {}); router.post('/note/upsert', (req, res) => { - if (!req.body || !req.body.title || !req.body.body || !req.body.tags || !req.body.owner_id || !req.body.address) { + 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!!!", @@ -98,21 +98,55 @@ router.post('/note/upsert', (req, res) => { }); } else { if (req.body.id) { //Update a Note - // var query = db.createQuery('Note').filter('owner_id', ) - utils.response_500(res, "Not Yet Implemented - Imean updating the Note"); - } else { + 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(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 || [] + tags: req.body.tags || [], } }, (err, response) => { @@ -120,22 +154,16 @@ router.post('/note/upsert', (req, res) => { utils.response_500(res, "DB Error"); } else { res.statusCode = 201; - res.send({ - status: "OK", - message: "Saved" + 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); }); - // var query = db.createQuery('Note') - // .filter('_id', response.mutationResults[0].key.path[0].id); - // db.get(db.key(['Note', response.mutationResults[0].key.path[0].id]), (err, entity) => { - // if (err) { - // utils.response_500(res, "DB Error" + err); - // } else { - // res.statusCode = 201; - // console.log(JSON.stringify(entity, null, 2)); - // console.log('====='); - // res.send(utils.convertToNoteObj(entity)); - // } - // }); } }); diff --git a/server/server.js b/server/server.js index b66e6b8..d1d3208 100644 --- a/server/server.js +++ b/server/server.js @@ -1,13 +1,13 @@ +'use strict'; var server; var express = require('express'); var bodyParser = require('body-parser'); var api = require('./api'); - class Server { constructor() { - console.log('Pin'); + console.log('Creating Server'); this.app = express(); // parse application/x-www-form-urlencoded this.app.use(bodyParser.urlencoded({ diff --git a/server/utils.js b/server/utils.js index 0ae5230..54b4bfd 100644 --- a/server/utils.js +++ b/server/utils.js @@ -34,14 +34,16 @@ module.exports = { title: note.data.title, location: { lat: note.data.lat, - lng: note.data.lng + lng: note.data.lng, + address: note.data.address }, body: note.data.body, owner_id: note.data.owner_id, is_public: note.data.is_public, created_at: note.data.created_at, likes: note.data.likes, - tags: note.data.tags, + tags: note.data.tags }; } + };