the-social-notebook-server/server/api.js

103 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-07-01 12:35:32 +00:00
var express = require('express');
var router = express.Router();
var utils = require('./utils');
var db = require('./DBManager');
2016-07-02 16:04:03 +00:00
var noteRouter = require('./noteRouter');
var userRouter = require('./userRouter');
2016-07-01 12:35:32 +00:00
router.get('/status', (req, res) => {
2016-07-01 12:35:32 +00:00
var d = new Date();
res.send({
Status: "OK",
Mantra: "KISS - Yo!",
Time: d.toString()
});
});
2016-07-02 16:04:03 +00:00
router.use('/note', noteRouter);
router.use('/user', userRouter);
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,
2016-07-02 16:04:03 +00:00
creation_time: Date.now(),
avatar: 'http://www.aljazeera.com/mritems/images/site/DefaultAvatar.jpg',
liked_notes_id: []
}
}, function (err, user) {
if (err) {
utils.response_500(res, "DB Error");
} else {
res.statusCode = 201;
res.send({
status: "OK",
message: "created"
});
}
});
}
});
}
});
2016-07-02 11:43:00 +00:00
2016-07-01 12:35:32 +00:00
module.exports = router;
// // about page route (http://localhost:8080/about)
// router.get('/about', function(req, res) {
// res.send('im the about page!');
// });