var express = require('express'); var router = express.Router(); var utils = require('./utils'); var db = require('./DBManager'); var noteRouter = require('./noteRouter'); var userRouter = require('./userRouter'); router.get('/status', (req, res) => { var d = new Date(); res.send({ Status: "OK", Mantra: "KISS - Yo!", Time: d.toString() }); }); 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, 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" }); } }); } }); } }); module.exports = router; // // about page route (http://localhost:8080/about) // router.get('/about', function(req, res) { // res.send('im the about page!'); // });