"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const express_1 = require("express"); const client_1 = require("@prisma/client"); const joi_1 = __importDefault(require("joi")); const router = (0, express_1.Router)(); const prisma = new client_1.PrismaClient(); const ingredientSchema = joi_1.default.object({ recipeNumber: joi_1.default.string().required().max(20), ingredients: joi_1.default.string().required(), }); const updateIngredientSchema = ingredientSchema.fork(['recipeNumber'], (schema) => schema.optional()); router.get('/', async (req, res, next) => { try { const { search = '', category = '', page = '1', limit = '10', sortBy = 'recipeNumber', sortOrder = 'asc' } = req.query; const pageNum = parseInt(page); const limitNum = parseInt(limit); const skip = (pageNum - 1) * limitNum; const where = {}; if (search) { where.OR = [ { recipeNumber: { contains: search } }, { ingredients: { contains: search } }, ]; } if (category) { where.recipeNumber = { contains: category }; } const [ingredients, total] = await Promise.all([ prisma.ingredient.findMany({ where, orderBy: { [sortBy]: sortOrder }, skip, take: limitNum, }), prisma.ingredient.count({ where }) ]); return res.json({ success: true, data: ingredients, pagination: { page: pageNum, limit: limitNum, total, pages: Math.ceil(total / limitNum), }, }); } catch (error) { next(error); } }); router.get('/:id', async (req, res, next) => { try { const { id } = req.params; if (!id) { return res.status(400).json({ success: false, message: 'Ingredient ID is required', }); } const ingredient = await prisma.ingredient.findUnique({ where: { id: parseInt(id) } }); if (!ingredient) { return res.status(404).json({ success: false, message: 'Ingredient not found', }); } return res.json({ success: true, data: ingredient, }); } catch (error) { next(error); } }); router.post('/', async (req, res, next) => { try { const { error, value } = ingredientSchema.validate(req.body); if (error) { return res.status(400).json({ success: false, message: 'Validation error', details: error.details, }); } const ingredient = await prisma.ingredient.create({ data: value }); return res.status(201).json({ success: true, data: ingredient, message: 'Ingredient created successfully', }); } catch (error) { next(error); } }); router.put('/:id', async (req, res, next) => { try { const { id } = req.params; if (!id) { return res.status(400).json({ success: false, message: 'Ingredient ID is required', }); } const { error, value } = updateIngredientSchema.validate(req.body); if (error) { return res.status(400).json({ success: false, message: 'Validation error', details: error.details, }); } const ingredient = await prisma.ingredient.update({ where: { id: parseInt(id) }, data: value }); return res.json({ success: true, data: ingredient, message: 'Ingredient updated successfully', }); } catch (error) { next(error); } }); router.delete('/:id', async (req, res, next) => { try { const { id } = req.params; if (!id) { return res.status(400).json({ success: false, message: 'Ingredient ID is required', }); } await prisma.ingredient.delete({ where: { id: parseInt(id) } }); return res.json({ success: true, message: 'Ingredient deleted successfully', }); } catch (error) { next(error); } }); exports.default = router; //# sourceMappingURL=ingredients.js.map