API Functions for communities and product offers added
This commit is contained in:
18
routes/communities.js
Normal file
18
routes/communities.js
Normal file
@@ -0,0 +1,18 @@
|
||||
import {Router} from 'express';
|
||||
import { createCommunity, getCommunityById, getAllCommunities, deleteCommunity, editCommunity } from '../functions/communityFunctions.js';
|
||||
const router = Router();
|
||||
|
||||
// Validate create community payload
|
||||
const validateCommunityPayload = (req, res, next) => {
|
||||
const { nombre, descripcion } = req.body;
|
||||
if (!nombre || !descripcion) {
|
||||
return res.status(400).json({ error: 'nombre and descripcion are required' });
|
||||
}
|
||||
next();
|
||||
};
|
||||
|
||||
router.post('/', validateCommunityPayload, createCommunity);
|
||||
router.get('/', getAllCommunities);
|
||||
router.get('/:id_comunidad', getCommunityById);
|
||||
router.put('/:id_comunidad', validateCommunityPayload, editCommunity);
|
||||
router.delete('/:id_comunidad', deleteCommunity);
|
||||
81
routes/offers.js
Normal file
81
routes/offers.js
Normal file
@@ -0,0 +1,81 @@
|
||||
import { Router } from "express";
|
||||
import { createNewOffer, getOfferById, deleteOffer, getAllOffers, getOffersByUserId, editOffer } from "../functions/offerFunctions.js";
|
||||
const router = Router();
|
||||
|
||||
const storage = multer.memoryStorage();
|
||||
|
||||
const fileFilter = (req, file, cb) => {
|
||||
if (file.mimetype.startsWith('image/')) {
|
||||
cb(null, true);
|
||||
} else {
|
||||
cb(new Error('Solo se permiten imágenes'), false);
|
||||
}
|
||||
};
|
||||
|
||||
const upload = multer({
|
||||
storage: storage,
|
||||
fileFilter: fileFilter,
|
||||
limits: {
|
||||
fileSize: 5 * 1024 * 1024 // 5MB limit
|
||||
}
|
||||
});
|
||||
|
||||
async function processImage(file) {
|
||||
const imagePath = path.join(process.cwd(), 'uploads');
|
||||
|
||||
const buffer = file.buffer;
|
||||
const hash = crypto.createHash('sha256').update(buffer).digest('hex');
|
||||
const mime = await fileTypeFromBuffer(buffer).then(type => type?.mime);
|
||||
|
||||
// Validate MIME type
|
||||
if (!mime || !['image/jpeg', 'image/png', 'image/gif', 'image/webp'].includes(mime)) {
|
||||
throw new Error('Unsupported image format');
|
||||
}
|
||||
|
||||
// File save name and path if not GIF
|
||||
let filename;
|
||||
if (mime === 'image/gif') {
|
||||
filename = `${hash}.gif`;
|
||||
await fs.writeFile(path.join(imagePath, filename), buffer);
|
||||
} else {
|
||||
filename = `${hash}.webp`;
|
||||
await sharp(buffer)
|
||||
.resize({ width: 800, height: 800, fit: 'inside', withoutEnlargement: true })
|
||||
.rotate()
|
||||
.normalise()
|
||||
.withMetadata()
|
||||
.toFormat('webp', { quality: 80 })
|
||||
.toFile(path.join(imagePath, filename));
|
||||
}
|
||||
return filename;
|
||||
}
|
||||
|
||||
const processUploadedImage = async (req, res, next) => {
|
||||
try {
|
||||
if (req.file) {
|
||||
const filename = await processImage(req.file);
|
||||
req.processedFilename = filename;
|
||||
}
|
||||
next();
|
||||
} catch (error) {
|
||||
console.error('Error processing image:', error);
|
||||
res.status(400).json({ error: 'Error al procesar la imagen: ' + error.message });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Validate create offer payload
|
||||
const validateOfferPayload = (req, res, next) => {
|
||||
const { nombre, descripcion, precio, id_usuario } = req.body;
|
||||
if (!nombre || !descripcion || !precio || !id_usuario) {
|
||||
return res.status(400).json({ error: 'nombre, descripcion, precio and id_usuario are required' });
|
||||
}
|
||||
next();
|
||||
};
|
||||
|
||||
router.post('/', upload.single('imagen'), validateOfferPayload, processUploadedImage, createNewOffer);
|
||||
router.get('/', getAllOffers);
|
||||
router.get('/:id_oferta', getOfferById);
|
||||
router.put('/:id_oferta', upload.single('imagen'), validateOfferPayload, processUploadedImage, editOffer);
|
||||
router.delete('/:id_oferta', deleteOffer);
|
||||
router.get('/user/:id_usuario', getOffersByUserId);
|
||||
Reference in New Issue
Block a user