3
3
4
4
import 'dart:io' ;
5
5
6
+ import 'package:dart_frog/dart_frog.dart' ;
7
+ import 'package:ht_api/src/registry/model_registry.dart' ;
6
8
import 'package:dart_frog/dart_frog.dart' ;
7
9
import 'package:ht_api/src/registry/model_registry.dart' ;
8
10
import 'package:ht_data_repository/ht_data_repository.dart' ;
9
11
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, PaginatedResponse
11
13
12
14
/// Handles requests for the /api/v1/data collection endpoint.
13
15
/// Dispatches requests to specific handlers based on the HTTP method.
@@ -61,49 +63,45 @@ Future<Response> _handleGet(RequestContext context, String modelName) async {
61
63
..remove ('limit' );
62
64
63
65
// Process based on model type
64
- List < Map < String , dynamic >> jsonList;
66
+ PaginatedResponse < dynamic > paginatedResponse; // Use dynamic for the list
65
67
try {
66
68
switch (modelName) {
67
69
case 'headline' :
68
70
final repo = context.read <HtDataRepository <Headline >>();
69
- final results = specificQuery.isNotEmpty
71
+ paginatedResponse = specificQuery.isNotEmpty
70
72
? await repo.readAllByQuery (
71
73
specificQuery,
72
74
startAfterId: startAfterId,
73
75
limit: limit,
74
76
)
75
77
: await repo.readAll (startAfterId: startAfterId, limit: limit);
76
- jsonList = results.map ((item) => item.toJson ()).toList ();
77
78
case 'category' :
78
79
final repo = context.read <HtDataRepository <Category >>();
79
- final results = specificQuery.isNotEmpty
80
+ paginatedResponse = specificQuery.isNotEmpty
80
81
? await repo.readAllByQuery (
81
82
specificQuery,
82
83
startAfterId: startAfterId,
83
84
limit: limit,
84
85
)
85
86
: await repo.readAll (startAfterId: startAfterId, limit: limit);
86
- jsonList = results.map ((item) => item.toJson ()).toList ();
87
87
case 'source' :
88
88
final repo = context.read <HtDataRepository <Source >>();
89
- final results = specificQuery.isNotEmpty
89
+ paginatedResponse = specificQuery.isNotEmpty
90
90
? await repo.readAllByQuery (
91
91
specificQuery,
92
92
startAfterId: startAfterId,
93
93
limit: limit,
94
94
)
95
95
: await repo.readAll (startAfterId: startAfterId, limit: limit);
96
- jsonList = results.map ((item) => item.toJson ()).toList ();
97
96
case 'country' :
98
97
final repo = context.read <HtDataRepository <Country >>();
99
- final results = specificQuery.isNotEmpty
98
+ paginatedResponse = specificQuery.isNotEmpty
100
99
? await repo.readAllByQuery (
101
100
specificQuery,
102
101
startAfterId: startAfterId,
103
102
limit: limit,
104
103
)
105
104
: await repo.readAll (startAfterId: startAfterId, limit: limit);
106
- jsonList = results.map ((item) => item.toJson ()).toList ();
107
105
default :
108
106
// This case should be caught by middleware, but added for safety
109
107
return Response (
@@ -123,8 +121,21 @@ Future<Response> _handleGet(RequestContext context, String modelName) async {
123
121
'Internal Server Error: Could not resolve repository for model "$modelName ".' ,
124
122
);
125
123
}
126
- // Return the serialized list
127
- return Response .json (body: jsonList);
124
+
125
+ // Wrap the PaginatedResponse in SuccessApiResponse and serialize
126
+ final successResponse = SuccessApiResponse <PaginatedResponse <dynamic >>(
127
+ data: paginatedResponse,
128
+ // metadata: ResponseMetadata(timestamp: DateTime.now()), // Optional
129
+ );
130
+
131
+ // Need to provide the correct toJsonT for PaginatedResponse
132
+ final responseJson = successResponse.toJson (
133
+ (paginated) => paginated.toJson (
134
+ (item) => (item as dynamic ).toJson (), // Assuming all models have toJson
135
+ ),
136
+ );
137
+
138
+ return Response .json (body: responseJson);
128
139
}
129
140
130
141
// --- POST Handler ---
@@ -162,26 +173,22 @@ Future<Response> _handlePost(
162
173
}
163
174
164
175
// Process based on model type
165
- Map < String , dynamic > createdJson;
176
+ dynamic createdItem; // Use dynamic
166
177
// Repository exceptions (like BadRequestException from create) will propagate
167
178
// up to the main onRequest try/catch and be re-thrown to the middleware.
168
179
switch (modelName) {
169
180
case 'headline' :
170
181
final repo = context.read <HtDataRepository <Headline >>();
171
- final createdItem = await repo.create (newItem as Headline );
172
- createdJson = createdItem.toJson ();
182
+ createdItem = await repo.create (newItem as Headline );
173
183
case 'category' :
174
184
final repo = context.read <HtDataRepository <Category >>();
175
- final createdItem = await repo.create (newItem as Category );
176
- createdJson = createdItem.toJson ();
185
+ createdItem = await repo.create (newItem as Category );
177
186
case 'source' :
178
187
final repo = context.read <HtDataRepository <Source >>();
179
- final createdItem = await repo.create (newItem as Source );
180
- createdJson = createdItem.toJson ();
188
+ createdItem = await repo.create (newItem as Source );
181
189
case 'country' :
182
190
final repo = context.read <HtDataRepository <Country >>();
183
- final createdItem = await repo.create (newItem as Country );
184
- createdJson = createdItem.toJson ();
191
+ createdItem = await repo.create (newItem as Country );
185
192
default :
186
193
// This case should ideally be caught by middleware, but added for safety
187
194
return Response (
@@ -190,6 +197,18 @@ Future<Response> _handlePost(
190
197
'Internal Server Error: Unsupported model type "$modelName " reached handler.' ,
191
198
);
192
199
}
193
- // Return the serialized created item
194
- return Response .json (statusCode: HttpStatus .created, body: createdJson);
200
+
201
+ // Wrap the created item in SuccessApiResponse and serialize
202
+ final successResponse = SuccessApiResponse <dynamic >(
203
+ data: createdItem,
204
+ // metadata: ResponseMetadata(timestamp: DateTime.now()), // Optional
205
+ );
206
+
207
+ // Provide the correct toJsonT for the specific model type
208
+ final responseJson = successResponse.toJson (
209
+ (item) => (item as dynamic ).toJson (), // Assuming all models have toJson
210
+ );
211
+
212
+ // Return the serialized response
213
+ return Response .json (statusCode: HttpStatus .created, body: responseJson);
195
214
}
0 commit comments