-
Notifications
You must be signed in to change notification settings - Fork 0
Enhance data route #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c2e5964
feat(registry): implement data operation registry for CRUD functions
fulleni 5da4e11
refactor(middlewares): replace switch statement with registry in data…
fulleni 50a0e2b
feat(backend): add data operation registry to middleware
fulleni 7456647
refactor(api): register data operations for efficient processing
fulleni 480f7e6
refactor(data): implement data operation registry for create and read…
fulleni aae6957
style: format misc
fulleni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
import 'package:core/core.dart'; | ||
import 'package:dart_frog/dart_frog.dart'; | ||
import 'package:data_repository/data_repository.dart'; | ||
import 'package:flutter_news_app_api_server_full_source_code/src/middlewares/ownership_check_middleware.dart'; | ||
import 'package:flutter_news_app_api_server_full_source_code/src/services/dashboard_summary_service.dart'; | ||
|
||
// --- Typedefs for Data Operations --- | ||
|
||
/// A function that fetches a single item by its ID. | ||
typedef ItemFetcher = | ||
Future<dynamic> Function(RequestContext context, String id); | ||
|
||
/// A function that fetches a paginated list of items. | ||
typedef AllItemsReader = | ||
Future<PaginatedResponse<dynamic>> Function( | ||
RequestContext context, | ||
String? userId, | ||
Map<String, dynamic>? filter, | ||
List<SortOption>? sort, | ||
PaginationOptions? pagination, | ||
); | ||
|
||
/// A function that creates a new item. | ||
typedef ItemCreator = | ||
Future<dynamic> Function( | ||
RequestContext context, | ||
dynamic item, | ||
String? userId, | ||
); | ||
|
||
/// A function that updates an existing item. | ||
typedef ItemUpdater = | ||
Future<dynamic> Function( | ||
RequestContext context, | ||
String id, | ||
dynamic item, | ||
String? userId, | ||
); | ||
|
||
/// A function that deletes an item by its ID. | ||
typedef ItemDeleter = | ||
Future<void> Function(RequestContext context, String id, String? userId); | ||
|
||
/// {@template data_operation_registry} | ||
/// A centralized registry for all data handling functions (CRUD operations). | ||
/// | ||
/// This class uses a map-based strategy to associate model names (strings) | ||
/// with their corresponding data operation functions. This approach avoids | ||
/// large, repetitive `switch` statements in the route handlers, making the | ||
/// code more maintainable, scalable, and easier to read. | ||
/// | ||
/// By centralizing these mappings, we create a single source of truth for how | ||
/// data operations are performed for each model, improving consistency across | ||
/// the API. | ||
/// {@endtemplate} | ||
class DataOperationRegistry { | ||
/// {@macro data_operation_registry} | ||
DataOperationRegistry() { | ||
_registerOperations(); | ||
} | ||
|
||
// --- Private Maps to Hold Operation Functions --- | ||
|
||
final Map<String, ItemFetcher> _itemFetchers = {}; | ||
final Map<String, AllItemsReader> _allItemsReaders = {}; | ||
final Map<String, ItemCreator> _itemCreators = {}; | ||
final Map<String, ItemUpdater> _itemUpdaters = {}; | ||
final Map<String, ItemDeleter> _itemDeleters = {}; | ||
|
||
// --- Public Getters to Access Operation Maps --- | ||
|
||
/// Provides access to the map of item fetcher functions. | ||
Map<String, ItemFetcher> get itemFetchers => _itemFetchers; | ||
|
||
/// Provides access to the map of collection reader functions. | ||
Map<String, AllItemsReader> get allItemsReaders => _allItemsReaders; | ||
|
||
/// Provides access to the map of item creator functions. | ||
Map<String, ItemCreator> get itemCreators => _itemCreators; | ||
|
||
/// Provides access to the map of item updater functions. | ||
Map<String, ItemUpdater> get itemUpdaters => _itemUpdaters; | ||
|
||
/// Provides access to the map of item deleter functions. | ||
Map<String, ItemDeleter> get itemDeleters => _itemDeleters; | ||
|
||
/// Populates the operation maps with their respective functions. | ||
void _registerOperations() { | ||
// --- Register Item Fetchers --- | ||
_itemFetchers.addAll({ | ||
'headline': (c, id) => | ||
c.read<DataRepository<Headline>>().read(id: id, userId: null), | ||
'topic': (c, id) => | ||
c.read<DataRepository<Topic>>().read(id: id, userId: null), | ||
'source': (c, id) => | ||
c.read<DataRepository<Source>>().read(id: id, userId: null), | ||
'country': (c, id) => | ||
c.read<DataRepository<Country>>().read(id: id, userId: null), | ||
'language': (c, id) => | ||
c.read<DataRepository<Language>>().read(id: id, userId: null), | ||
'user': (c, id) => | ||
c.read<DataRepository<User>>().read(id: id, userId: null), | ||
'user_app_settings': (c, id) => | ||
c.read<DataRepository<UserAppSettings>>().read(id: id, userId: null), | ||
'user_content_preferences': (c, id) => c | ||
.read<DataRepository<UserContentPreferences>>() | ||
.read(id: id, userId: null), | ||
'remote_config': (c, id) => | ||
c.read<DataRepository<RemoteConfig>>().read(id: id, userId: null), | ||
'dashboard_summary': (c, id) => | ||
c.read<DashboardSummaryService>().getSummary(), | ||
}); | ||
|
||
// --- Register "Read All" Readers --- | ||
_allItemsReaders.addAll({ | ||
'headline': (c, uid, f, s, p) => c | ||
.read<DataRepository<Headline>>() | ||
.readAll(userId: uid, filter: f, sort: s, pagination: p), | ||
'topic': (c, uid, f, s, p) => c.read<DataRepository<Topic>>().readAll( | ||
userId: uid, | ||
filter: f, | ||
sort: s, | ||
pagination: p, | ||
), | ||
'source': (c, uid, f, s, p) => c.read<DataRepository<Source>>().readAll( | ||
userId: uid, | ||
filter: f, | ||
sort: s, | ||
pagination: p, | ||
), | ||
'country': (c, uid, f, s, p) => c.read<DataRepository<Country>>().readAll( | ||
userId: uid, | ||
filter: f, | ||
sort: s, | ||
pagination: p, | ||
), | ||
'language': (c, uid, f, s, p) => c | ||
.read<DataRepository<Language>>() | ||
.readAll(userId: uid, filter: f, sort: s, pagination: p), | ||
'user': (c, uid, f, s, p) => c.read<DataRepository<User>>().readAll( | ||
userId: uid, | ||
filter: f, | ||
sort: s, | ||
pagination: p, | ||
), | ||
}); | ||
|
||
// --- Register Item Creators --- | ||
_itemCreators.addAll({ | ||
'headline': (c, item, uid) => c.read<DataRepository<Headline>>().create( | ||
item: item as Headline, | ||
userId: uid, | ||
), | ||
'topic': (c, item, uid) => c.read<DataRepository<Topic>>().create( | ||
item: item as Topic, | ||
userId: uid, | ||
), | ||
'source': (c, item, uid) => c.read<DataRepository<Source>>().create( | ||
item: item as Source, | ||
userId: uid, | ||
), | ||
'country': (c, item, uid) => c.read<DataRepository<Country>>().create( | ||
item: item as Country, | ||
userId: uid, | ||
), | ||
'language': (c, item, uid) => c.read<DataRepository<Language>>().create( | ||
item: item as Language, | ||
userId: uid, | ||
), | ||
'remote_config': (c, item, uid) => c | ||
.read<DataRepository<RemoteConfig>>() | ||
.create(item: item as RemoteConfig, userId: uid), | ||
}); | ||
|
||
// --- Register Item Updaters --- | ||
_itemUpdaters.addAll({ | ||
'headline': (c, id, item, uid) => c | ||
.read<DataRepository<Headline>>() | ||
.update(id: id, item: item as Headline, userId: uid), | ||
'topic': (c, id, item, uid) => c.read<DataRepository<Topic>>().update( | ||
id: id, | ||
item: item as Topic, | ||
userId: uid, | ||
), | ||
'source': (c, id, item, uid) => c.read<DataRepository<Source>>().update( | ||
id: id, | ||
item: item as Source, | ||
userId: uid, | ||
), | ||
'country': (c, id, item, uid) => c.read<DataRepository<Country>>().update( | ||
id: id, | ||
item: item as Country, | ||
userId: uid, | ||
), | ||
'language': (c, id, item, uid) => c | ||
.read<DataRepository<Language>>() | ||
.update(id: id, item: item as Language, userId: uid), | ||
'user': (c, id, item, uid) { | ||
final repo = c.read<DataRepository<User>>(); | ||
final existingUser = c.read<FetchedItem<dynamic>>().data as User; | ||
final updatedUser = existingUser.copyWith( | ||
feedActionStatus: (item as User).feedActionStatus, | ||
); | ||
return repo.update(id: id, item: updatedUser, userId: uid); | ||
}, | ||
'user_app_settings': (c, id, item, uid) => c | ||
.read<DataRepository<UserAppSettings>>() | ||
.update(id: id, item: item as UserAppSettings, userId: uid), | ||
'user_content_preferences': (c, id, item, uid) => c | ||
.read<DataRepository<UserContentPreferences>>() | ||
.update(id: id, item: item as UserContentPreferences, userId: uid), | ||
'remote_config': (c, id, item, uid) => c | ||
.read<DataRepository<RemoteConfig>>() | ||
.update(id: id, item: item as RemoteConfig, userId: uid), | ||
}); | ||
|
||
// --- Register Item Deleters --- | ||
_itemDeleters.addAll({ | ||
'headline': (c, id, uid) => | ||
c.read<DataRepository<Headline>>().delete(id: id, userId: uid), | ||
'topic': (c, id, uid) => | ||
c.read<DataRepository<Topic>>().delete(id: id, userId: uid), | ||
'source': (c, id, uid) => | ||
c.read<DataRepository<Source>>().delete(id: id, userId: uid), | ||
'country': (c, id, uid) => | ||
c.read<DataRepository<Country>>().delete(id: id, userId: uid), | ||
'language': (c, id, uid) => | ||
c.read<DataRepository<Language>>().delete(id: id, userId: uid), | ||
'user': (c, id, uid) => | ||
c.read<DataRepository<User>>().delete(id: id, userId: uid), | ||
'user_app_settings': (c, id, uid) => | ||
c.read<DataRepository<UserAppSettings>>().delete(id: id, userId: uid), | ||
'user_content_preferences': (c, id, uid) => c | ||
.read<DataRepository<UserContentPreferences>>() | ||
.delete(id: id, userId: uid), | ||
'remote_config': (c, id, uid) => | ||
c.read<DataRepository<RemoteConfig>>().delete(id: id, userId: uid), | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.