Skip to content

Commit 9c6599e

Browse files
Merge pull request #43 from HTTPArchive/development
Cleanup
2 parents 36bef34 + 6943364 commit 9c6599e

15 files changed

+342
-1058
lines changed

src/controllers/adoptionController.js

Lines changed: 0 additions & 111 deletions
This file was deleted.
Lines changed: 27 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,52 @@
11
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';
93

104
/**
11-
* List categories with optional filtering and field selection - Optimized version
5+
* List categories with optional filtering and field selection
126
*/
137
const listCategories = async (req, res) => {
14-
try {
15-
const params = req.query;
8+
const queryBuilder = async (params) => {
169
const isOnlyNames = params.onlyname || typeof params.onlyname === 'string';
1710
const hasCustomFields = params.fields && !isOnlyNames;
1811

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-
3512
let query = firestore.collection('categories').orderBy('category', 'asc');
3613

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+
}
3921

22+
// Apply field selection
4023
if (isOnlyNames) {
41-
// Only select category field for names-only queries
4224
query = query.select('category');
4325
} else if (hasCustomFields) {
44-
// Select only requested fields
4526
const requestedFields = params.fields.split(',').map(f => f.trim());
4627
query = query.select(...requestedFields);
4728
}
4829

49-
// Execute query
50-
const snapshot = await query.get();
51-
const data = [];
30+
return query;
31+
};
5232

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';
5635

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+
};
6442

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+
};
6748

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);
7850
};
7951

8052
export { listCategories };

src/controllers/cwvtechController.js

Lines changed: 0 additions & 112 deletions
This file was deleted.

src/controllers/geosController.js

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,15 @@
11
import { firestore } from '../utils/db.js';
2-
import { handleControllerError, generateQueryCacheKey, getCachedQueryResult, setCachedQueryResult } from '../utils/controllerHelpers.js';
2+
import { executeQuery } from '../utils/controllerHelpers.js';
33

44
/**
55
* List all geographic locations from database
66
*/
77
const listGeos = async (req, res) => {
8-
try {
9-
// Generate cache key for this query
10-
const cacheKey = generateQueryCacheKey('geos', { orderBy: 'mobile_origins' });
8+
const queryBuilder = async () => {
9+
return firestore.collection('geos').orderBy('mobile_origins', 'desc').select('geo');
10+
};
1111

12-
// Check cache first
13-
const cachedResult = getCachedQueryResult(cacheKey);
14-
if (cachedResult) {
15-
res.statusCode = 200;
16-
res.end(JSON.stringify(cachedResult));
17-
return;
18-
}
19-
20-
const snapshot = await firestore.collection('geos').orderBy('mobile_origins', 'desc').select('geo').get();
21-
const data = [];
22-
23-
// Extract only the 'geo' property from each document
24-
snapshot.forEach(doc => {
25-
data.push(doc.data());
26-
});
27-
28-
// Cache the result
29-
setCachedQueryResult(cacheKey, data);
30-
31-
res.statusCode = 200;
32-
res.end(JSON.stringify(data));
33-
} catch (error) {
34-
handleControllerError(res, error, 'fetching geographic locations');
35-
}
12+
await executeQuery(req, res, 'geos', queryBuilder);
3613
};
3714

3815
export {

0 commit comments

Comments
 (0)