2020-05-07 12:14:13 +00:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
2020-05-05 09:54:02 +00:00
|
|
|
|
2020-05-10 22:09:16 +00:00
|
|
|
const { authenticate } = require('../../middlewares/authenticate');
|
2020-06-02 14:51:42 +00:00
|
|
|
const Availability = require('../models/availabilityModel');
|
2020-05-05 09:54:02 +00:00
|
|
|
|
2020-05-10 22:03:48 +00:00
|
|
|
router.post('/', authenticate, async (req, res) => {
|
2020-05-05 09:54:02 +00:00
|
|
|
const data = { ...req.body };
|
|
|
|
|
|
|
|
try {
|
2020-06-02 14:51:42 +00:00
|
|
|
const [availability] = await Availability.addAvailability(data);
|
|
|
|
res.status(201).json(availability);
|
2020-05-05 09:54:02 +00:00
|
|
|
} catch (error) {
|
|
|
|
res.status(500).json({
|
2020-06-02 14:51:42 +00:00
|
|
|
message: 'Failed to add a new availability.',
|
2020-05-05 09:54:02 +00:00
|
|
|
error,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-10 22:03:48 +00:00
|
|
|
router.delete('/:id', authenticate, async (req, res) => {
|
2020-09-01 16:57:33 +00:00
|
|
|
const { id } = req.params;
|
2020-05-05 09:54:02 +00:00
|
|
|
|
|
|
|
try {
|
2020-06-02 14:51:42 +00:00
|
|
|
const availability = await Availability.deleteAvailability(id);
|
2020-05-05 09:54:02 +00:00
|
|
|
res.status(200).json({
|
2020-06-02 14:51:42 +00:00
|
|
|
message: `Availability with id ${id} successfully deleted.`,
|
2020-05-05 09:54:02 +00:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
res.status(500).json({
|
2020-06-02 14:51:42 +00:00
|
|
|
message: `Failed to delete availability with id ${id}.`,
|
2020-05-05 09:54:02 +00:00
|
|
|
error,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|