Spaces:
Paused
Paused
Download src/controllers/product.controller.js from imkrish/yatra-x-booking: direct link, hf CLI and curl.
- Browser
- Download file 2.45 kB
-
https://huggingface.co/spaces/imkrish/yatra-x-booking/resolve/main/src/controllers/product.controller.js
- Command line
-
hf download hf://spaces/imkrish/yatra-x-booking/src/controllers/product.controller.js
-
curl -L -o product.controller.js https://huggingface.co/spaces/imkrish/yatra-x-booking/resolve/main/src/controllers/product.controller.js
2.45 kB
| ; | |
| 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 | |
| }; | |