|
1 | 1 | import { firestore } from '../utils/db.js'; |
2 | | -import { |
3 | | - applyArrayFilter, |
4 | | - generateQueryCacheKey, |
5 | | - getCachedQueryResult, |
6 | | - setCachedQueryResult |
7 | | -} from '../utils/controllerHelpers.js'; |
| 2 | +import { executeQuery, validateArrayParameter } from '../utils/controllerHelpers.js'; |
8 | 3 |
|
9 | 4 | /** |
10 | | - * List categories with optional filtering and field selection - Optimized version |
| 5 | + * List categories with optional filtering and field selection |
11 | 6 | */ |
12 | 7 | const listCategories = async (req, res) => { |
13 | | - try { |
14 | | - const params = req.query; |
| 8 | + const queryBuilder = async (params) => { |
15 | 9 | const isOnlyNames = params.onlyname || typeof params.onlyname === 'string'; |
16 | 10 | const hasCustomFields = params.fields && !isOnlyNames; |
17 | 11 |
|
18 | | - // Create cache key for this specific query |
19 | | - const queryFilters = { |
20 | | - category: params.category, |
21 | | - onlyname: isOnlyNames, |
22 | | - fields: params.fields |
23 | | - }; |
24 | | - const cacheKey = generateQueryCacheKey('categories', queryFilters); |
25 | | - |
26 | | - // Check cache first |
27 | | - const cachedResult = getCachedQueryResult(cacheKey); |
28 | | - if (cachedResult) { |
29 | | - res.statusCode = 200; |
30 | | - res.end(JSON.stringify(cachedResult)); |
31 | | - return; |
32 | | - } |
33 | | - |
34 | 12 | let query = firestore.collection('categories').orderBy('category', 'asc'); |
35 | 13 |
|
36 | | - // Apply category filter using shared utility |
37 | | - query = applyArrayFilter(query, 'category', params.category); |
| 14 | + // Apply category filter with validation |
| 15 | + if (params.category) { |
| 16 | + const categories = validateArrayParameter(params.category, 'category'); |
| 17 | + if (categories.length > 0) { |
| 18 | + query = query.where('category', 'in', categories); |
| 19 | + } |
| 20 | + } |
38 | 21 |
|
| 22 | + // Apply field selection |
39 | 23 | if (isOnlyNames) { |
40 | | - // Only select category field for names-only queries |
41 | 24 | query = query.select('category'); |
42 | 25 | } else if (hasCustomFields) { |
43 | | - // Select only requested fields |
44 | 26 | const requestedFields = params.fields.split(',').map(f => f.trim()); |
45 | 27 | query = query.select(...requestedFields); |
46 | 28 | } |
47 | 29 |
|
48 | | - // Execute query |
49 | | - const snapshot = await query.get(); |
50 | | - const data = []; |
| 30 | + return query; |
| 31 | + }; |
51 | 32 |
|
52 | | - // Process results based on response type |
53 | | - snapshot.forEach(doc => { |
54 | | - const docData = doc.data(); |
| 33 | + const dataProcessor = (data, params) => { |
| 34 | + const isOnlyNames = params.onlyname || typeof params.onlyname === 'string'; |
55 | 35 |
|
56 | | - if (isOnlyNames) { |
57 | | - data.push(docData.category); |
58 | | - } else { |
59 | | - // Data already filtered by select(), just return it |
60 | | - data.push(docData); |
61 | | - } |
62 | | - }); |
| 36 | + if (isOnlyNames) { |
| 37 | + return data.map(item => item.category); |
| 38 | + } |
| 39 | + |
| 40 | + return data; |
| 41 | + }; |
63 | 42 |
|
64 | | - // Cache the result |
65 | | - setCachedQueryResult(cacheKey, data); |
| 43 | + // Include onlyname and fields in cache key calculation |
| 44 | + const customCacheKeyData = { |
| 45 | + onlyname: req.query.onlyname || false, |
| 46 | + fields: req.query.fields |
| 47 | + }; |
66 | 48 |
|
67 | | - // Direct response |
68 | | - res.statusCode = 200; |
69 | | - res.end(JSON.stringify(data)); |
70 | | - } catch (error) { |
71 | | - console.error('Error fetching categories:', error); |
72 | | - res.statusCode = 500; |
73 | | - res.end(JSON.stringify({ |
74 | | - errors: [{ error: 'Failed to fetch categories' }] |
75 | | - })); |
76 | | - } |
| 49 | + await executeQuery(req, res, 'categories', queryBuilder, dataProcessor, customCacheKeyData); |
77 | 50 | }; |
78 | 51 |
|
79 | 52 | export { listCategories }; |
0 commit comments