diff --git a/package.json b/package.json index 417f952..127a58e 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,8 @@ "author": "", "license": "ISC", "dependencies": { - "express": "^4.14.0" + "body-parser": "^1.15.2", + "express": "^4.14.0", + "gcloud": "^0.36.0" } } diff --git a/server/DBManager.js b/server/DBManager.js new file mode 100644 index 0000000..d973543 --- /dev/null +++ b/server/DBManager.js @@ -0,0 +1,9 @@ +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' +}); + +module.exports = dataset; diff --git a/server/api.js b/server/api.js index da0d8a2..89c2492 100644 --- a/server/api.js +++ b/server/api.js @@ -1,9 +1,9 @@ -'use strict'; var express = require('express'); var router = express.Router(); +var utils = require('./utils'); +var db = require('./DBManager'); -// home page route (http://localhost:8080) -router.get('/status', function (req, res) { +router.get('/status', (req, res) => { var d = new Date(); res.send({ Status: "OK", @@ -12,6 +12,163 @@ router.get('/status', function (req, res) { }); }); +router.post('/login', (req, res) => { + if (!req.body.username || !req.body.password) { + utils.response_400(res, { + username: "JhonSnow", + password: "I-know_nothing" + }); + } else { + var query = db.createQuery('User') + .filter('username', req.body.username) + .filter('password', req.body.password); + + db.runQuery(query, (err, entities) => { + if (err || entities.length !== 1) { + res.send({ + status: "OK", + user: null + }); + } else { + entities = entities.map(utils.convertToUserObj); + res.send({ + status: "OK", + user: entities[0] + }); + } + }); + } +}); + +router.post('/register', (req, res) => { + if (!req.body.username || !req.body.password || !req.body.email) { + utils.response_400(res, { + username: "JhonSnow", + password: "I-know_nothing", + email: "jhon@nights_watch.com" + }); + } else { + var query = db.createQuery('User') + .filter('username', req.body.username); + + db.runQuery(query, (err, entities) => { + if (err || entities.length === 1) { + res.send({ + status: "Error", + message: "User name exists" + }); + } else { + db.save({ + key: db.key('User'), + data: { + username: req.body.username, + password: req.body.password, + email: req.body.email, + creation_time: new Date(), + avatar: null + } + }, function (err, user) { + if (err) { + utils.response_500(res, "DB Error"); + } else { + res.statusCode = 201; + res.send({ + status: "OK", + message: "created" + }); + } + }); + } + }); + } +}); + +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) { + 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 query = db.createQuery('Note').filter('owner_id', ) + utils.response_500(res, "Not Yet Implemented - Imean updating the Note"); + } else { + db.save({ + key: db.key('Note'), + data: { + title: req.body.title, + lat: req.body.lat, + lng: req.body.lng, + 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; + res.send({ + status: "OK", + message: "Saved" + }); + // 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)); + // } + // }); + } + + }); + } + } +}); +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/server.js b/server/server.js index 7024b55..b66e6b8 100644 --- a/server/server.js +++ b/server/server.js @@ -1,6 +1,6 @@ -'use strict'; var server; var express = require('express'); +var bodyParser = require('body-parser'); var api = require('./api'); @@ -9,11 +9,18 @@ class Server { constructor() { console.log('Pin'); this.app = express(); + // parse application/x-www-form-urlencoded + this.app.use(bodyParser.urlencoded({ + extended: false + })); + + // parse application/json + this.app.use(bodyParser.json()); this.app.use('/api', api); - // this.app.get('/', (req, res) => { - // res.status(200) - // .send('Hello, world - PIN PIN PIN!'); - // }); + this.app.get('/', (req, res) => { + res.status(200) + .send('