From b76539d70acc27eefe2d0744a846fa23b882bb5b Mon Sep 17 00:00:00 2001 From: Varij Kapil Date: Wed, 16 Jan 2019 19:58:48 +0530 Subject: [PATCH] feat: added sign up api with already existing avatar and without avatar (by creating a new one) --- server/controllers/user.js | 94 +++++++++++++++++++++++++++++--------- server/routes/users.js | 5 ++ 2 files changed, 77 insertions(+), 22 deletions(-) diff --git a/server/controllers/user.js b/server/controllers/user.js index 1c041456..8a29fe31 100644 --- a/server/controllers/user.js +++ b/server/controllers/user.js @@ -1,22 +1,72 @@ -// import model from '../models'; -// -// const { User } = model; -// -// class Users { -// static signUp(req, res) { -// const { email, password, roles, avatarId } = req.body; -// return User -// .create({ -// email, -// password, -// roles -// }) -// .then(userData => res.status(201).send({ -// success: true, -// message: 'User successfully created', -// userData -// })) -// } -// } -// -// export default Users; \ No newline at end of file +import model from '../models'; + +const {User} = model; +const {Avatar} = model; + +class Users { + static signUpWithAvatar(req, res) { + const {email, password, roles} = req.body; + const avatarId = req.params.avatarId; + Avatar.findById(avatarId).then(avatar => { + if (avatar) { + return User.create({ + email, + password, + avatarId, + roles + }).then(userData => + res.status(201).send({ + success: true, + message: 'User successfully created', + userData + }) + ); + } else { + return res.status(400).send({ + status: false, + errors: [ + { + name: 'No Avatar found', + details: 'Avatar with id ' + avatarId + ' was not found in the database' + } + ] + }); + } + }); + } + static signUp(req, res) { + const {email, password, roles, first_name, last_name} = req.body; + Avatar.create({ + first_name, + last_name, + email + }).then(avatar => { + if (avatar) { + return User.create({ + email, + password, + avatarId: avatar.id, + roles + }).then(userData => + res.status(201).send({ + success: true, + message: 'User successfully created', + userData + }) + ); + } else { + return res.status(400).send({ + status: false, + errors: [ + { + name: 'No Avatar found', + details: 'Avatar with id ' + avatarId + ' was not found in the database' + } + ] + }); + } + }); + } +} + +export default Users; diff --git a/server/routes/users.js b/server/routes/users.js index 44f19ed2..bafe571a 100644 --- a/server/routes/users.js +++ b/server/routes/users.js @@ -1,5 +1,10 @@ import express from 'express'; +import User from '../controllers/user'; const routes = express.Router(); +// sign ip with avatar id +routes.post('/signup/:avatarId', User.signUpWithAvatar); +routes.post('/signup', User.signUp); + export default routes;