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
11
13
12
14
/// Handles requests for the /api/v1/data/[id] endpoint.
13
15
/// Dispatches requests to specific handlers based on the HTTP method.
@@ -55,25 +57,21 @@ Future<Response> _handleGet(
55
57
String id,
56
58
String modelName,
57
59
) async {
58
- Map < String , dynamic > itemJson;
60
+ dynamic item; // Use dynamic
59
61
// Repository exceptions (like NotFoundException) will propagate up.
60
62
switch (modelName) {
61
63
case 'headline' :
62
64
final repo = context.read <HtDataRepository <Headline >>();
63
- final item = await repo.read (id);
64
- itemJson = item.toJson ();
65
+ item = await repo.read (id);
65
66
case 'category' :
66
67
final repo = context.read <HtDataRepository <Category >>();
67
- final item = await repo.read (id);
68
- itemJson = item.toJson ();
68
+ item = await repo.read (id);
69
69
case 'source' :
70
70
final repo = context.read <HtDataRepository <Source >>();
71
- final item = await repo.read (id);
72
- itemJson = item.toJson ();
71
+ item = await repo.read (id);
73
72
case 'country' :
74
73
final repo = context.read <HtDataRepository <Country >>();
75
- final item = await repo.read (id);
76
- itemJson = item.toJson ();
74
+ item = await repo.read (id);
77
75
default :
78
76
// This case should ideally be caught by middleware, but added for safety
79
77
return Response (
@@ -82,8 +80,20 @@ Future<Response> _handleGet(
82
80
'Internal Server Error: Unsupported model type "$modelName " reached handler.' ,
83
81
);
84
82
}
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);
87
97
}
88
98
89
99
// --- PUT Handler ---
@@ -121,7 +131,7 @@ Future<Response> _handlePut(
121
131
);
122
132
}
123
133
124
- Map < String , dynamic > updatedJson;
134
+ dynamic updatedItem; // Use dynamic
125
135
// Repository exceptions (like NotFoundException, BadRequestException)
126
136
// will propagate up.
127
137
switch (modelName) {
@@ -136,8 +146,7 @@ Future<Response> _handlePut(
136
146
'Bad Request: ID in request body ("${typedItem .id }") does not match ID in path ("$id ").' ,
137
147
);
138
148
}
139
- final updatedItem = await repo.update (id, typedItem);
140
- updatedJson = updatedItem.toJson ();
149
+ updatedItem = await repo.update (id, typedItem);
141
150
}
142
151
case 'category' :
143
152
{
@@ -150,8 +159,7 @@ Future<Response> _handlePut(
150
159
'Bad Request: ID in request body ("${typedItem .id }") does not match ID in path ("$id ").' ,
151
160
);
152
161
}
153
- final updatedItem = await repo.update (id, typedItem);
154
- updatedJson = updatedItem.toJson ();
162
+ updatedItem = await repo.update (id, typedItem);
155
163
}
156
164
case 'source' :
157
165
{
@@ -164,8 +172,7 @@ Future<Response> _handlePut(
164
172
'Bad Request: ID in request body ("${typedItem .id }") does not match ID in path ("$id ").' ,
165
173
);
166
174
}
167
- final updatedItem = await repo.update (id, typedItem);
168
- updatedJson = updatedItem.toJson ();
175
+ updatedItem = await repo.update (id, typedItem);
169
176
}
170
177
case 'country' :
171
178
{
@@ -178,8 +185,7 @@ Future<Response> _handlePut(
178
185
'Bad Request: ID in request body ("${typedItem .id }") does not match ID in path ("$id ").' ,
179
186
);
180
187
}
181
- final updatedItem = await repo.update (id, typedItem);
182
- updatedJson = updatedItem.toJson ();
188
+ updatedItem = await repo.update (id, typedItem);
183
189
}
184
190
default :
185
191
// This case should ideally be caught by middleware, but added for safety
@@ -189,8 +195,20 @@ Future<Response> _handlePut(
189
195
'Internal Server Error: Unsupported model type "$modelName " reached handler.' ,
190
196
);
191
197
}
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);
194
212
}
195
213
196
214
// --- DELETE Handler ---
0 commit comments