Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | 2x 2x 2x 2x 23x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 23x 4x 4x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 23x 9x 9x 2x 9x 3x 2x 2x 2x 2x 2x 6x 2x 6x 2x 2x 2x 2x 2x 2x 5x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 23x 5x 5x 2x 5x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | // src/routes/category.routes.ts
import { Router, Request, Response, NextFunction } from 'express';
import { CategoryDbService } from '../services/db/category.db';
const router = Router();
/**
* @swagger
* /api/categories:
* get:
* summary: List all available grocery categories
* description: Returns a list of all predefined grocery categories. Use this endpoint to populate category dropdowns in the UI.
* tags: [Categories]
* responses:
* 200:
* description: List of categories ordered alphabetically by name
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* data:
* type: array
* items:
* type: object
* properties:
* category_id:
* type: integer
* example: 3
* name:
* type: string
* example: "Dairy & Eggs"
* created_at:
* type: string
* format: date-time
* updated_at:
* type: string
* format: date-time
* 500:
* description: Server error
*/
router.get('/', async (req: Request, res: Response, next: NextFunction) => {
try {
const categories = await CategoryDbService.getAllCategories(req.log);
res.json({
success: true,
data: categories,
});
} catch (error) {
next(error);
}
});
/**
* @swagger
* /api/categories/lookup:
* get:
* summary: Lookup category by name
* description: Find a category by its name (case-insensitive). This endpoint is provided for migration support to help clients transition from using category names to category IDs.
* tags: [Categories]
* parameters:
* - in: query
* name: name
* required: true
* schema:
* type: string
* description: The category name to search for (case-insensitive)
* example: "Dairy & Eggs"
* responses:
* 200:
* description: Category found
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* data:
* type: object
* properties:
* category_id:
* type: integer
* name:
* type: string
* 404:
* description: Category not found
* 400:
* description: Missing or invalid query parameter
*/
router.get('/lookup', async (req: Request, res: Response, next: NextFunction) => {
try {
const name = req.query.name as string;
if (!name || typeof name !== 'string' || name.trim() === '') {
return res.status(400).json({
success: false,
error: 'Query parameter "name" is required and must be a non-empty string',
});
}
const category = await CategoryDbService.getCategoryByName(name, req.log);
if (!category) {
return res.status(404).json({
success: false,
error: `Category '${name}' not found`,
});
}
res.json({
success: true,
data: category,
});
} catch (error) {
next(error);
}
});
/**
* @swagger
* /api/categories/{id}:
* get:
* summary: Get a specific category by ID
* description: Retrieve detailed information about a single category
* tags: [Categories]
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: The category ID
* responses:
* 200:
* description: Category details
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* data:
* type: object
* properties:
* category_id:
* type: integer
* name:
* type: string
* created_at:
* type: string
* format: date-time
* updated_at:
* type: string
* format: date-time
* 404:
* description: Category not found
* 400:
* description: Invalid category ID
*/
router.get('/:id', async (req: Request, res: Response, next: NextFunction) => {
try {
const categoryId = parseInt(req.params.id, 10);
if (isNaN(categoryId) || categoryId <= 0) {
return res.status(400).json({
success: false,
error: 'Invalid category ID. Must be a positive integer.',
});
}
const category = await CategoryDbService.getCategoryById(categoryId, req.log);
if (!category) {
return res.status(404).json({
success: false,
error: `Category with ID ${categoryId} not found`,
});
}
res.json({
success: true,
data: category,
});
} catch (error) {
next(error);
}
});
export default router;
|