@@ -24,10 +24,10 @@ Future<Response> onRequest(RequestContext context, String id) async {
24
24
// --- GET Request ---
25
25
if (context.request.method == HttpMethod .get ) {
26
26
Map <String , dynamic > itemJson;
27
- try {
28
- switch (modelName) {
29
- case 'headline' :
30
- final repo = context.read <HtDataRepository <Headline >>();
27
+ // Removed inner try-catch block to allow exceptions to propagate
28
+ switch (modelName) {
29
+ case 'headline' :
30
+ final repo = context.read <HtDataRepository <Headline >>();
31
31
final item = await repo.read (id);
32
32
// Serialize using the specific model's toJson method
33
33
itemJson = item.toJson ();
@@ -44,23 +44,13 @@ Future<Response> onRequest(RequestContext context, String id) async {
44
44
final item = await repo.read (id);
45
45
itemJson = item.toJson ();
46
46
default :
47
+ // This case should ideally be caught by middleware, but added for safety
47
48
return Response (
48
49
statusCode: HttpStatus .internalServerError,
49
50
body:
50
51
'Internal Server Error: Unsupported model type "$modelName " reached handler.' ,
51
52
);
52
53
}
53
- } catch (e) {
54
- // Catch potential provider errors during context.read
55
- print (
56
- 'Error reading repository provider for model "$modelName " in GET [id]: $e ' ,
57
- );
58
- return Response (
59
- statusCode: HttpStatus .internalServerError,
60
- body:
61
- 'Internal Server Error: Could not resolve repository for model "$modelName ".' ,
62
- );
63
- }
64
54
// Return the serialized item
65
55
return Response .json (body: itemJson);
66
56
}
@@ -78,65 +68,89 @@ Future<Response> onRequest(RequestContext context, String id) async {
78
68
// Deserialize using ModelConfig's fromJson
79
69
final itemToUpdate = modelConfig.fromJson (requestBody);
80
70
81
- // Validate ID consistency using ModelConfig's getId
82
- final incomingId = modelConfig.getId (itemToUpdate);
83
- if (incomingId != id) {
84
- return Response (
85
- statusCode: HttpStatus .badRequest,
86
- body:
87
- 'Bad Request: ID in request body ("$incomingId ") does not match ID in path ("$id ").' ,
88
- );
89
- }
71
+ // ID validation moved inside the switch block after type casting
90
72
91
73
Map <String , dynamic > updatedJson;
92
- try {
93
- switch (modelName) {
94
- case 'headline' :
74
+ // Removed inner try-catch block to allow exceptions to propagate
75
+ switch (modelName) {
76
+ case 'headline' :
77
+ { // Added block scope
95
78
final repo = context.read <HtDataRepository <Headline >>();
96
- // Cast itemToUpdate to the specific type expected by the repository's update method
97
- final updatedItem = await repo.update (id, itemToUpdate as Headline );
98
- // Serialize using the specific model's toJson method
79
+ final typedItem = itemToUpdate as Headline ; // Cast to specific type
80
+ // Validate ID consistency
81
+ if (typedItem.id != id) {
82
+ return Response (
83
+ statusCode: HttpStatus .badRequest,
84
+ body:
85
+ 'Bad Request: ID in request body ("${typedItem .id }") does not match ID in path ("$id ").' ,
86
+ );
87
+ }
88
+ final updatedItem = await repo.update (id, typedItem);
99
89
updatedJson = updatedItem.toJson ();
90
+ } // End block scope
100
91
case 'category' :
92
+ { // Added block scope
101
93
final repo = context.read <HtDataRepository <Category >>();
102
- final updatedItem = await repo.update (id, itemToUpdate as Category );
94
+ final typedItem = itemToUpdate as Category ; // Cast to specific type
95
+ // Validate ID consistency
96
+ if (typedItem.id != id) {
97
+ return Response (
98
+ statusCode: HttpStatus .badRequest,
99
+ body:
100
+ 'Bad Request: ID in request body ("${typedItem .id }") does not match ID in path ("$id ").' ,
101
+ );
102
+ }
103
+ final updatedItem = await repo.update (id, typedItem);
103
104
updatedJson = updatedItem.toJson ();
105
+ } // End block scope
104
106
case 'source' :
107
+ { // Added block scope
105
108
final repo = context.read <HtDataRepository <Source >>();
106
- final updatedItem = await repo.update (id, itemToUpdate as Source );
109
+ final typedItem = itemToUpdate as Source ; // Cast to specific type
110
+ // Validate ID consistency
111
+ if (typedItem.id != id) {
112
+ return Response (
113
+ statusCode: HttpStatus .badRequest,
114
+ body:
115
+ 'Bad Request: ID in request body ("${typedItem .id }") does not match ID in path ("$id ").' ,
116
+ );
117
+ }
118
+ final updatedItem = await repo.update (id, typedItem);
107
119
updatedJson = updatedItem.toJson ();
120
+ } // End block scope
108
121
case 'country' :
122
+ { // Added block scope
109
123
final repo = context.read <HtDataRepository <Country >>();
110
- final updatedItem = await repo.update (id, itemToUpdate as Country );
124
+ final typedItem = itemToUpdate as Country ; // Cast to specific type
125
+ // Validate ID consistency
126
+ if (typedItem.id != id) {
127
+ return Response (
128
+ statusCode: HttpStatus .badRequest,
129
+ body:
130
+ 'Bad Request: ID in request body ("${typedItem .id }") does not match ID in path ("$id ").' ,
131
+ );
132
+ }
133
+ final updatedItem = await repo.update (id, typedItem);
111
134
updatedJson = updatedItem.toJson ();
135
+ } // End block scope
112
136
default :
137
+ // This case should ideally be caught by middleware, but added for safety
113
138
return Response (
114
139
statusCode: HttpStatus .internalServerError,
115
140
body:
116
141
'Internal Server Error: Unsupported model type "$modelName " reached handler.' ,
117
142
);
118
143
}
119
- } catch (e) {
120
- // Catch potential provider errors during context.read
121
- print (
122
- 'Error reading repository provider for model "$modelName " in PUT [id]: $e ' ,
123
- );
124
- return Response (
125
- statusCode: HttpStatus .internalServerError,
126
- body:
127
- 'Internal Server Error: Could not resolve repository for model "$modelName ".' ,
128
- );
129
- }
130
144
// Return the serialized updated item
131
145
return Response .json (body: updatedJson);
132
146
}
133
147
134
148
// --- DELETE Request ---
135
149
if (context.request.method == HttpMethod .delete) {
136
- try {
137
- // No serialization needed, just call delete based on type
138
- switch (modelName) {
139
- case 'headline' :
150
+ // Removed inner try-catch block to allow exceptions to propagate
151
+ // No serialization needed, just call delete based on type
152
+ switch (modelName) {
153
+ case 'headline' :
140
154
await context.read <HtDataRepository <Headline >>().delete (id);
141
155
case 'category' :
142
156
await context.read <HtDataRepository <Category >>().delete (id);
@@ -145,23 +159,13 @@ Future<Response> onRequest(RequestContext context, String id) async {
145
159
case 'country' :
146
160
await context.read <HtDataRepository <Country >>().delete (id);
147
161
default :
162
+ // This case should ideally be caught by middleware, but added for safety
148
163
return Response (
149
164
statusCode: HttpStatus .internalServerError,
150
165
body:
151
166
'Internal Server Error: Unsupported model type "$modelName " reached handler.' ,
152
167
);
153
168
}
154
- } catch (e) {
155
- // Catch potential provider errors during context.read
156
- print (
157
- 'Error reading repository provider for model "$modelName " in DELETE [id]: $e ' ,
158
- );
159
- return Response (
160
- statusCode: HttpStatus .internalServerError,
161
- body:
162
- 'Internal Server Error: Could not resolve repository for model "$modelName ".' ,
163
- );
164
- }
165
169
// Return 204 No Content for successful deletion
166
170
return Response (statusCode: HttpStatus .noContent);
167
171
}
0 commit comments