Works. fixed GAE errors...

This commit is contained in:
Sagi Dayan 2016-07-02 14:43:00 +03:00
parent bd072b675e
commit f26625b469
6 changed files with 58 additions and 29 deletions

View file

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

View file

@ -1,4 +1,3 @@
'use strict';
var serverModule = require('./server/server');
var server = serverModule.getInstance();

View file

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

View file

@ -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");
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({
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",
message: "Saved"
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));
// }
// });
}
});

View file

@ -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({

View file

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