add list router
This commit is contained in:
1 parent
4203624857
commit
6316051f66
1 file changed
+172
@@ -0,0 +1,172 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const List = require('../models/List');
|
||||||
|
const ListEntry = require('../models/ListEntry');
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
|
||||||
|
// ! /lists
|
||||||
|
// * POST - CREATE LIST
|
||||||
|
router.post('/', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { name, description, user_rating, poster_path } = req.body;
|
||||||
|
const newList = await List.create({ name, description, user_rating, poster_path });
|
||||||
|
res.status(201).json(newList);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(400).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// * GET - ALL LISTS
|
||||||
|
router.get('/', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const lists = await List.findAll();
|
||||||
|
res.status(200).json(lists);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// * GET - LIST BY ID
|
||||||
|
router.get('/:id', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const list = await List.findByPk(req.params.id);
|
||||||
|
if (list) {
|
||||||
|
res.status(200).json(list);
|
||||||
|
} else {
|
||||||
|
res.status(404).json({ message: 'List not found' });
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// * PUT - UPDATE A LIST
|
||||||
|
router.put('/:id', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { name, description, user_rating, poster_path } = req.body;
|
||||||
|
const [updated] = await List.update({ name, description, user_rating, poster_path }, { where: { id: req.params.id } });
|
||||||
|
|
||||||
|
if (updated) {
|
||||||
|
const updatedList = await List.findByPk(req.params.id);
|
||||||
|
res.status(200).json(updatedList);
|
||||||
|
} else {
|
||||||
|
res.status(404).json({ message: 'List not found' });
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// * DELETE - A LIST
|
||||||
|
router.delete('/:id', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const deleted = await List.destroy({ where: { id: req.params.id } });
|
||||||
|
if (deleted) {
|
||||||
|
res.status(204).send();
|
||||||
|
} else {
|
||||||
|
res.status(404).json({ message: 'List not found' });
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// * 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 });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// * PATCH - UPDATE ENTRY ORDER IN LIST
|
||||||
|
router.patch('/:listId/entries/:entryId/order', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { listId, entryId } = req.params;
|
||||||
|
const { newOrder } = req.body; // The new order value
|
||||||
|
|
||||||
|
// Assuming you have a model ListEntry for the junction table
|
||||||
|
const updated = await ListEntry.update(
|
||||||
|
{ entryOrder: newOrder }, // Set new order
|
||||||
|
{
|
||||||
|
where: {
|
||||||
|
listId: listId,
|
||||||
|
entryId: entryId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (updated[0] > 0) {
|
||||||
|
res.status(200).json({ message: 'Entry order updated successfully.' });
|
||||||
|
} else {
|
||||||
|
res.status(404).json({ message: 'List entry not found.' });
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// * DELETE - AND ENTRY FROM A LIST
|
||||||
|
router.delete('/lists/:listId/entries/:entryId', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { listId, entryId } = req.params;
|
||||||
|
|
||||||
|
const deleted = await ListEntry.destroy({
|
||||||
|
where: {
|
||||||
|
list_id: listId,
|
||||||
|
entry_id: entryId
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (deleted) {
|
||||||
|
res.status(204).send();
|
||||||
|
} else {
|
||||||
|
res.status(404).json({ message: 'List entry not found.' });
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// * POST - CREATE LIST WITH ENTRY
|
||||||
|
router.post('/lists-with-entry', async (req, res) => {
|
||||||
|
const { listDetails, entryId, entryOrder } = req.body; // Destructure needed properties from the request body
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await sequelize.transaction(async (t) => {
|
||||||
|
// Step 1: Create the list
|
||||||
|
const newList = await List.create(listDetails, { transaction: t });
|
||||||
|
|
||||||
|
// Step 2: Add an entry to the newly created list
|
||||||
|
const newListEntry = await ListEntry.create({
|
||||||
|
listId: newList.id,
|
||||||
|
entryId: entryId,
|
||||||
|
entryOrder: entryOrder
|
||||||
|
}, { transaction: t });
|
||||||
|
|
||||||
|
return { newList, newListEntry };
|
||||||
|
});
|
||||||
|
|
||||||
|
// If the transaction is successful, send back the created list and entry relationship
|
||||||
|
res.status(201).json(result);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to create list and add entry:', error);
|
||||||
|
res.status(500).json({ message: 'Failed to create list and add entry', error: error.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
Reference in new issue
Block a user