From ffb9ce8eefe704ebca91420d5034ca4bd687ecc1 Mon Sep 17 00:00:00 2001 From: X4Lo Date: Wed, 3 Apr 2024 16:27:05 +0000 Subject: [PATCH] fixed some routes --- backend/routes/listRoutes.js | 54 +++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/backend/routes/listRoutes.js b/backend/routes/listRoutes.js index 20759b5..9550d5c 100644 --- a/backend/routes/listRoutes.js +++ b/backend/routes/listRoutes.js @@ -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,