implemented UserCollection edit modal + custom collection poster + media access route

This commit is contained in:
X4Lo committed 2024-05-01 23:44:33 +01:00
1 parent 0ff3c49ca6
commit e8c4fba943
17 files changed
+499 -73

No files matched your search

+55 -4
View File
@@ -6,6 +6,8 @@ const EntryId = require('../models/EntryId');
const EntryRating = require('../models/EntryRating');
const { Sequelize } = require('sequelize');
const router = express.Router();
const multer = require('multer');
const { v4: uuidv4 } = require('uuid');
// ! /user-collections
// * POST - CREATE UserCollection
@@ -65,19 +67,68 @@ router.get('/:id', async (req, res) => {
}
});
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/collections/posters/')
},
filename: function (req, file, cb) {
const uniqueFilename = uuidv4();
const extension = file.originalname.split('.').pop();
cb(null, `${uniqueFilename}.${extension}`);
}
});
const upload = multer({ storage: storage });
// * PUT - UPDATE A UserCollection
router.put('/:id', async (req, res) => {
router.put('/:id', upload.single('poster'), 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 } });
const userCollectionId = req.params.id;
const { name, description, user_rating, is_poster_link } = req.body;
let poster_path = req.file ? req.file.filename : undefined;
const updateData = {
name,
description,
user_rating: parseInt(user_rating, 10)
};
if (poster_path) {
updateData.poster_path = poster_path;
}
const updated = await UserCollection.update(updateData, {
where: { id: userCollectionId }
});
if (!updated[0]) {
return res.status(404).json({ message: 'UserCollection not found' });
}
if (updated) {
const updatedUserCollection = await UserCollection.findByPk(req.params.id);
const updatedUserCollection = await UserCollection.findByPk(req.params.id, {
include: [{
model: Entry,
as: 'entries',
include: [
{ model: EntryId, as: 'entryId' },
{ model: EntryRating, as: 'entryRating' }
],
through: {
model: UserCollectionEntry,
attributes: ['order'], // attributes (to include) from the join model
},
}],
order: [[Sequelize.literal("`entries->UserCollectionEntry`.`order`"), 'ASC']], // sort by 'order' in ascending order
});
res.status(200).json(updatedUserCollection);
} else {
res.status(404).json({ message: 'UserCollection not found' });
}
} catch (error) {
console.log(`UserCollection update (put) error: `, error)
res.status(500).json({ error: error.message });
}
});
+24 -1
View File
@@ -3,6 +3,7 @@ const express = require('express');
const bodyParser = require('body-parser');
const routes = require('./routes/routes.js');
const sequelize = require('./db/database.js');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 4000;
@@ -25,9 +26,31 @@ app.use((req, res, next) => {
next();
});
// Routes
// ? Routes
// * media access route
app.use('/api/v1/media/:category/:type', (req, res, next) => {
const category = req.params.category;
const type = req.params.type;
if (!['entries', 'collections', 'franchises'].includes(category)) {
return res.status(404).send('Not Found');
}
if (!['banners', 'posters', 'others'].includes(type)) {
return res.status(404).send('Not Found');
}
const options = {
fallthrough: false, // ! Important: don't call next middleware if file isn't found
};
express.static(path.join(__dirname, '..', `uploads/${category}/${type}`), options)(req, res, next);
});
// * other api routes
app.use('/api/v1', routes);
// Start the server
const relations = require('./db/relations.js');
const sequelizeHooks = require('./db/sequelizeHooks.js');