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 express = require('express');
|
||||||
const List = require('../models/List');
|
const List = require('../models/List');
|
||||||
const ListEntry = require('../models/ListEntry');
|
const ListEntry = require('../models/ListEntry');
|
||||||
|
const Entry = require('../models/Entry');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
|
|
||||||
@@ -18,10 +19,16 @@ router.post('/', async (req, res) => {
|
|||||||
|
|
||||||
// * GET - ALL LISTS
|
// * GET - ALL LISTS
|
||||||
router.get('/', async (req, res) => {
|
router.get('/', async (req, res) => {
|
||||||
|
let lists; // Declare outside to broaden scope, albeit unnecessary in your given context
|
||||||
try {
|
try {
|
||||||
const lists = await List.findAll();
|
lists = await List.findAll({
|
||||||
|
include: [
|
||||||
|
{ model: ListEntry, as: 'listEntries' },
|
||||||
|
]
|
||||||
|
});
|
||||||
res.status(200).json(lists);
|
res.status(200).json(lists);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error("Error fetching lists:", error); // More detailed error logging
|
||||||
res.status(500).json({ error: error.message });
|
res.status(500).json({ error: error.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -74,23 +81,31 @@ router.delete('/:id', async (req, res) => {
|
|||||||
|
|
||||||
// * POST - ADD ENTRY TO A LIST
|
// * POST - ADD ENTRY TO A LIST
|
||||||
router.post('/:listId/entries/:entryId', async (req, res) => {
|
router.post('/:listId/entries/:entryId', async (req, res) => {
|
||||||
router.post('/:listId/entries/:entryId', async (req, res) => {
|
try {
|
||||||
try {
|
const { listId, entryId } = req.params;
|
||||||
const { listId, entryId } = req.params;
|
const { order } = req.body; // Get entryOrder from the request body or query parameters
|
||||||
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
|
// Create a new record in the ListEntry table with listId, entryId, and entryOrder
|
||||||
const newListEntry = await ListEntry.create({
|
const newListEntry = await ListEntry.create({
|
||||||
listId,
|
list_id: listId,
|
||||||
entryId,
|
entry_id: entryId,
|
||||||
entryOrder
|
order: order
|
||||||
});
|
});
|
||||||
|
|
||||||
res.status(201).json(newListEntry);
|
// ? set a default poster if none exists
|
||||||
} catch (error) {
|
await List.findByPk(listId).then(list => {
|
||||||
res.status(400).json({ error: error.message });
|
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
|
// * 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
|
// * DELETE - AND ENTRY FROM A LIST
|
||||||
router.delete('/lists/:listId/entries/:entryId', async (req, res) => {
|
router.delete('/:listId/entries/:entryId', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { listId, entryId } = req.params;
|
const { listId, entryId } = req.params;
|
||||||
|
|
||||||
const deleted = await ListEntry.destroy({
|
const deleted = await ListEntry.destroy({
|
||||||
where: {
|
where: {
|
||||||
list_id: listId,
|
list_id: listId,
|
||||||
|
|||||||
Reference in new issue
Block a user