add favorit button in entry page

This commit is contained in:
X4Lo committed 2024-04-28 00:26:07 +01:00
1 parent 62056bdc98
commit 299f566b96
6 files changed
+130 -45

No files matched your search

+53 -22
View File
@@ -76,39 +76,70 @@ entryRouter.post('/', async (req, res) => {
res.status(201).json(entry);
});
// * PUT - REQUEST ENTRY UPDATE
// * PUT - ENTRY UPDATE
entryRouter.put('/:id', async (req, res) => {
const { id } = req.params;
const { favorit, hidden, watchlist } = req.body;
// getting the old entry
const oldEntry = await Entry.findByPk(id, {
include: [
{ model: EntryId, as: 'entryId' },
{ model: EntryRating, as: 'entryRating' },
{ model: EntrySeen, as: 'entrySeen' },
{
model: Genre,
as: 'genres',
attributes: ['id'],
through: {
attributes: []
}
}
]
// ? checking if the provied id is valid
const oldEntry = await Entry.findByPk(id);
if (!oldEntry)
return res.status(404).json({ message: `Entry with the provided id was not found` });
// ? updating
await oldEntry.update({
favorit: favorit,
watchlist: watchlist,
hidden: hidden
});
if (!oldEntry) {
// TODO 404 error
return;
}
// getting new data from TMDB
// ? returning a new entry with updated values
let includes = [
{ model: EntryId, as: 'entryId' },
{ model: EntryRating, as: 'entryRating' },
{ model: EntrySeen, as: 'entrySeen' },
{
model: Genre,
as: 'genres',
attributes: ['id'],
through: {
attributes: []
}
}
];
if (oldEntry.is_tvseries) includes.push({ model: Season, as: 'seasons' });
const entry = await Entry.findByPk(id, {
include: includes
});
res.status(201).json(entry);
});
// * GET - REQUEST ENTRY UPDATE
entryRouter.get('/:id/refrech', async (req, res) => {
const { id } = req.params;
// ? checking if the provied id is valid
const oldEntry = await Entry.findByPk(id);
if (!oldEntry)
return res.status(404).json({ message: `Entry id not found` });
// ? getting new data from TMDB
const data = await fetchEntryData(oldEntry.entryId.tmdb_id, oldEntry.is_tvseries);
if (data == null) return res.status(500).json({ message: `Counldn't fetch data from TMDB` });
// updating
// ? updating
await updateEntry(oldEntry, data);
// ? returning a new entry with updated values
let includes = [
{ model: EntryId, as: 'entryId' },
{ model: EntryRating, as: 'entryRating' },
+4 -1
View File
@@ -9,7 +9,7 @@ const Episode = require('../models/Episode.js');
async function saveEntry(data) {
if (data == null) return;
// creating the object
var is_tvseries = data.release_date == null ? true : false;
@@ -24,6 +24,7 @@ async function saveEntry(data) {
country: data.production_countries.length > 0 ? data.production_countries[0].name : null,
is_tvseries: is_tvseries,
watchlist: true,
status: data.status,
genres: data.genre_ids,
@@ -99,6 +100,8 @@ async function saveEntry(data) {
episodes.push(savedEpisode);
}
// TODO fetch cast
savedSeason.setDataValue('episodes', episodes);
seasons.push(savedSeason)
}