diff --git a/backend/routes/entryRoutes.js b/backend/routes/entryRoutes.js index 930cbac..6c34154 100644 --- a/backend/routes/entryRoutes.js +++ b/backend/routes/entryRoutes.js @@ -76,7 +76,7 @@ entryRouter.post('/', async (req, res) => { res.status(201).json(entry); }); -// * POST - CREATE ENTRY +// * PUT - REQUEST ENTRY UPDATE entryRouter.put('/:id', async (req, res) => { const { id } = req.params; diff --git a/backend/routes/routes.js b/backend/routes/routes.js index bd13f3d..2615f9d 100644 --- a/backend/routes/routes.js +++ b/backend/routes/routes.js @@ -1,12 +1,26 @@ const express = require('express'); -const entryRoutes = require('./entryRoutes'); const userCollectionRoutes = require('./userCollectionRoutes'); -const entrySeenRoutes = require('./entrySeenRoutes'); +const entryIdsRouter = require('./entryIdsRoutes'); +const entrySeenRouter = require('./entrySeenRoutes'); +const entryRouter = require('./entryRoutes'); +const entryRatingsRouter = require('./entryRatingsRoutes'); +const killerRouter = require('./killerRoutes'); +const seasonRouter = require('./seasonRoutes'); +const episodeSeenRouter = require('./episodeSeenRoutes'); const router = express.Router(); -router.use('/entries', entryRoutes); +router.use('/entries', entryRouter); router.use('/user-collections', userCollectionRoutes); -router.use('/entry-seen', entrySeenRoutes) +router.use('/entry-seen', entrySeenRouter); +router.use('/entry-ids', entryIdsRouter); +router.use('/entry-ratings', entryRatingsRouter); + +router.use('/seasons', seasonRouter); +router.use('/episode-seen', episodeSeenRouter); + + + +router.use('/killer', killerRouter); // ! this route is used to crash the backend module.exports = router; \ No newline at end of file diff --git a/backend/routes/seasonRoutes.js b/backend/routes/seasonRoutes.js new file mode 100644 index 0000000..f52fd77 --- /dev/null +++ b/backend/routes/seasonRoutes.js @@ -0,0 +1,98 @@ +const express = require('express'); +const Season = require('../models/Season'); +const Episode = require('../models/Episode'); +const seasonRouter = express.Router(); + +// ! /seasons +// POST /seasons - 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 /seasons - 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 /seasons/:id - 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/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 /seasons/:id - 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 /seasons/:id - 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; \ No newline at end of file