modified the entry routes
This commit is contained in:
1 parent
4225644ec0
commit
16cf031369
1 file changed
+109
-6
@@ -3,11 +3,15 @@ const { fetchEntryData } = require('../services/tmdbService');
|
||||
const Entry = require('../models/Entry');
|
||||
const EntryId = require('../models/EntryId');
|
||||
const EntryRating = require('../models/EntryRating');
|
||||
const router = express.Router();
|
||||
const Genre = require('../models/Genre');
|
||||
const EntrySeen = require('../models/EntrySeen');
|
||||
const { saveEntry, updateEntry } = require('../services/entryService');
|
||||
const Season = require('../models/Season');
|
||||
const entryRouter = express.Router();
|
||||
|
||||
// ! /entries
|
||||
// * GET - ALL ENTRIES
|
||||
router.get('/', async (req, res) => {
|
||||
entryRouter.get('/', async (req, res) => {
|
||||
const { page, pageSize } = req.body;
|
||||
|
||||
const entries = await Entry.findAll({
|
||||
@@ -20,17 +24,116 @@ router.get('/', async (req, res) => {
|
||||
res.status(200).json(entries);
|
||||
});
|
||||
|
||||
// * 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: 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 - CREATE ENTRY
|
||||
router.post('/', async (req, res) => {
|
||||
entryRouter.post('/', async (req, res) => {
|
||||
const { tmdb_id, is_tvserie } = req.body;
|
||||
|
||||
const entry = await fetchEntryData(tmdb_id, is_tvserie);
|
||||
const data = await fetchEntryData(tmdb_id, is_tvserie);
|
||||
const entry = await saveEntry(data);
|
||||
|
||||
res.status(201).json(entry);
|
||||
});
|
||||
|
||||
// * POST - CREATE ENTRY
|
||||
entryRouter.put('/:id', async (req, res) => {
|
||||
const { id } = req.params;
|
||||
|
||||
// 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: []
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
if (!oldEntry) {
|
||||
// TODO 404 error
|
||||
return;
|
||||
}
|
||||
|
||||
// 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);
|
||||
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);
|
||||
});
|
||||
|
||||
// * DELETE - ENTRY BY ID
|
||||
router.delete('/:id', async (req, res) => {
|
||||
entryRouter.delete('/:id', async (req, res) => {
|
||||
const { id } = req.params;
|
||||
|
||||
try {
|
||||
@@ -48,4 +151,4 @@ router.delete('/:id', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
module.exports = entryRouter;
|
||||
Reference in new issue
Block a user