const express = require('express'); const Season = require('../models/Season'); const Episode = require('../models/Episode'); const seasonRouter = express.Router(); // ! /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); } }); // * 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); } }); // * 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); } }); // * 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); } }); // * 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); } }); // * 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); } }); module.exports = seasonRouter;