Skip to content

Commit

Permalink
feat: added sign up api with already existing avatar and without avat…
Browse files Browse the repository at this point in the history
…ar (by creating a new one)
  • Loading branch information
varijkapil13 committed Jan 16, 2019
1 parent 852b03f commit b76539d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 22 deletions.
94 changes: 72 additions & 22 deletions server/controllers/user.js
Original file line number Diff line number Diff line change
@@ -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;
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;
5 changes: 5 additions & 0 deletions server/routes/users.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit b76539d

Please sign in to comment.