From ac65f4372cdb73f58d2b5a276cdd2c75e68f2f38 Mon Sep 17 00:00:00 2001 From: Sagi Dayan Date: Sat, 28 Apr 2018 17:52:36 +0300 Subject: [PATCH] Added get user by id route --- Server/API/API.js | 6 ++++-- Server/API/Routers/UserRouter.js | 37 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 Server/API/Routers/UserRouter.js diff --git a/Server/API/API.js b/Server/API/API.js index 22d1bd0..a5ef665 100644 --- a/Server/API/API.js +++ b/Server/API/API.js @@ -6,6 +6,7 @@ const bodyParser = require('body-parser'); const UpdateRouter = require('./Routers/UpdateRouter'); const AccountRouter = require('./Routers/AccountRouter'); const FrameRouter = require('./Routers/FrameRouter'); +const UserRouter = require('./Routers/UserRouter'); // parse application/x-www-form-urlencoded @@ -20,6 +21,7 @@ router.use(bodyParser.json()) // } router.use('/update', UpdateRouter); -router.use('/account', AccountRouter) -router.use('/frame', FrameRouter) +router.use('/account', AccountRouter); +router.use('/frame', FrameRouter); +router.use('/user', UserRouter); module.exports = router; diff --git a/Server/API/Routers/UserRouter.js b/Server/API/Routers/UserRouter.js new file mode 100644 index 0000000..b6bf044 --- /dev/null +++ b/Server/API/Routers/UserRouter.js @@ -0,0 +1,37 @@ +const express = require("express"); +const DBUtils = require('../../Utils/DBUtil'); +const AuthUtil = require('../../Utils/AuthUtil'); + +const router = express.Router(); + +router.get('/:accountId', (req, res) => { + const token = req.get('token'); + const accountId = req.params.accountId; + if(!accountId){ + res.status(400).json({ + message: 'Invalid account id' + }); + return; + } + AuthUtil.getAccountByToken(token) + .then(account => { + AuthUtil.getUserByAccountId(accountId) + .then(user => { + res.json(user); + }) + .catch(reason => { + res.status(400).json({ + message: reason + }); + }) + }) + .catch(reason => { + res.status(401).json({ + message: reason + }); + }) +}); + + + +module.exports = router;