-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added sign up api with already existing avatar and without avat…
…ar (by creating a new one)
- Loading branch information
1 parent
852b03f
commit b76539d
Showing
2 changed files
with
77 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}).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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |