Skip to content

Commit 0023066

Browse files
committed
refactor(api): wrap data endpoint responses in SuccessApiResponse
- Modify GET and PUT handlers to use dynamic item type instead of Map<String, dynamic> - Wrap returned items in SuccessApiResponse object - Use generic toJson method for serialization - Remove redundant package imports
1 parent 7dab2d2 commit 0023066

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

routes/api/v1/data/[id].dart

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33

44
import 'dart:io';
55

6+
import 'package:dart_frog/dart_frog.dart';
7+
import 'package:ht_api/src/registry/model_registry.dart';
68
import 'package:dart_frog/dart_frog.dart';
79
import 'package:ht_api/src/registry/model_registry.dart';
810
import 'package:ht_data_repository/ht_data_repository.dart';
911
import 'package:ht_http_client/ht_http_client.dart'; // Import exceptions
10-
import 'package:ht_shared/ht_shared.dart'; // Import models
12+
import 'package:ht_shared/ht_shared.dart'; // Import models, SuccessApiResponse
1113

1214
/// Handles requests for the /api/v1/data/[id] endpoint.
1315
/// Dispatches requests to specific handlers based on the HTTP method.
@@ -55,25 +57,21 @@ Future<Response> _handleGet(
5557
String id,
5658
String modelName,
5759
) async {
58-
Map<String, dynamic> itemJson;
60+
dynamic item; // Use dynamic
5961
// Repository exceptions (like NotFoundException) will propagate up.
6062
switch (modelName) {
6163
case 'headline':
6264
final repo = context.read<HtDataRepository<Headline>>();
63-
final item = await repo.read(id);
64-
itemJson = item.toJson();
65+
item = await repo.read(id);
6566
case 'category':
6667
final repo = context.read<HtDataRepository<Category>>();
67-
final item = await repo.read(id);
68-
itemJson = item.toJson();
68+
item = await repo.read(id);
6969
case 'source':
7070
final repo = context.read<HtDataRepository<Source>>();
71-
final item = await repo.read(id);
72-
itemJson = item.toJson();
71+
item = await repo.read(id);
7372
case 'country':
7473
final repo = context.read<HtDataRepository<Country>>();
75-
final item = await repo.read(id);
76-
itemJson = item.toJson();
74+
item = await repo.read(id);
7775
default:
7876
// This case should ideally be caught by middleware, but added for safety
7977
return Response(
@@ -82,8 +80,20 @@ Future<Response> _handleGet(
8280
'Internal Server Error: Unsupported model type "$modelName" reached handler.',
8381
);
8482
}
85-
// Return the serialized item
86-
return Response.json(body: itemJson);
83+
84+
// Wrap the item in SuccessApiResponse and serialize
85+
final successResponse = SuccessApiResponse<dynamic>(
86+
data: item,
87+
// metadata: ResponseMetadata(timestamp: DateTime.now()), // Optional
88+
);
89+
90+
// Provide the correct toJsonT for the specific model type
91+
final responseJson = successResponse.toJson(
92+
(item) => (item as dynamic).toJson(), // Assuming all models have toJson
93+
);
94+
95+
// Return the serialized response
96+
return Response.json(body: responseJson);
8797
}
8898

8999
// --- PUT Handler ---
@@ -121,7 +131,7 @@ Future<Response> _handlePut(
121131
);
122132
}
123133

124-
Map<String, dynamic> updatedJson;
134+
dynamic updatedItem; // Use dynamic
125135
// Repository exceptions (like NotFoundException, BadRequestException)
126136
// will propagate up.
127137
switch (modelName) {
@@ -136,8 +146,7 @@ Future<Response> _handlePut(
136146
'Bad Request: ID in request body ("${typedItem.id}") does not match ID in path ("$id").',
137147
);
138148
}
139-
final updatedItem = await repo.update(id, typedItem);
140-
updatedJson = updatedItem.toJson();
149+
updatedItem = await repo.update(id, typedItem);
141150
}
142151
case 'category':
143152
{
@@ -150,8 +159,7 @@ Future<Response> _handlePut(
150159
'Bad Request: ID in request body ("${typedItem.id}") does not match ID in path ("$id").',
151160
);
152161
}
153-
final updatedItem = await repo.update(id, typedItem);
154-
updatedJson = updatedItem.toJson();
162+
updatedItem = await repo.update(id, typedItem);
155163
}
156164
case 'source':
157165
{
@@ -164,8 +172,7 @@ Future<Response> _handlePut(
164172
'Bad Request: ID in request body ("${typedItem.id}") does not match ID in path ("$id").',
165173
);
166174
}
167-
final updatedItem = await repo.update(id, typedItem);
168-
updatedJson = updatedItem.toJson();
175+
updatedItem = await repo.update(id, typedItem);
169176
}
170177
case 'country':
171178
{
@@ -178,8 +185,7 @@ Future<Response> _handlePut(
178185
'Bad Request: ID in request body ("${typedItem.id}") does not match ID in path ("$id").',
179186
);
180187
}
181-
final updatedItem = await repo.update(id, typedItem);
182-
updatedJson = updatedItem.toJson();
188+
updatedItem = await repo.update(id, typedItem);
183189
}
184190
default:
185191
// This case should ideally be caught by middleware, but added for safety
@@ -189,8 +195,20 @@ Future<Response> _handlePut(
189195
'Internal Server Error: Unsupported model type "$modelName" reached handler.',
190196
);
191197
}
192-
// Return the serialized updated item
193-
return Response.json(body: updatedJson);
198+
199+
// Wrap the updated item in SuccessApiResponse and serialize
200+
final successResponse = SuccessApiResponse<dynamic>(
201+
data: updatedItem,
202+
// metadata: ResponseMetadata(timestamp: DateTime.now()), // Optional
203+
);
204+
205+
// Provide the correct toJsonT for the specific model type
206+
final responseJson = successResponse.toJson(
207+
(item) => (item as dynamic).toJson(), // Assuming all models have toJson
208+
);
209+
210+
// Return the serialized response
211+
return Response.json(body: responseJson);
194212
}
195213

196214
// --- DELETE Handler ---

0 commit comments

Comments
 (0)