Skip to content

Commit 58f615b

Browse files
committed
refactor(data): improve query parameter handling
- Simplify allowed key checks - Improve readability - Remove unnecessary breaks
1 parent 0787077 commit 58f615b

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

routes/api/v1/data/index.dart

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ Future<Response> _handleGet(
113113

114114
final specificQueryForClient = <String, String>{};
115115
final Set<String> allowedKeys;
116-
final receivedKeys =
117-
queryParams.keys.where((k) => k != 'model' && k != 'startAfterId' && k != 'limit').toSet();
116+
final receivedKeys = queryParams.keys
117+
.where((k) => k != 'model' && k != 'startAfterId' && k != 'limit')
118+
.toSet();
118119

119120
switch (modelName) {
120121
case 'headline':
@@ -131,7 +132,6 @@ Future<Response> _handleGet(
131132
specificQueryForClient['source.id_in'] = queryParams['sources']!;
132133
}
133134
}
134-
break;
135135
case 'source':
136136
allowedKeys = {'countries', 'sourceTypes', 'languages', 'q'};
137137
final qValue = queryParams['q'];
@@ -140,32 +140,32 @@ Future<Response> _handleGet(
140140
specificQueryForClient['description_contains'] = qValue;
141141
} else {
142142
if (queryParams.containsKey('countries')) {
143-
specificQueryForClient['headquarters.iso_code_in'] = queryParams['countries']!;
143+
specificQueryForClient['headquarters.iso_code_in'] =
144+
queryParams['countries']!;
144145
}
145146
if (queryParams.containsKey('sourceTypes')) {
146-
specificQueryForClient['source_type_in'] = queryParams['sourceTypes']!;
147+
specificQueryForClient['source_type_in'] =
148+
queryParams['sourceTypes']!;
147149
}
148150
if (queryParams.containsKey('languages')) {
149151
specificQueryForClient['language_in'] = queryParams['languages']!;
150152
}
151153
}
152-
break;
153154
case 'category':
154155
allowedKeys = {'q'};
155156
final qValue = queryParams['q'];
156157
if (qValue != null && qValue.isNotEmpty) {
157158
specificQueryForClient['name_contains'] = qValue;
158159
specificQueryForClient['description_contains'] = qValue;
159160
}
160-
break;
161161
case 'country':
162162
allowedKeys = {'q'};
163163
final qValue = queryParams['q'];
164164
if (qValue != null && qValue.isNotEmpty) {
165165
specificQueryForClient['name_contains'] = qValue;
166-
specificQueryForClient['iso_code_contains'] = qValue; // Also search iso_code
166+
specificQueryForClient['iso_code_contains'] =
167+
qValue; // Also search iso_code
167168
}
168-
break;
169169
default:
170170
// For other models, pass through all non-standard query params directly.
171171
// No specific validation of allowed keys for these other models here.
@@ -176,7 +176,6 @@ Future<Response> _handleGet(
176176
specificQueryForClient[key] = value;
177177
}
178178
});
179-
break;
180179
}
181180

182181
// Validate received keys against allowed keys for the specific models
@@ -195,7 +194,9 @@ Future<Response> _handleGet(
195194
}
196195

197196
PaginatedResponse<dynamic> paginatedResponse;
198-
String? userIdForRepoCall = modelConfig.getOwnerId != null ? authenticatedUser.id : null;
197+
// ignore: prefer_final_locals
198+
var userIdForRepoCall =
199+
modelConfig.getOwnerId != null ? authenticatedUser.id : null;
199200

200201
// Repository calls using specificQueryForClient
201202
switch (modelName) {
@@ -207,7 +208,6 @@ Future<Response> _handleGet(
207208
startAfterId: startAfterId,
208209
limit: limit,
209210
);
210-
break;
211211
case 'category':
212212
final repo = context.read<HtDataRepository<Category>>();
213213
paginatedResponse = await repo.readAllByQuery(
@@ -216,7 +216,6 @@ Future<Response> _handleGet(
216216
startAfterId: startAfterId,
217217
limit: limit,
218218
);
219-
break;
220219
case 'source':
221220
final repo = context.read<HtDataRepository<Source>>();
222221
paginatedResponse = await repo.readAllByQuery(
@@ -225,7 +224,6 @@ Future<Response> _handleGet(
225224
startAfterId: startAfterId,
226225
limit: limit,
227226
);
228-
break;
229227
case 'country':
230228
final repo = context.read<HtDataRepository<Country>>();
231229
paginatedResponse = await repo.readAllByQuery(
@@ -234,7 +232,6 @@ Future<Response> _handleGet(
234232
startAfterId: startAfterId,
235233
limit: limit,
236234
);
237-
break;
238235
case 'user':
239236
final repo = context.read<HtDataRepository<User>>();
240237
paginatedResponse = await repo.readAllByQuery(
@@ -243,7 +240,6 @@ Future<Response> _handleGet(
243240
startAfterId: startAfterId,
244241
limit: limit,
245242
);
246-
break;
247243
case 'user_app_settings':
248244
final repo = context.read<HtDataRepository<UserAppSettings>>();
249245
paginatedResponse = await repo.readAllByQuery(
@@ -252,7 +248,6 @@ Future<Response> _handleGet(
252248
startAfterId: startAfterId,
253249
limit: limit,
254250
);
255-
break;
256251
case 'user_content_preferences':
257252
final repo = context.read<HtDataRepository<UserContentPreferences>>();
258253
paginatedResponse = await repo.readAllByQuery(
@@ -261,7 +256,6 @@ Future<Response> _handleGet(
261256
startAfterId: startAfterId,
262257
limit: limit,
263258
);
264-
break;
265259
case 'app_config':
266260
final repo = context.read<HtDataRepository<AppConfig>>();
267261
paginatedResponse = await repo.readAllByQuery(
@@ -270,7 +264,6 @@ Future<Response> _handleGet(
270264
startAfterId: startAfterId,
271265
limit: limit,
272266
);
273-
break;
274267
default:
275268
throw OperationFailedException(
276269
'Unsupported model type "$modelName" reached data retrieval switch.',

0 commit comments

Comments
 (0)