From 6368760e8c12e801f6cffd26704bf27ffc93fa2b Mon Sep 17 00:00:00 2001 From: Phillip Arman Rios Date: Sun, 2 Aug 2020 02:30:33 +0800 Subject: [PATCH] added create user and search user feature --- index.js | 30 ++++++------ models/user.js | 1 - serviceflag.json.tpl | 14 ++++++ services/UserManagement/routes/v1/index.js | 14 +++--- services/UserManagement/routes/v1/users.js | 55 ++++++++++++++++++---- 5 files changed, 82 insertions(+), 32 deletions(-) create mode 100644 serviceflag.json.tpl diff --git a/index.js b/index.js index 7db63f6..b89a1fe 100644 --- a/index.js +++ b/index.js @@ -4,32 +4,32 @@ require('make-promises-safe'); // installs an 'unhandledRejection' handler // Initialize App const fastify = require('fastify')({ - http2: process.env.NODE_ENV === 'production', - logger: true, + http2: process.env.NODE_ENV === 'production', + logger: true, }); // Register vendor modules fastify.register(require('fastify-cors')); fastify.register(require('fastify-compress')); fastify.register(require('fastify-mongodb'), { - // force to close the mongodb connection when app stopped - // the default value is false - forceClose: true, - useUnifiedTopology: true, - url: `mongodb://${process.env.MONGO_DB_HOST}:${process.env.MONGO_DB_PORT}/`, + // force to close the mongodb connection when app stopped + // the default value is false + forceClose: true, + useUnifiedTopology: true, + url: `mongodb://${process.env.MONGO_DB_HOST}:${process.env.MONGO_DB_PORT}/${process.env.MONGO_DB_NAME}`, }); fastify.register(require('fastify-rate-limit'), { - max: 100, - timeWindow: '1 minute', + max: 100, + timeWindow: '1 minute', }); // Register app modules fastify.register(require('./services')); fastify.listen(process.env.API_PORT, (err, address) => { - if (err) { - fastify.log.error(err); - throw new Error('An error has occured.'); - } - fastify.log.info(`Server listening on ${address}`); -}); + if (err) { + fastify.log.error(err); + throw new Error('An error has occured.'); + } + fastify.log.info(`Server listening on ${address}`); +}); \ No newline at end of file diff --git a/models/user.js b/models/user.js index 6588d57..b49694f 100644 --- a/models/user.js +++ b/models/user.js @@ -1,7 +1,6 @@ const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({ - id: String, username: String, password: String, first_name: String, diff --git a/serviceflag.json.tpl b/serviceflag.json.tpl new file mode 100644 index 0000000..38e81da --- /dev/null +++ b/serviceflag.json.tpl @@ -0,0 +1,14 @@ +[ + { + "name": "Authentication", + "enabled": true + }, + { + "name": "UserManagement", + "enabled": true + }, + { + "name": "FacilityManagement", + "enabled": true + } +] diff --git a/services/UserManagement/routes/v1/index.js b/services/UserManagement/routes/v1/index.js index 7c87dc1..c271903 100644 --- a/services/UserManagement/routes/v1/index.js +++ b/services/UserManagement/routes/v1/index.js @@ -1,12 +1,14 @@ /** * V1 REST API Routes -*/ -const { SearchUser } = require('./users'); + */ +const { createUser, userProfile } = require('./users'); async function routes(fastify) { - // TODO: This is a dummy content for DEMO purpose only. Please remove if not needed. - fastify.get('/', async () => ({ message: 'UserManagement API' })); - fastify.get('/user/:id', SearchUser); + // TODO: This is a dummy content for DEMO purpose only. Please remove if not needed. + fastify.get('/', async() => ({ message: 'UserManagement API' })); + fastify.post('/', createUser); + fastify.get('/profile/:username', userProfile); + // fastify.get('/users', userList); } -module.exports = routes; +module.exports = routes; \ No newline at end of file diff --git a/services/UserManagement/routes/v1/users.js b/services/UserManagement/routes/v1/users.js index 421011d..e25fd68 100644 --- a/services/UserManagement/routes/v1/users.js +++ b/services/UserManagement/routes/v1/users.js @@ -6,16 +6,51 @@ * * @param {Object} */ -async function SearchUser(request) { - const { db } = this.mongo; - const result = await db.collection.findOne({ id: request.params.id }); - - if (result.value === null) { - throw new Error('Invalid value'); - } - return result.value; +const User = require('../../../../models/user'); + +async function userProfile(req, res) { + const { db } = this.mongo; + + try { + const profile = await db.collection('User').findOne({ username: req.params.username }); + res.send(profile); + } catch (err) { + console.log(err); + } +} + +// async function userList(req, res) { +// const { db } = this.mongo; + +// try { +// const users = await db.collection('User').find(); +// res.send(users); +// } catch (err) { +// res.send(err); +// } +// } + +async function createUser(req, res) { + const { db } = this.mongo; + + const post = new User({ + username: req.body.username, + password: req.body.password, + first_name: req.body.first_name, + last_name: req.body.last_name, + assigned_area: req.body.assigned_area + }); + + try { + const savedPost = await db.collection('User').save(post); + res.send(savedPost); + } catch (err) { + res.send({ message: err }); + } } module.exports = { - SearchUser, -}; + createUser, + userProfile, + // userList +}; \ No newline at end of file