Spaces:
Paused
Paused
File size: 2,453 Bytes
498f280 e4256f0 498f280 e4256f0 498f280 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | 'use strict';
const marketplaceService = require('../services/marketplace.service');
const bookingService = require('../services/booking.service');
const asyncHandler = (fn) => (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
const list = asyncHandler(async (req, res) => {
const { category, limit, offset } = req.query;
const products = await marketplaceService.listProducts({ category, limit, offset });
res.json({ data: products });
});
const getById = asyncHandler(async (req, res) => {
const product = await marketplaceService.getProductById(req.params.id);
if (!product) return res.status(404).json({ error: { message: 'Product not found' } });
res.json({ data: product });
});
const create = asyncHandler(async (req, res) => {
const userId = req.user.id;
const providerProfile = await bookingService.getProviderProfile(userId);
if (!providerProfile) {
return res.status(403).json({ error: { message: 'Only registered providers can list products' } });
}
const product = await marketplaceService.createProduct({
providerId: providerProfile.id,
...req.body
});
res.status(201).json({ data: product });
});
const update = asyncHandler(async (req, res) => {
const userId = req.user.id;
const providerProfile = await bookingService.getProviderProfile(userId);
if (!providerProfile) {
return res.status(403).json({ error: { message: 'Unauthorized' } });
}
try {
const product = await marketplaceService.updateProduct(req.params.id, req.body, providerProfile.id);
if (!product) return res.status(404).json({ error: { message: 'Product not found' } });
res.json({ data: product });
} catch (err) {
res.status(403).json({ error: { message: err.message } });
}
});
const remove = asyncHandler(async (req, res) => {
const userId = req.user.id;
const providerProfile = await bookingService.getProviderProfile(userId);
if (!providerProfile) {
return res.status(403).json({ error: { message: 'Unauthorized' } });
}
try {
const result = await marketplaceService.deleteProduct(req.params.id, providerProfile.id);
if (!result) return res.status(404).json({ error: { message: 'Product not found' } });
res.json({ success: true, message: 'Product deleted' });
} catch (err) {
res.status(403).json({ error: { message: err.message } });
}
});
module.exports = {
list,
getById,
create,
update,
remove
};
|