const express = require('express'); const { v4: uuidv4 } = require('uuid'); const router = express.Router(); module.exports = (pool, query, authMiddleware) => { // Apply authentication middleware to all routes router.use(authMiddleware); // Get all categories router.get('/', async (req, res, next) => { try { const result = await query('SELECT * FROM product_categories ORDER BY name ASC'); res.json(result.rows); } catch (error) { next(error); } }); // Get single category by ID router.get('/:id', async (req, res, next) => { try { const { id } = req.params; const result = await query( 'SELECT * FROM product_categories WHERE id = $1', [id] ); if (result.rows.length === 0) { return res.status(404).json({ error: true, message: 'Category not found' }); } res.json(result.rows[0]); } catch (error) { next(error); } }); // Create a new category router.post('/', async (req, res, next) => { try { const { name, description, imagePath } = req.body; // Validate required fields if (!name) { return res.status(400).json({ error: true, message: 'Category name is required' }); } // Check if category with same name already exists const existingCategory = await query( 'SELECT * FROM product_categories WHERE name = $1', [name] ); if (existingCategory.rows.length > 0) { return res.status(400).json({ error: true, message: 'A category with this name already exists' }); } // Create new category const result = await query( 'INSERT INTO product_categories (id, name, description, image_path) VALUES ($1, $2, $3, $4) RETURNING *', [uuidv4(), name, description || null, imagePath || null] ); res.status(201).json({ message: 'Category created successfully', category: result.rows[0] }); } catch (error) { next(error); } }); // Update a category router.put('/:id', async (req, res, next) => { try { const { id } = req.params; const { name, description, imagePath } = req.body; // Validate required fields if (!name) { return res.status(400).json({ error: true, message: 'Category name is required' }); } // Check if category exists const categoryCheck = await query( 'SELECT * FROM product_categories WHERE id = $1', [id] ); if (categoryCheck.rows.length === 0) { return res.status(404).json({ error: true, message: 'Category not found' }); } // Check if new name conflicts with existing category if (name !== categoryCheck.rows[0].name) { const nameCheck = await query( 'SELECT * FROM product_categories WHERE name = $1 AND id != $2', [name, id] ); if (nameCheck.rows.length > 0) { return res.status(400).json({ error: true, message: 'A category with this name already exists' }); } } // Update category const result = await query( 'UPDATE product_categories SET name = $1, description = $2, image_path = $3 WHERE id = $4 RETURNING *', [name, description || null, imagePath, id] ); res.json({ message: 'Category updated successfully', category: result.rows[0] }); } catch (error) { next(error); } }); // Delete a category router.delete('/:id', async (req, res, next) => { try { const { id } = req.params; // Check if category exists const categoryCheck = await query( 'SELECT * FROM product_categories WHERE id = $1', [id] ); if (categoryCheck.rows.length === 0) { return res.status(404).json({ error: true, message: 'Category not found' }); } // Check if category is being used by products const productsUsingCategory = await query( 'SELECT COUNT(*) FROM products WHERE category_id = $1', [id] ); if (parseInt(productsUsingCategory.rows[0].count) > 0) { return res.status(400).json({ error: true, message: 'This category cannot be deleted because it is associated with products. Please reassign those products to a different category first.' }); } // Delete category await query( 'DELETE FROM product_categories WHERE id = $1', [id] ); res.json({ message: 'Category deleted successfully' }); } catch (error) { next(error); } }); return router; };