|
| 1 | +import 'package:core/core.dart'; |
| 2 | +import 'package:dart_frog/dart_frog.dart'; |
| 3 | +import 'package:data_repository/data_repository.dart'; |
| 4 | +import 'package:flutter_news_app_api_server_full_source_code/src/middlewares/ownership_check_middleware.dart'; |
| 5 | +import 'package:flutter_news_app_api_server_full_source_code/src/services/dashboard_summary_service.dart'; |
| 6 | + |
| 7 | +// --- Typedefs for Data Operations --- |
| 8 | + |
| 9 | +/// A function that fetches a single item by its ID. |
| 10 | +typedef ItemFetcher = |
| 11 | + Future<dynamic> Function(RequestContext context, String id); |
| 12 | + |
| 13 | +/// A function that fetches a paginated list of items. |
| 14 | +typedef AllItemsReader = |
| 15 | + Future<PaginatedResponse<dynamic>> Function( |
| 16 | + RequestContext context, |
| 17 | + String? userId, |
| 18 | + Map<String, dynamic>? filter, |
| 19 | + List<SortOption>? sort, |
| 20 | + PaginationOptions? pagination, |
| 21 | + ); |
| 22 | + |
| 23 | +/// A function that creates a new item. |
| 24 | +typedef ItemCreator = |
| 25 | + Future<dynamic> Function( |
| 26 | + RequestContext context, |
| 27 | + dynamic item, |
| 28 | + String? userId, |
| 29 | + ); |
| 30 | + |
| 31 | +/// A function that updates an existing item. |
| 32 | +typedef ItemUpdater = |
| 33 | + Future<dynamic> Function( |
| 34 | + RequestContext context, |
| 35 | + String id, |
| 36 | + dynamic item, |
| 37 | + String? userId, |
| 38 | + ); |
| 39 | + |
| 40 | +/// A function that deletes an item by its ID. |
| 41 | +typedef ItemDeleter = |
| 42 | + Future<void> Function(RequestContext context, String id, String? userId); |
| 43 | + |
| 44 | +/// {@template data_operation_registry} |
| 45 | +/// A centralized registry for all data handling functions (CRUD operations). |
| 46 | +/// |
| 47 | +/// This class uses a map-based strategy to associate model names (strings) |
| 48 | +/// with their corresponding data operation functions. This approach avoids |
| 49 | +/// large, repetitive `switch` statements in the route handlers, making the |
| 50 | +/// code more maintainable, scalable, and easier to read. |
| 51 | +/// |
| 52 | +/// By centralizing these mappings, we create a single source of truth for how |
| 53 | +/// data operations are performed for each model, improving consistency across |
| 54 | +/// the API. |
| 55 | +/// {@endtemplate} |
| 56 | +class DataOperationRegistry { |
| 57 | + /// {@macro data_operation_registry} |
| 58 | + DataOperationRegistry() { |
| 59 | + _registerOperations(); |
| 60 | + } |
| 61 | + |
| 62 | + // --- Private Maps to Hold Operation Functions --- |
| 63 | + |
| 64 | + final Map<String, ItemFetcher> _itemFetchers = {}; |
| 65 | + final Map<String, AllItemsReader> _allItemsReaders = {}; |
| 66 | + final Map<String, ItemCreator> _itemCreators = {}; |
| 67 | + final Map<String, ItemUpdater> _itemUpdaters = {}; |
| 68 | + final Map<String, ItemDeleter> _itemDeleters = {}; |
| 69 | + |
| 70 | + // --- Public Getters to Access Operation Maps --- |
| 71 | + |
| 72 | + /// Provides access to the map of item fetcher functions. |
| 73 | + Map<String, ItemFetcher> get itemFetchers => _itemFetchers; |
| 74 | + |
| 75 | + /// Provides access to the map of collection reader functions. |
| 76 | + Map<String, AllItemsReader> get allItemsReaders => _allItemsReaders; |
| 77 | + |
| 78 | + /// Provides access to the map of item creator functions. |
| 79 | + Map<String, ItemCreator> get itemCreators => _itemCreators; |
| 80 | + |
| 81 | + /// Provides access to the map of item updater functions. |
| 82 | + Map<String, ItemUpdater> get itemUpdaters => _itemUpdaters; |
| 83 | + |
| 84 | + /// Provides access to the map of item deleter functions. |
| 85 | + Map<String, ItemDeleter> get itemDeleters => _itemDeleters; |
| 86 | + |
| 87 | + /// Populates the operation maps with their respective functions. |
| 88 | + void _registerOperations() { |
| 89 | + // --- Register Item Fetchers --- |
| 90 | + _itemFetchers.addAll({ |
| 91 | + 'headline': (c, id) => |
| 92 | + c.read<DataRepository<Headline>>().read(id: id, userId: null), |
| 93 | + 'topic': (c, id) => |
| 94 | + c.read<DataRepository<Topic>>().read(id: id, userId: null), |
| 95 | + 'source': (c, id) => |
| 96 | + c.read<DataRepository<Source>>().read(id: id, userId: null), |
| 97 | + 'country': (c, id) => |
| 98 | + c.read<DataRepository<Country>>().read(id: id, userId: null), |
| 99 | + 'language': (c, id) => |
| 100 | + c.read<DataRepository<Language>>().read(id: id, userId: null), |
| 101 | + 'user': (c, id) => |
| 102 | + c.read<DataRepository<User>>().read(id: id, userId: null), |
| 103 | + 'user_app_settings': (c, id) => |
| 104 | + c.read<DataRepository<UserAppSettings>>().read(id: id, userId: null), |
| 105 | + 'user_content_preferences': (c, id) => c |
| 106 | + .read<DataRepository<UserContentPreferences>>() |
| 107 | + .read(id: id, userId: null), |
| 108 | + 'remote_config': (c, id) => |
| 109 | + c.read<DataRepository<RemoteConfig>>().read(id: id, userId: null), |
| 110 | + 'dashboard_summary': (c, id) => |
| 111 | + c.read<DashboardSummaryService>().getSummary(), |
| 112 | + }); |
| 113 | + |
| 114 | + // --- Register "Read All" Readers --- |
| 115 | + _allItemsReaders.addAll({ |
| 116 | + 'headline': (c, uid, f, s, p) => c |
| 117 | + .read<DataRepository<Headline>>() |
| 118 | + .readAll(userId: uid, filter: f, sort: s, pagination: p), |
| 119 | + 'topic': (c, uid, f, s, p) => c.read<DataRepository<Topic>>().readAll( |
| 120 | + userId: uid, |
| 121 | + filter: f, |
| 122 | + sort: s, |
| 123 | + pagination: p, |
| 124 | + ), |
| 125 | + 'source': (c, uid, f, s, p) => c.read<DataRepository<Source>>().readAll( |
| 126 | + userId: uid, |
| 127 | + filter: f, |
| 128 | + sort: s, |
| 129 | + pagination: p, |
| 130 | + ), |
| 131 | + 'country': (c, uid, f, s, p) => c.read<DataRepository<Country>>().readAll( |
| 132 | + userId: uid, |
| 133 | + filter: f, |
| 134 | + sort: s, |
| 135 | + pagination: p, |
| 136 | + ), |
| 137 | + 'language': (c, uid, f, s, p) => c |
| 138 | + .read<DataRepository<Language>>() |
| 139 | + .readAll(userId: uid, filter: f, sort: s, pagination: p), |
| 140 | + 'user': (c, uid, f, s, p) => c.read<DataRepository<User>>().readAll( |
| 141 | + userId: uid, |
| 142 | + filter: f, |
| 143 | + sort: s, |
| 144 | + pagination: p, |
| 145 | + ), |
| 146 | + }); |
| 147 | + |
| 148 | + // --- Register Item Creators --- |
| 149 | + _itemCreators.addAll({ |
| 150 | + 'headline': (c, item, uid) => c.read<DataRepository<Headline>>().create( |
| 151 | + item: item as Headline, |
| 152 | + userId: uid, |
| 153 | + ), |
| 154 | + 'topic': (c, item, uid) => c.read<DataRepository<Topic>>().create( |
| 155 | + item: item as Topic, |
| 156 | + userId: uid, |
| 157 | + ), |
| 158 | + 'source': (c, item, uid) => c.read<DataRepository<Source>>().create( |
| 159 | + item: item as Source, |
| 160 | + userId: uid, |
| 161 | + ), |
| 162 | + 'country': (c, item, uid) => c.read<DataRepository<Country>>().create( |
| 163 | + item: item as Country, |
| 164 | + userId: uid, |
| 165 | + ), |
| 166 | + 'language': (c, item, uid) => c.read<DataRepository<Language>>().create( |
| 167 | + item: item as Language, |
| 168 | + userId: uid, |
| 169 | + ), |
| 170 | + 'remote_config': (c, item, uid) => c |
| 171 | + .read<DataRepository<RemoteConfig>>() |
| 172 | + .create(item: item as RemoteConfig, userId: uid), |
| 173 | + }); |
| 174 | + |
| 175 | + // --- Register Item Updaters --- |
| 176 | + _itemUpdaters.addAll({ |
| 177 | + 'headline': (c, id, item, uid) => c |
| 178 | + .read<DataRepository<Headline>>() |
| 179 | + .update(id: id, item: item as Headline, userId: uid), |
| 180 | + 'topic': (c, id, item, uid) => c.read<DataRepository<Topic>>().update( |
| 181 | + id: id, |
| 182 | + item: item as Topic, |
| 183 | + userId: uid, |
| 184 | + ), |
| 185 | + 'source': (c, id, item, uid) => c.read<DataRepository<Source>>().update( |
| 186 | + id: id, |
| 187 | + item: item as Source, |
| 188 | + userId: uid, |
| 189 | + ), |
| 190 | + 'country': (c, id, item, uid) => c.read<DataRepository<Country>>().update( |
| 191 | + id: id, |
| 192 | + item: item as Country, |
| 193 | + userId: uid, |
| 194 | + ), |
| 195 | + 'language': (c, id, item, uid) => c |
| 196 | + .read<DataRepository<Language>>() |
| 197 | + .update(id: id, item: item as Language, userId: uid), |
| 198 | + 'user': (c, id, item, uid) { |
| 199 | + final repo = c.read<DataRepository<User>>(); |
| 200 | + final existingUser = c.read<FetchedItem<dynamic>>().data as User; |
| 201 | + final updatedUser = existingUser.copyWith( |
| 202 | + feedActionStatus: (item as User).feedActionStatus, |
| 203 | + ); |
| 204 | + return repo.update(id: id, item: updatedUser, userId: uid); |
| 205 | + }, |
| 206 | + 'user_app_settings': (c, id, item, uid) => c |
| 207 | + .read<DataRepository<UserAppSettings>>() |
| 208 | + .update(id: id, item: item as UserAppSettings, userId: uid), |
| 209 | + 'user_content_preferences': (c, id, item, uid) => c |
| 210 | + .read<DataRepository<UserContentPreferences>>() |
| 211 | + .update(id: id, item: item as UserContentPreferences, userId: uid), |
| 212 | + 'remote_config': (c, id, item, uid) => c |
| 213 | + .read<DataRepository<RemoteConfig>>() |
| 214 | + .update(id: id, item: item as RemoteConfig, userId: uid), |
| 215 | + }); |
| 216 | + |
| 217 | + // --- Register Item Deleters --- |
| 218 | + _itemDeleters.addAll({ |
| 219 | + 'headline': (c, id, uid) => |
| 220 | + c.read<DataRepository<Headline>>().delete(id: id, userId: uid), |
| 221 | + 'topic': (c, id, uid) => |
| 222 | + c.read<DataRepository<Topic>>().delete(id: id, userId: uid), |
| 223 | + 'source': (c, id, uid) => |
| 224 | + c.read<DataRepository<Source>>().delete(id: id, userId: uid), |
| 225 | + 'country': (c, id, uid) => |
| 226 | + c.read<DataRepository<Country>>().delete(id: id, userId: uid), |
| 227 | + 'language': (c, id, uid) => |
| 228 | + c.read<DataRepository<Language>>().delete(id: id, userId: uid), |
| 229 | + 'user': (c, id, uid) => |
| 230 | + c.read<DataRepository<User>>().delete(id: id, userId: uid), |
| 231 | + 'user_app_settings': (c, id, uid) => |
| 232 | + c.read<DataRepository<UserAppSettings>>().delete(id: id, userId: uid), |
| 233 | + 'user_content_preferences': (c, id, uid) => c |
| 234 | + .read<DataRepository<UserContentPreferences>>() |
| 235 | + .delete(id: id, userId: uid), |
| 236 | + 'remote_config': (c, id, uid) => |
| 237 | + c.read<DataRepository<RemoteConfig>>().delete(id: id, userId: uid), |
| 238 | + }); |
| 239 | + } |
| 240 | +} |
0 commit comments