-
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 holiday addition, deletion and get api
- Loading branch information
1 parent
d3293d6
commit 0bc7036
Showing
3 changed files
with
66 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import model from '../models'; | ||
|
||
const {Holiday} = model; | ||
|
||
class HolidayController { | ||
static addHoliday(req, res) { | ||
const {date, notes, from, to} = req.body; | ||
|
||
return Holiday.create({ | ||
date, | ||
notes, | ||
from, | ||
to | ||
}) | ||
.then(holidayData => | ||
res.status(201).send({ | ||
success: true, | ||
message: 'Holiday was successfully added', | ||
holidayData | ||
}) | ||
) | ||
.catch(error => res.status(400).send(error)); | ||
} | ||
|
||
static deleteHoliday(req, res) { | ||
const holidayId = req.params.holidayId; | ||
|
||
Holiday.findById(holidayId).then(holiday => { | ||
if (!holiday) { | ||
return res.status(400).send({ | ||
message: 'Avatar Not Found' | ||
}); | ||
} else { | ||
holiday | ||
.destroy() | ||
.then(() => | ||
res.status(200).send({ | ||
message: 'Avatar successfully deleted' | ||
}) | ||
) | ||
.catch(error => res.status(400).send(error)); | ||
} | ||
}); | ||
} | ||
|
||
static getAllHolidays(req, res) { | ||
Holiday.findAll().then(holidays => { | ||
return res | ||
.status(200) | ||
.send({ | ||
status: true, | ||
message: 'Found holidays', | ||
holidays | ||
}) | ||
.catch(error => res.status(400).send(error)); | ||
}); | ||
} | ||
} | ||
|
||
export default HolidayController; |
Empty file.
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 HolidayController from '../controllers/holiday'; | ||
|
||
const routes = express.Router(); | ||
|
||
export default routes; | ||
routes.post('/', HolidayController.addHoliday); | ||
routes.delete('/', HolidayController.deleteHoliday); | ||
routes.get('/', HolidayController.getAllHolidays); | ||
|
||
export default routes; |