fixed some routes

This commit is contained in:
X4Lo committed 2024-04-03 16:27:05 +00:00
1 parent 343a49daad
commit ffb9ce8eef
1 file changed
+34 -20
+34 -20
View File
@@ -1,6 +1,7 @@
const express = require('express');
const List = require('../models/List');
const ListEntry = require('../models/ListEntry');
const Entry = require('../models/Entry');
const router = express.Router();
@@ -18,10 +19,16 @@ router.post('/', async (req, res) => {
// * GET - ALL LISTS
router.get('/', async (req, res) => {
let lists; // Declare outside to broaden scope, albeit unnecessary in your given context
try {
const lists = await List.findAll();
lists = await List.findAll({
include: [
{ model: ListEntry, as: 'listEntries' },
]
});
res.status(200).json(lists);
} catch (error) {
console.error("Error fetching lists:", error); // More detailed error logging
res.status(500).json({ error: error.message });
}
});
@@ -74,23 +81,31 @@ router.delete('/:id', async (req, res) => {
// * POST - ADD ENTRY TO A LIST
router.post('/:listId/entries/:entryId', async (req, res) => {
router.post('/:listId/entries/:entryId', async (req, res) => {
try {
const { listId, entryId } = req.params;
const { entryOrder } = req.body; // Get entryOrder from the request body or query parameters
// Create a new record in the ListEntry table with listId, entryId, and entryOrder
const newListEntry = await ListEntry.create({
listId,
entryId,
entryOrder
});
res.status(201).json(newListEntry);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
try {
const { listId, entryId } = req.params;
const { order } = req.body; // Get entryOrder from the request body or query parameters
// Create a new record in the ListEntry table with listId, entryId, and entryOrder
const newListEntry = await ListEntry.create({
list_id: listId,
entry_id: entryId,
order: order
});
// ? set a default poster if none exists
await List.findByPk(listId).then(list => {
if (list.poster_path == null || list.poster_path.toString().trim() == '') {
Entry.findByPk(entryId).then(entry => {
list.poster_path = entry.poster_path;
list.save();
})
}
})
res.status(201).json(newListEntry);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
// * PATCH - UPDATE ENTRY ORDER IN LIST
@@ -121,10 +136,9 @@ router.patch('/:listId/entries/:entryId/order', async (req, res) => {
});
// * DELETE - AND ENTRY FROM A LIST
router.delete('/lists/:listId/entries/:entryId', async (req, res) => {
router.delete('/:listId/entries/:entryId', async (req, res) => {
try {
const { listId, entryId } = req.params;
const deleted = await ListEntry.destroy({
where: {
list_id: listId,