rename model list to usercollection & fixed redlated errors
This commit is contained in:
1 parent
60b517500f
commit
b48b395574
37 files changed
+2479
-1210
No files matched your search
@@ -4,7 +4,7 @@ const router = express.Router();
|
||||
|
||||
// ! /entry-seen
|
||||
// * POST - CREATE ENTRY SEEN
|
||||
router.post('/entry-seen', async (req, res) => {
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const { entry_id, date_seen, seen_with } = req.body;
|
||||
const newEntrySeen = await EntrySeen.create({ entry_id, date_seen, seen_with });
|
||||
@@ -15,7 +15,7 @@ router.post('/entry-seen', async (req, res) => {
|
||||
});
|
||||
|
||||
// * GET - ALL ENTRY SEEN
|
||||
router.get('/entry-seen', async (req, res) => {
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const entrySeens = await EntrySeen.findAll();
|
||||
res.status(200).json(entrySeens);
|
||||
@@ -25,7 +25,7 @@ router.get('/entry-seen', async (req, res) => {
|
||||
});
|
||||
|
||||
// * GET - ENTRY SEEN BY ID
|
||||
router.get('/entry-seen/:id', async (req, res) => {
|
||||
router.get('/:id', async (req, res) => {
|
||||
try {
|
||||
const entrySeen = await EntrySeen.findByPk(req.params.id);
|
||||
if (entrySeen) {
|
||||
@@ -39,7 +39,7 @@ router.get('/entry-seen/:id', async (req, res) => {
|
||||
});
|
||||
|
||||
// * GET - ENTRY SEEN BY ENTRY ID
|
||||
router.get('/entry-seen/by-entry/:entryId', async (req, res) => {
|
||||
router.get('/by-entry/:entryId', async (req, res) => {
|
||||
try {
|
||||
const { entryId } = req.params;
|
||||
const entrySeens = await EntrySeen.findAll({
|
||||
@@ -58,7 +58,7 @@ router.get('/entry-seen/by-entry/:entryId', async (req, res) => {
|
||||
});
|
||||
|
||||
// * PUT - UPDATE ENTRY SEEN
|
||||
router.put('/entry-seen/:id', async (req, res) => {
|
||||
router.put('/:id', async (req, res) => {
|
||||
try {
|
||||
const { date_seen, seen_with } = req.body;
|
||||
const updated = await EntrySeen.update({ date_seen, seen_with }, { where: { id: req.params.id } });
|
||||
@@ -75,7 +75,7 @@ router.put('/entry-seen/:id', async (req, res) => {
|
||||
});
|
||||
|
||||
// * DELETE - ENTRY SEEN
|
||||
router.delete('/entry-seen/:id', async (req, res) => {
|
||||
router.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
const deleted = await EntrySeen.destroy({ where: { id: req.params.id } });
|
||||
if (deleted) {
|
||||
|
||||
@@ -1,208 +0,0 @@
|
||||
const express = require('express');
|
||||
const List = require('../models/List');
|
||||
const ListEntry = require('../models/ListEntry');
|
||||
const Entry = require('../models/Entry');
|
||||
const EntryId = require('../models/EntryId');
|
||||
const EntryRating = require('../models/EntryRating');
|
||||
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) => {
|
||||
let lists; // Declare outside to broaden scope, albeit unnecessary in your given context
|
||||
try {
|
||||
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 });
|
||||
}
|
||||
});
|
||||
|
||||
// * GET - LIST BY ID
|
||||
router.get('/:id', async (req, res) => {
|
||||
try {
|
||||
const list = await List.findByPk(req.params.id, {
|
||||
include: [{
|
||||
model: Entry,
|
||||
as: 'entries', // Make sure this matches your alias in the association, if any
|
||||
through: {
|
||||
attributes: [], // Exclude attributes from the join model, if you don't need them
|
||||
},
|
||||
}],
|
||||
});
|
||||
|
||||
if (list) {
|
||||
// Optionally, sort the entries here if needed, using JavaScript's sort()
|
||||
// This is a workaround for the direct ordering limitation via Sequelize in many-to-many relationships
|
||||
for (let i = 0; i < list.entries.length; i++) {
|
||||
list.entries[i] = await Entry.findByPk(list.entries[i].id,
|
||||
{
|
||||
include: [
|
||||
{ model: EntryId, as: 'entryId' },
|
||||
{ model: EntryRating, as: 'entryRating' }
|
||||
]
|
||||
})
|
||||
}
|
||||
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) => {
|
||||
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
|
||||
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('/: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;
|
||||
@@ -1,12 +1,12 @@
|
||||
const express = require('express');
|
||||
const entryRoutes = require('./entryRoutes');
|
||||
const listRoutes = require('./listRoutes');
|
||||
const userCollectionRoutes = require('./userCollectionRoutes');
|
||||
const entrySeenRoutes = require('./entrySeenRoutes');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.use('/entries', entryRoutes);
|
||||
router.use('/lists', listRoutes);
|
||||
router.use('/user-collections', userCollectionRoutes);
|
||||
router.use('/entry-seen', entrySeenRoutes)
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,204 @@
|
||||
const express = require('express');
|
||||
const UserCollection = require('../models/UserCollection');
|
||||
const UserCollectionEntry = require('../models/UserCollectionEntry');
|
||||
const Entry = require('../models/Entry');
|
||||
const EntryId = require('../models/EntryId');
|
||||
const EntryRating = require('../models/EntryRating');
|
||||
const router = express.Router();
|
||||
|
||||
// ! /user-collections
|
||||
// * POST - CREATE UserCollection
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const { name, description, user_rating, poster_path } = req.body;
|
||||
const newUserCollection = await UserCollection.create({ name, description, user_rating, poster_path });
|
||||
res.status(201).json(newUserCollection);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// * GET - ALL UserCollections
|
||||
router.get('/', async (req, res) => {
|
||||
let userCollections; // Declare outside to broaden scope, albeit unnecessary in your given context
|
||||
try {
|
||||
userCollections = await UserCollection.findAll({
|
||||
include: [
|
||||
{ model: UserCollectionEntry, as: 'userCollectionEntries' },
|
||||
]
|
||||
});
|
||||
res.status(200).json(userCollections);
|
||||
} catch (error) {
|
||||
console.error("Error fetching UserCollections:", error); // More detailed error logging
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// * GET - userCollection BY ID
|
||||
router.get('/:id', async (req, res) => {
|
||||
try {
|
||||
const userCollection = await UserCollection.findByPk(req.params.id, {
|
||||
include: [{
|
||||
model: Entry,
|
||||
as: 'entries', // Make sure this matches your alias in the association, if any
|
||||
through: {
|
||||
attributes: [], // Exclude attributes from the join model, if you don't need them
|
||||
},
|
||||
}],
|
||||
});
|
||||
|
||||
if (userCollection) {
|
||||
// Optionally, sort the entries here if needed, using JavaScript's sort()
|
||||
// This is a workaround for the direct ordering limitation via Sequelize in many-to-many relationships
|
||||
for (let i = 0; i < userCollection.entries.length; i++) {
|
||||
userCollection.entries[i] = await Entry.findByPk(userCollection.entries[i].id,
|
||||
{
|
||||
include: [
|
||||
{ model: EntryId, as: 'entryId' },
|
||||
{ model: EntryRating, as: 'entryRating' }
|
||||
]
|
||||
})
|
||||
}
|
||||
res.status(200).json(userCollection);
|
||||
} else {
|
||||
res.status(404).json({ message: 'UserCollection not found' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// * PUT - UPDATE A UserCollection
|
||||
router.put('/:id', async (req, res) => {
|
||||
try {
|
||||
const { name, description, user_rating, poster_path } = req.body;
|
||||
const [updated] = await UserCollection.update({ name, description, user_rating, poster_path }, { where: { id: req.params.id } });
|
||||
|
||||
if (updated) {
|
||||
const updatedUserCollection = await UserCollection.findByPk(req.params.id);
|
||||
res.status(200).json(updatedUserCollection);
|
||||
} else {
|
||||
res.status(404).json({ message: 'UserCollection not found' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// * DELETE - A UserCollection
|
||||
router.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
const deleted = await UserCollection.destroy({ where: { id: req.params.id } });
|
||||
if (deleted) {
|
||||
res.status(204).send();
|
||||
} else {
|
||||
res.status(404).json({ message: 'UserCollection not found' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// * POST - ADD ENTRY TO A UserCollection
|
||||
router.post('/:userCollectionId/entries/:entryId', async (req, res) => {
|
||||
try {
|
||||
const { userCollectionId, entryId } = req.params;
|
||||
const { order } = req.body;
|
||||
|
||||
const newUserCollectionEntry = await UserCollectionEntry.create({
|
||||
user_collection_id: userCollectionId,
|
||||
entry_id: entryId,
|
||||
order: order || 0
|
||||
});
|
||||
|
||||
// ? set a default poster if none exists
|
||||
const userCollection = await UserCollection.findByPk(userCollectionId);
|
||||
if (!userCollection.poster_path || userCollection.poster_path.trim() === '') {
|
||||
const entry = await Entry.findByPk(entryId);
|
||||
userCollection.poster_path = entry.poster_path;
|
||||
await userCollection.save();
|
||||
}
|
||||
|
||||
res.status(201).json(newUserCollectionEntry);
|
||||
} catch (error) {
|
||||
res.status(400).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// * PATCH - UPDATE ENTRY ORDER IN UserCollection
|
||||
router.patch('/:userCollectionId/entries/:entryId/order', async (req, res) => {
|
||||
try {
|
||||
const { userCollectionId, entryId } = req.params;
|
||||
const { newOrder } = req.body; // The new order value
|
||||
|
||||
// Assuming you have a model UserCollectionEntry for the junction table
|
||||
const updated = await UserCollectionEntry.update(
|
||||
{ entryOrder: newOrder }, // Set new order
|
||||
{
|
||||
where: {
|
||||
userCollectionId: userCollectionId,
|
||||
entryId: entryId
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (updated[0] > 0) {
|
||||
res.status(200).json({ message: 'Entry order updated successfully.' });
|
||||
} else {
|
||||
res.status(404).json({ message: 'UserCollection entry not found.' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// * DELETE - AND ENTRY FROM A UserCollection
|
||||
router.delete('/:userCollectionId/entries/:entryId', async (req, res) => {
|
||||
try {
|
||||
const { userCollectionId, entryId } = req.params;
|
||||
const deleted = await UserCollectionEntry.destroy({
|
||||
where: {
|
||||
user_collection_id: userCollectionId,
|
||||
entry_id: entryId
|
||||
}
|
||||
});
|
||||
|
||||
if (deleted) {
|
||||
res.status(204).send();
|
||||
} else {
|
||||
res.status(404).json({ message: 'UserCollection entry not found.' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// * POST - CREATE UserCollection 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 userCollection
|
||||
const newUserCollection = await UserCollection.create(listDetails, { transaction: t });
|
||||
|
||||
// Step 2: Add an entry to the newly created userCollection
|
||||
const newUserCollectionEntry = await UserCollectionEntry.create({
|
||||
userCollectionId: newUserCollection.id,
|
||||
entryId: entryId,
|
||||
entryOrder: entryOrder
|
||||
}, { transaction: t });
|
||||
|
||||
return { newUserCollection, newUserCollectionEntry };
|
||||
});
|
||||
|
||||
// If the transaction is successful, send back the created userCollection and entry relationship
|
||||
res.status(201).json(result);
|
||||
} catch (error) {
|
||||
console.error('Failed to create userCollection and add entry:', error);
|
||||
res.status(500).json({ message: 'Failed to create userCollection and add entry', error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in new issue
Block a user