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