renamed backend routes files + add fields to entry model

This commit is contained in:
X4Lo committed 2024-05-18 15:37:02 +01:00
1 parent 21aaead02a
commit d6c3a974a1
13 files changed
+24 -18

No files matched your search

+275
View File
@@ -0,0 +1,275 @@
const express = require('express');
const { fetchEntryData } = require('../services/tmdbService');
const Entry = require('../models/Entry');
const EntryId = require('../models/EntryId');
const EntryRating = require('../models/EntryRating');
const Genre = require('../models/Genre');
const EntrySeen = require('../models/EntrySeen');
const { saveEntry, refrechEntry } = require('../services/entryService');
const Season = require('../models/Season');
const { bulkImportMovies, bulkImportTmdb, bulkImportEpisodes } = require('../services/importerService');
const Media = require('../models/Media');
const entryRouter = express.Router();
// ! /entries
// * GET - ALL ENTRIES
entryRouter.get('/', async (req, res) => {
const { page, pageSize } = req.body;
const entries = await Entry.findAll({
include: [
{ model: EntryId, as: 'entryId' },
{ model: EntryRating, as: 'entryRating' },
{ model: Media, as: 'medias' },
{
model: Genre,
as: 'genres',
attributes: ['name'], // Only fetch the genre ID
through: {
attributes: [] // Do not fetch any attributes from the join table
}
}
]
});
// converting genres from object list to string list
const entriesWithGenreNames = entries.map(entry => ({
...entry.get({ plain: true }),
genres: entry.genres.map(genre => genre.name) // Convert genre objects to string array
}));
res.status(200).json(entriesWithGenreNames);
});
// * GET ENTRY BY ID
entryRouter.get('/:id', async (req, res) => {
try {
const { id } = req.params;
if (id == null) { // ! entry id not provided
res.status(404).json({ message: 'Entry id not provided' });
}
let entry = await Entry.findByPk(id, {
include: [
{ model: EntryId, as: 'entryId' },
{ model: EntryRating, as: 'entryRating' },
{ model: EntrySeen, as: 'entrySeen' },
{ model: Season, as: 'seasons' },
{ model: Media, as: 'medias' },
{
model: Genre,
as: 'genres',
attributes: ['name'], // Only fetch the genre ID
through: {
attributes: [] // Do not fetch any attributes from the join table
}
}
]
});
if (!entry) { // ! entry not found
res.status(404).json({ message: 'Entry not found' });
}
const entryData = entry.get({ plain: true });
const genres = entryData.genres.map(genre => genre.name);
entryData.genres = genres;
res.status(200).json(entryData);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
});
// ! POST
// * POST - CREATE ENTRY
entryRouter.post('/', async (req, res) => {
const { tmdb_id, is_tvserie } = req.body;
const data = await fetchEntryData(tmdb_id, is_tvserie);
const entry = await saveEntry(data);
res.status(201).json(entry);
});
// ! import routes
entryRouter.post('/bulk-import-movies', async (req, res) => {
const { movies } = req.body;
let entries = [];
try {
entries = await bulkImportMovies(movies);
} catch (error) {
res.status(500);
}
res.status(201).json(entries);
});
entryRouter.post('/bulk-import-tmdb', async (req, res) => {
const { values } = req.body;
let entries = [];
try {
entries = await bulkImportTmdb(values);
} catch (error) {
res.status(500);
}
res.status(201).json(entries);
});
entryRouter.post('/bulk-import-episodes-seen', async (req, res) => {
const { values } = req.body;
let createdRows = 0;
try {
createdRows = await bulkImportEpisodes(values);
console.log(`Episodes Seen - counts ${episodes.length}`)
} catch (error) {
res.status(500);
}
res.status(201).json(createdRows);
});
// + import routes end
// * PUT - ENTRY UPDATE
entryRouter.put('/:id', async (req, res) => {
const { id } = req.params;
const { favorit, hidden, watchlist } = req.body;
// ? 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
});
// ? returning a new entry with updated values
let includes = [
{ model: EntryId, as: 'entryId' },
{ model: EntryRating, as: 'entryRating' },
{ model: EntrySeen, as: 'entrySeen' },
{ model: Media, as: 'medias' },
{
model: Genre,
as: 'genres',
attributes: ['name'],
through: {
attributes: []
}
}
];
if (oldEntry.is_tvseries) includes.push({ model: Season, as: 'seasons' });
const updatedEntry = await Entry.findByPk(id, {
include: includes
});
const entryData = updatedEntry.get({ plain: true });
const genres = entryData.genres.map(genre => genre.name);
entryData.genres = genres;
res.status(201).json(entryData);
});
// * GET - REQUEST ENTRY REFRECH
entryRouter.get('/:id/refrech', async (req, res, next) => {
const { id } = req.params;
try {
const entry = await refrechEntry(id);
res.status(201).json(entry);
} catch (error) {
next(error)
}
// // ? checking if the provied id is valid
// // ! note: the includes are required for the update
// const oldEntry = await Entry.findByPk(id, {
// include: [
// { model: EntryId, as: 'entryId' },
// { model: EntryRating, as: 'entryRating' },
// { model: EntrySeen, as: 'entrySeen' },
// { model: Season, as: 'seasons' },
// { model: Media, as: 'medias' },
// {
// model: Genre,
// as: 'genres',
// attributes: ['id'],
// through: {
// attributes: []
// }
// }
// ]
// });
// 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
// await updateEntry(oldEntry, data);
// // ? returning a new entry with updated values
// let includes = [
// { model: EntryId, as: 'entryId' },
// { model: EntryRating, as: 'entryRating' },
// { model: EntrySeen, as: 'entrySeen' },
// { model: Season, as: 'seasons' },
// { model: Media, as: 'medias' },
// {
// model: Genre,
// as: 'genres',
// attributes: ['name'],
// through: {
// attributes: []
// }
// }
// ];
// if (oldEntry.is_tvseries) includes.push({ model: Season, as: 'seasons' });
// const updatedEntry = await Entry.findByPk(id, {
// include: includes
// });
// const entryData = updatedEntry.get({ plain: true });
// entryData.genres = entryData.genres.map(genre => genre.name);
// res.status(201).json(entryData);
});
// * DELETE - ENTRY BY ID
entryRouter.delete('/:id', async (req, res) => {
const { id } = req.params;
try {
const numDeleted = await Entry.destroy({
where: { id: id }
});
if (numDeleted) {
res.status(200).json({ message: `Entry with ID ${id} deleted.` });
} else {
res.status(404).json({ message: `Entry with ID ${id} not found.` });
}
} catch (error) {
res.status(500).json({ message: `Error deleting entry with ID ${id}.`, error: error.message });
}
});
module.exports = entryRouter;