fixed some routes
This commit is contained in:
1 parent
343a49daad
commit
ffb9ce8eef
1 file changed
+32
-18
@@ -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
|
||||
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({
|
||||
listId,
|
||||
entryId,
|
||||
entryOrder
|
||||
});
|
||||
// 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
|
||||
});
|
||||
|
||||
res.status(201).json(newListEntry);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
// ? 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,
|
||||
|
||||
Reference in new issue
Block a user