@@ -6,6 +6,7 @@ import 'package:dart_frog/dart_frog.dart';
66import 'package:data_repository/data_repository.dart' ;
77import 'package:flutter_news_app_api_server_full_source_code/src/helpers/response_helper.dart' ;
88import 'package:flutter_news_app_api_server_full_source_code/src/rbac/permission_service.dart' ;
9+ import 'package:flutter_news_app_api_server_full_source_code/src/registry/data_operation_registry.dart' ;
910import 'package:flutter_news_app_api_server_full_source_code/src/registry/model_registry.dart' ;
1011import 'package:logging/logging.dart' ;
1112import 'package:mongo_dart/mongo_dart.dart' ;
@@ -165,55 +166,16 @@ Future<PaginatedResponse<dynamic>> _readAllItems(
165166 'Executing _readAllItems for model "$modelName ", userId: $userId .' ,
166167 );
167168 try {
168- switch (modelName) {
169- case 'headline' :
170- return await context.read <DataRepository <Headline >>().readAll (
171- userId: userId,
172- filter: filter,
173- sort: sort,
174- pagination: pagination,
175- );
176- case 'topic' :
177- return await context.read <DataRepository <Topic >>().readAll (
178- userId: userId,
179- filter: filter,
180- sort: sort,
181- pagination: pagination,
182- );
183- case 'source' :
184- return await context.read <DataRepository <Source >>().readAll (
185- userId: userId,
186- filter: filter,
187- sort: sort,
188- pagination: pagination,
189- );
190- case 'country' :
191- return await context.read <DataRepository <Country >>().readAll (
192- userId: userId,
193- filter: filter,
194- sort: sort,
195- pagination: pagination,
196- );
197- case 'language' :
198- return await context.read <DataRepository <Language >>().readAll (
199- userId: userId,
200- filter: filter,
201- sort: sort,
202- pagination: pagination,
203- );
204- case 'user' :
205- return await context.read <DataRepository <User >>().readAll (
206- userId: userId,
207- filter: filter,
208- sort: sort,
209- pagination: pagination,
210- );
211- default :
212- _logger.warning ('Unsupported model type "$modelName " for GET all.' );
213- throw OperationFailedException (
214- 'Unsupported model type "$modelName " for GET all.' ,
215- );
169+ final registry = context.read <DataOperationRegistry >();
170+ final reader = registry.allItemsReaders[modelName];
171+
172+ if (reader == null ) {
173+ _logger.warning ('Unsupported model type "$modelName " for GET all.' );
174+ throw OperationFailedException (
175+ 'Unsupported model type "$modelName " for GET all.' ,
176+ );
216177 }
178+ return await reader (context, userId, filter, sort, pagination);
217179 } catch (e, s) {
218180 _logger.severe (
219181 'Unhandled exception in _readAllItems for model "$modelName ".' ,
@@ -237,43 +199,16 @@ Future<dynamic> _createItem(
237199 'Executing _createItem for model "$modelName ", userId: $userId .' ,
238200 );
239201 try {
240- switch (modelName) {
241- case 'headline' :
242- return await context.read <DataRepository <Headline >>().create (
243- item: itemToCreate as Headline ,
244- userId: userId,
245- );
246- case 'topic' :
247- return await context.read <DataRepository <Topic >>().create (
248- item: itemToCreate as Topic ,
249- userId: userId,
250- );
251- case 'source' :
252- return await context.read <DataRepository <Source >>().create (
253- item: itemToCreate as Source ,
254- userId: userId,
255- );
256- case 'country' :
257- return await context.read <DataRepository <Country >>().create (
258- item: itemToCreate as Country ,
259- userId: userId,
260- );
261- case 'language' :
262- return await context.read <DataRepository <Language >>().create (
263- item: itemToCreate as Language ,
264- userId: userId,
265- );
266- case 'remote_config' :
267- return await context.read <DataRepository <RemoteConfig >>().create (
268- item: itemToCreate as RemoteConfig ,
269- userId: userId,
270- );
271- default :
272- _logger.warning ('Unsupported model type "$modelName " for POST.' );
273- throw OperationFailedException (
274- 'Unsupported model type "$modelName " for POST.' ,
275- );
202+ final registry = context.read <DataOperationRegistry >();
203+ final creator = registry.itemCreators[modelName];
204+
205+ if (creator == null ) {
206+ _logger.warning ('Unsupported model type "$modelName " for POST.' );
207+ throw OperationFailedException (
208+ 'Unsupported model type "$modelName " for POST.' ,
209+ );
276210 }
211+ return await creator (context, itemToCreate, userId);
277212 } catch (e, s) {
278213 _logger.severe (
279214 'Unhandled exception in _createItem for model "$modelName ".' ,
0 commit comments