Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: added create user and search user feature #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
});
1 change: 0 additions & 1 deletion models/user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
id: String,
username: String,
password: String,
first_name: String,
Expand Down
14 changes: 14 additions & 0 deletions serviceflag.json.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"name": "Authentication",
"enabled": true
},
{
"name": "UserManagement",
"enabled": true
},
{
"name": "FacilityManagement",
"enabled": true
}
]
14 changes: 8 additions & 6 deletions services/UserManagement/routes/v1/index.js
Original file line number Diff line number Diff line change
@@ -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;
55 changes: 45 additions & 10 deletions services/UserManagement/routes/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};