1
1
//
2
- // ignore_for_file: lines_longer_than_80_chars, avoid_catches_without_on_clauses
2
+ // ignore_for_file: lines_longer_than_80_chars, avoid_catches_without_on_clauses, avoid_catching_errors
3
3
4
4
import 'dart:io' ;
5
5
@@ -28,29 +28,29 @@ Future<Response> onRequest(RequestContext context, String id) async {
28
28
switch (modelName) {
29
29
case 'headline' :
30
30
final repo = context.read <HtDataRepository <Headline >>();
31
- final item = await repo.read (id);
32
- // Serialize using the specific model's toJson method
33
- itemJson = item.toJson ();
34
- case 'category' :
35
- final repo = context.read <HtDataRepository <Category >>();
36
- final item = await repo.read (id);
37
- itemJson = item.toJson ();
38
- case 'source' :
39
- final repo = context.read <HtDataRepository <Source >>();
40
- final item = await repo.read (id);
41
- itemJson = item.toJson ();
42
- case 'country' :
43
- final repo = context.read <HtDataRepository <Country >>();
44
- final item = await repo.read (id);
45
- itemJson = item.toJson ();
46
- default :
47
- // This case should ideally be caught by middleware, but added for safety
48
- return Response (
49
- statusCode: HttpStatus .internalServerError,
50
- body:
51
- 'Internal Server Error: Unsupported model type "$modelName " reached handler.' ,
52
- );
53
- }
31
+ final item = await repo.read (id);
32
+ // Serialize using the specific model's toJson method
33
+ itemJson = item.toJson ();
34
+ case 'category' :
35
+ final repo = context.read <HtDataRepository <Category >>();
36
+ final item = await repo.read (id);
37
+ itemJson = item.toJson ();
38
+ case 'source' :
39
+ final repo = context.read <HtDataRepository <Source >>();
40
+ final item = await repo.read (id);
41
+ itemJson = item.toJson ();
42
+ case 'country' :
43
+ final repo = context.read <HtDataRepository <Country >>();
44
+ final item = await repo.read (id);
45
+ itemJson = item.toJson ();
46
+ default :
47
+ // This case should ideally be caught by middleware, but added for safety
48
+ return Response (
49
+ statusCode: HttpStatus .internalServerError,
50
+ body:
51
+ 'Internal Server Error: Unsupported model type "$modelName " reached handler.' ,
52
+ );
53
+ }
54
54
// Return the serialized item
55
55
return Response .json (body: itemJson);
56
56
}
@@ -91,7 +91,8 @@ Future<Response> onRequest(RequestContext context, String id) async {
91
91
// Removed inner try-catch block to allow exceptions to propagate
92
92
switch (modelName) {
93
93
case 'headline' :
94
- { // Added block scope
94
+ {
95
+ // Added block scope
95
96
final repo = context.read <HtDataRepository <Headline >>();
96
97
final typedItem = itemToUpdate as Headline ; // Cast to specific type
97
98
// Validate ID consistency
@@ -105,8 +106,9 @@ Future<Response> onRequest(RequestContext context, String id) async {
105
106
final updatedItem = await repo.update (id, typedItem);
106
107
updatedJson = updatedItem.toJson ();
107
108
} // End block scope
108
- case 'category' :
109
- { // Added block scope
109
+ case 'category' :
110
+ {
111
+ // Added block scope
110
112
final repo = context.read <HtDataRepository <Category >>();
111
113
final typedItem = itemToUpdate as Category ; // Cast to specific type
112
114
// Validate ID consistency
@@ -120,8 +122,9 @@ Future<Response> onRequest(RequestContext context, String id) async {
120
122
final updatedItem = await repo.update (id, typedItem);
121
123
updatedJson = updatedItem.toJson ();
122
124
} // End block scope
123
- case 'source' :
124
- { // Added block scope
125
+ case 'source' :
126
+ {
127
+ // Added block scope
125
128
final repo = context.read <HtDataRepository <Source >>();
126
129
final typedItem = itemToUpdate as Source ; // Cast to specific type
127
130
// Validate ID consistency
@@ -135,8 +138,9 @@ Future<Response> onRequest(RequestContext context, String id) async {
135
138
final updatedItem = await repo.update (id, typedItem);
136
139
updatedJson = updatedItem.toJson ();
137
140
} // End block scope
138
- case 'country' :
139
- { // Added block scope
141
+ case 'country' :
142
+ {
143
+ // Added block scope
140
144
final repo = context.read <HtDataRepository <Country >>();
141
145
final typedItem = itemToUpdate as Country ; // Cast to specific type
142
146
// Validate ID consistency
@@ -150,14 +154,14 @@ Future<Response> onRequest(RequestContext context, String id) async {
150
154
final updatedItem = await repo.update (id, typedItem);
151
155
updatedJson = updatedItem.toJson ();
152
156
} // End block scope
153
- default :
154
- // This case should ideally be caught by middleware, but added for safety
155
- return Response (
156
- statusCode: HttpStatus .internalServerError,
157
- body:
158
- 'Internal Server Error: Unsupported model type "$modelName " reached handler.' ,
159
- );
160
- }
157
+ default :
158
+ // This case should ideally be caught by middleware, but added for safety
159
+ return Response (
160
+ statusCode: HttpStatus .internalServerError,
161
+ body:
162
+ 'Internal Server Error: Unsupported model type "$modelName " reached handler.' ,
163
+ );
164
+ }
161
165
// Return the serialized updated item
162
166
return Response .json (body: updatedJson);
163
167
}
@@ -168,21 +172,21 @@ Future<Response> onRequest(RequestContext context, String id) async {
168
172
// No serialization needed, just call delete based on type
169
173
switch (modelName) {
170
174
case 'headline' :
171
- await context.read <HtDataRepository <Headline >>().delete (id);
172
- case 'category' :
173
- await context.read <HtDataRepository <Category >>().delete (id);
174
- case 'source' :
175
- await context.read <HtDataRepository <Source >>().delete (id);
176
- case 'country' :
177
- await context.read <HtDataRepository <Country >>().delete (id);
178
- default :
179
- // This case should ideally be caught by middleware, but added for safety
180
- return Response (
181
- statusCode: HttpStatus .internalServerError,
182
- body:
183
- 'Internal Server Error: Unsupported model type "$modelName " reached handler.' ,
184
- );
185
- }
175
+ await context.read <HtDataRepository <Headline >>().delete (id);
176
+ case 'category' :
177
+ await context.read <HtDataRepository <Category >>().delete (id);
178
+ case 'source' :
179
+ await context.read <HtDataRepository <Source >>().delete (id);
180
+ case 'country' :
181
+ await context.read <HtDataRepository <Country >>().delete (id);
182
+ default :
183
+ // This case should ideally be caught by middleware, but added for safety
184
+ return Response (
185
+ statusCode: HttpStatus .internalServerError,
186
+ body:
187
+ 'Internal Server Error: Unsupported model type "$modelName " reached handler.' ,
188
+ );
189
+ }
186
190
// Return 204 No Content for successful deletion
187
191
return Response (statusCode: HttpStatus .noContent);
188
192
}
0 commit comments