API create-Note/Register/Login/get-note
This commit is contained in:
parent
15ed3f11b4
commit
bd072b675e
5 changed files with 231 additions and 9 deletions
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
9
server/DBManager.js
Normal file
9
server/DBManager.js
Normal file
|
@ -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;
|
163
server/api.js
163
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;
|
||||
|
||||
|
|
|
@ -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('<h1>The Social Notework<h1><h4>Under Development</h4>');
|
||||
});
|
||||
|
||||
this.app.use((req, res, next) => {
|
||||
res.statusCode = 404;
|
||||
|
|
47
server/utils.js
Normal file
47
server/utils.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
module.exports = {
|
||||
response_400: (res, example, msg) => {
|
||||
res.statusCode = 400;
|
||||
var r = {
|
||||
example_body: example,
|
||||
status: 'Error',
|
||||
code: 400,
|
||||
message: 'Bad request, ' + msg || 'see the example for more information'
|
||||
};
|
||||
res.send(r);
|
||||
},
|
||||
response_500: (res, msg) => {
|
||||
res.statusCode = 500;
|
||||
var r = {
|
||||
status: 'Error',
|
||||
code: 500,
|
||||
message: msg
|
||||
};
|
||||
res.send(r);
|
||||
},
|
||||
convertToUserObj: (user) => {
|
||||
return {
|
||||
id: user.key.id,
|
||||
username: user.data.username,
|
||||
password: user.data.password,
|
||||
email: user.data.email,
|
||||
creation_time: user.data.creation_time,
|
||||
avatar: user.data.avatar
|
||||
};
|
||||
},
|
||||
convertToNoteObj: (note) => {
|
||||
return {
|
||||
id: note.key.id,
|
||||
title: note.data.title,
|
||||
location: {
|
||||
lat: note.data.lat,
|
||||
lng: note.data.lng
|
||||
},
|
||||
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,
|
||||
};
|
||||
}
|
||||
};
|
Loading…
Reference in a new issue