changes to routes + add season route

This commit is contained in:
X4Lo committed 2024-04-20 15:44:49 +01:00
1 parent e1223171b0
commit 7bf23432d6
3 files changed
+117 -5

No files matched your search

+1 -1
View File
@@ -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;
+18 -4
View File
@@ -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;
+98
View File
@@ -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;