Files
Showy/backend/routes/entryRoutes.js
T
2024-04-01 21:15:48 +00:00

52 lines
1.3 KiB
JavaScript

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 router = express.Router();
// ! /entries
// * GET - ALL ENTRIES
router.get('/', async (req, res) => {
const { page, pageSize } = req.body;
const entries = await Entry.findAll({
include: [
{ model: EntryId, as: 'entryId' },
{ model: EntryRating, as: 'entryRating' }
]
});
res.status(200).json(entries);
});
// * POST - CREATE ENTRY
router.post('/', async (req, res) => {
const { tmdb_id, is_tvserie } = req.body;
const entry = await fetchEntryData(tmdb_id, is_tvserie);
res.status(201).json(entry);
});
// * DELETE - ENTRY BY ID
router.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 = router;