Major Backend SI Update part 2: added controllers
This commit is contained in:
1 parent
a51e207707
commit
dbb00fdb61
38 files changed
+2129
-1231
No files matched your search
@@ -1,99 +1,25 @@
|
||||
const express = require('express');
|
||||
const Season = require('../models/Season');
|
||||
const Episode = require('../models/Episode');
|
||||
const seasonRouter = express.Router();
|
||||
|
||||
const seasonController = require('../controllers/seasonController');
|
||||
|
||||
// ! /seasons
|
||||
// * POST - Create a new season
|
||||
seasonRouter.post('/', async (req, res) => {
|
||||
try {
|
||||
const season = await Season.create(req.body);
|
||||
res.status(201).send(season);
|
||||
} catch (error) {
|
||||
res.status(400).send(error.message);
|
||||
}
|
||||
});
|
||||
seasonRouter.post('/', seasonController.createSeason);
|
||||
|
||||
// * GET - Retrieve all seasons
|
||||
seasonRouter.get('/', async (req, res) => {
|
||||
try {
|
||||
const seasons = await Season.findAll();
|
||||
res.send(seasons);
|
||||
} catch (error) {
|
||||
res.status(500).send(error.message);
|
||||
}
|
||||
});
|
||||
seasonRouter.get('/', seasonController.getSeasons);
|
||||
|
||||
// * GET - Retrieve a specific season by id
|
||||
seasonRouter.get('/:id', async (req, res) => {
|
||||
try {
|
||||
const season = await Season.findByPk(req.params.id, {
|
||||
include: [{
|
||||
model: Episode,
|
||||
as: 'episodes'
|
||||
}]
|
||||
});
|
||||
|
||||
if (season) {
|
||||
res.send(season);
|
||||
} else {
|
||||
res.status(404).send('Season not found');
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).send(error.message);
|
||||
}
|
||||
});
|
||||
seasonRouter.get('/:id', seasonController.getSeaonById);
|
||||
|
||||
// * GET - Retrieve episodes of a season
|
||||
seasonRouter.get('/:id/episodes', async (req, res) => {
|
||||
try {
|
||||
const episodes = await Episode.findAll({
|
||||
where: {
|
||||
season_id: req.params.id
|
||||
}
|
||||
});
|
||||
|
||||
if (episodes) {
|
||||
res.send(episodes);
|
||||
} else {
|
||||
res.status(404).send('Episodes not found');
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).send(error.message);
|
||||
}
|
||||
});
|
||||
seasonRouter.get('/:id/episodes', seasonController.getSeasonEpisodes);
|
||||
|
||||
// * PUT - Update a season
|
||||
seasonRouter.put('/:id', async (req, res) => {
|
||||
try {
|
||||
const [updated] = await Season.update(req.body, {
|
||||
where: { id: req.params.id }
|
||||
});
|
||||
if (updated) {
|
||||
const updatedSeason = await Season.findByPk(req.params.id);
|
||||
res.send(updatedSeason);
|
||||
} else {
|
||||
res.status(404).send('Season not found');
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).send(error.message);
|
||||
}
|
||||
});
|
||||
seasonRouter.put('/:id', seasonController.updateSeason);
|
||||
|
||||
// * DELETE - Delete a season
|
||||
seasonRouter.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
const deleted = await Season.destroy({
|
||||
where: { id: req.params.id }
|
||||
});
|
||||
if (deleted) {
|
||||
res.status(204).send('Season deleted');
|
||||
} else {
|
||||
res.status(404).send('Season not found');
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).send(error.message);
|
||||
}
|
||||
});
|
||||
seasonRouter.delete('/:id', seasonController.deleteSeason);
|
||||
|
||||
module.exports = seasonRouter;
|
||||
Reference in new issue
Block a user