From 4225644ec0955b94813d67792b635e6d9e2e2892 Mon Sep 17 00:00:00 2001 From: Mohammed Abad Date: Tue, 16 Apr 2024 10:16:16 +0100 Subject: [PATCH] add entry ids & ratings routes --- backend/routes/entryIdsRoutes.js | 27 ++++++++++++++++++++++++ backend/routes/entryRatingsRoutes.js | 31 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 backend/routes/entryIdsRoutes.js create mode 100644 backend/routes/entryRatingsRoutes.js diff --git a/backend/routes/entryIdsRoutes.js b/backend/routes/entryIdsRoutes.js new file mode 100644 index 0000000..25c64a7 --- /dev/null +++ b/backend/routes/entryIdsRoutes.js @@ -0,0 +1,27 @@ +const express = require('express'); +const entryIdsRouter = express.Router(); + +// ! /entry-ids +// * PUT - UPDATE ENTRY RATING BY ID +entryIdsRouter.put('/:id', async (req, res) => { + try { + const { id } = req.params; + const { tmdb_id, imdb_id, tvdb_id } = req.body; + + const entryId = await EntryId.findByPk(id); + if (!entryId) { // ! entry not found + return res.status(404).send('EntryId not found'); + } + + entryId.tmdb_id = tmdb_id ?? entryId.tmdb_id; + entryId.imdb_id = imdb_id ?? entryId.imdb_id; + entryId.tvdb_id = tvdb_id ?? entryId.tvdb_id; + + await entryId.save(); + res.status(200).json(entryId); + } catch (error) { + res.status(500).send('Server error'); + } +}); + +module.exports = entryIdsRouter; diff --git a/backend/routes/entryRatingsRoutes.js b/backend/routes/entryRatingsRoutes.js new file mode 100644 index 0000000..410463e --- /dev/null +++ b/backend/routes/entryRatingsRoutes.js @@ -0,0 +1,31 @@ +const express = require('express'); +const EntrySeen = require('../models/EntrySeen'); +const EntryRating = require('../models/EntryRating'); +const entryRatingsRouter = express.Router(); + +// ! /entry-ratings +// * PUT - UPDATE ENTRY RATING BY ID +entryRatingsRouter.put('/:id', async (req, res) => { + try { + const { id } = req.params; + const { entryR } = req.body; + console.log(entryR) + + const entryRating = await EntryRating.findByPk(id); + if (!entryRating) { + return res.status(404).json({ message: 'EntryRating not found' }); + } + + entryRating.user_rating = entryR.user_rating ?? entryRating.user_rating; + entryRating.tmdb_rating = entryR.tmdb_rating ?? entryRating.tmdb_rating; + entryRating.imdb_rating = entryR.imdb_rating ?? entryRating.imdb_rating; + entryRating.rotten_tomatoes_rating = entryR.rotten_tomatoes_rating ?? entryRating.rotten_tomatoes_rating; + + await entryRating.save(); + res.status(200).json(entryRating); + } catch (error) { + res.status(500).json({ message: 'Server error', error: error.message }); + } +}); + +module.exports = entryRatingsRouter;