var express = require('express'); var router = express.Router(); var utils = require('./utils'); var db = require('./DBManager'); router.post('/upsert', (req, res) => { if (!req.body || !req.body.id) { utils.response_400(res, { password: "TheKingOfTheN0rth_65bc", email: "jhon@nights_watch.com", avatar: "path/to/avatar", id: 123 }); } else { var key = db.key(['User', Number(req.body.id)]); db.get(key, (err, entity) => { if (err) { utils.response_500(res, "DB Error"); } else { entity.data.email = req.body.email || entity.data.email; entity.data.avatar = req.body.avatar || entity.data.avatar; entity.data.password = req.body.password || entity.data.password; db.save(entity, (err, _resp) => { if (err) { utils.response_500(res, "DB Error"); } else { if (!entity) { utils.response_400(res, { password: "TheKingOfTheN0rth_65bc", email: "jhon@nights_watch.com", avatar: "path/to/avatar", id: 123 }, "id not valid - No User with that ID"); } else { res.send({ status: "OK", user: utils.convertToUserObj(entity) }); } } }); } }); } }); module.exports = router;