Skip to content

Commit

Permalink
fix: bug fixes with error catch, and api fixes for post requests
Browse files Browse the repository at this point in the history
  • Loading branch information
varijkapil13 committed Jan 17, 2019
1 parent 0bc7036 commit d9dbaf2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
22 changes: 11 additions & 11 deletions server/controllers/holiday.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import model from '../models';
import moment from 'moment';

const {Holiday} = model;

Expand All @@ -7,7 +8,7 @@ class HolidayController {
const {date, notes, from, to} = req.body;

return Holiday.create({
date,
date: moment.utc(date),
notes,
from,
to
Expand All @@ -25,17 +26,17 @@ class HolidayController {
static deleteHoliday(req, res) {
const holidayId = req.params.holidayId;

Holiday.findById(holidayId).then(holiday => {
Holiday.findByPk(holidayId).then(holiday => {
if (!holiday) {
return res.status(400).send({
message: 'Avatar Not Found'
message: 'Holiday Not Found'
});
} else {
holiday
.destroy()
.then(() =>
res.status(200).send({
message: 'Avatar successfully deleted'
message: 'Holiday successfully deleted'
})
)
.catch(error => res.status(400).send(error));
Expand All @@ -44,16 +45,15 @@ class HolidayController {
}

static getAllHolidays(req, res) {
Holiday.findAll().then(holidays => {
return res
.status(200)
.send({
Holiday.findAll()
.then(holidays => {
return res.status(200).send({
status: true,
message: 'Found holidays',
holidays
})
.catch(error => res.status(400).send(error));
});
});
})
.catch(error => res.status(400).send(error));
}
}

Expand Down
49 changes: 24 additions & 25 deletions server/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,31 @@ 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
Avatar.findByPk(avatarId)
.then(avatar => {
if (avatar) {
return User.create({
email,
password,
avatarId,
roles
})
);
} else {
return res.status(400).send({
status: false,
errors: [
{
name: 'No Avatar found',
details: 'Avatar with id ' + avatarId + ' was not found in the database'
}
]
});
}
});
.then(userData =>
res.status(201).send({
success: true,
message: 'User successfully created',
userData
})
)
.catch(error => res.status(400).send(error));
} else {
return res.status(400).send({
status: false,
message: 'Avatar with id ' + avatarId + ' was not found in the database'
});
}
})
.catch(error => res.status(400).send(error));
}

static signUp(req, res) {
Expand Down
2 changes: 1 addition & 1 deletion server/routes/holiday.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import HolidayController from '../controllers/holiday';
const routes = express.Router();

routes.post('/', HolidayController.addHoliday);
routes.delete('/', HolidayController.deleteHoliday);
routes.delete('/:holidayId', HolidayController.deleteHoliday);
routes.get('/', HolidayController.getAllHolidays);

export default routes;

0 comments on commit d9dbaf2

Please sign in to comment.