@@ -7,8 +7,10 @@ import 'package:ht_api/src/rbac/permission_service.dart'; // Import PermissionSe
7
7
import 'package:ht_api/src/registry/model_registry.dart' ;
8
8
import 'package:ht_api/src/services/auth_service.dart' ;
9
9
import 'package:ht_api/src/services/auth_token_service.dart' ;
10
+ import 'package:ht_api/src/services/default_user_preference_limit_service.dart' ; // Import DefaultUserPreferenceLimitService
10
11
import 'package:ht_api/src/services/jwt_auth_token_service.dart' ;
11
12
import 'package:ht_api/src/services/token_blacklist_service.dart' ;
13
+ import 'package:ht_api/src/services/user_preference_limit_service.dart' ; // Import UserPreferenceLimitService interface
12
14
import 'package:ht_api/src/services/verification_code_storage_service.dart' ;
13
15
import 'package:ht_app_settings_client/ht_app_settings_client.dart' ;
14
16
import 'package:ht_app_settings_inmemory/ht_app_settings_inmemory.dart' ;
@@ -19,6 +21,7 @@ import 'package:ht_email_repository/ht_email_repository.dart';
19
21
import 'package:ht_shared/ht_shared.dart' ;
20
22
import 'package:uuid/uuid.dart' ;
21
23
24
+
22
25
// --- Request ID Wrapper ---
23
26
24
27
/// {@template request_id}
@@ -154,6 +157,47 @@ HtDataRepository<Country> _createCountryRepository() {
154
157
return HtDataRepository <Country >(dataClient: client);
155
158
}
156
159
160
+ // New repositories for user settings and preferences
161
+ HtDataRepository <UserAppSettings > _createUserAppSettingsRepository () {
162
+ print ('Initializing UserAppSettings Repository...' );
163
+ final client = HtDataInMemoryClient <UserAppSettings >(
164
+ toJson: (i) => i.toJson (),
165
+ getId: (i) => i.id,
166
+ // User settings are created on demand, no initial fixture needed
167
+ );
168
+ print ('UserAppSettings Repository Initialized.' );
169
+ return HtDataRepository <UserAppSettings >(dataClient: client);
170
+ }
171
+
172
+ HtDataRepository <UserContentPreferences >
173
+ _createUserContentPreferencesRepository () {
174
+ print ('Initializing UserContentPreferences Repository...' );
175
+ final client = HtDataInMemoryClient <UserContentPreferences >(
176
+ toJson: (i) => i.toJson (),
177
+ getId: (i) => i.id,
178
+ // User preferences are created on demand, no initial fixture needed
179
+ );
180
+ print ('UserContentPreferences Repository Initialized.' );
181
+ return HtDataRepository <UserContentPreferences >(dataClient: client);
182
+ }
183
+
184
+ HtDataRepository <AppConfig > _createAppConfigRepository () {
185
+ print ('Initializing AppConfig Repository...' );
186
+ // AppConfig should have a single instance, potentially loaded from a file
187
+ // or created with defaults if not found. For in-memory, we can create a
188
+ // default instance.
189
+ final initialData = [
190
+ const AppConfig (id: 'app_config' ), // Default config
191
+ ];
192
+ final client = HtDataInMemoryClient <AppConfig >(
193
+ toJson: (i) => i.toJson (),
194
+ getId: (i) => i.id,
195
+ initialData: initialData,
196
+ );
197
+ print ('AppConfig Repository Initialized.' );
198
+ return HtDataRepository <AppConfig >(dataClient: client);
199
+ }
200
+
157
201
// --- Middleware Definition ---
158
202
Handler middleware (Handler handler) {
159
203
// Initialize repositories when middleware is first created
@@ -162,6 +206,11 @@ Handler middleware(Handler handler) {
162
206
final categoryRepository = _createCategoryRepository ();
163
207
final sourceRepository = _createSourceRepository ();
164
208
final countryRepository = _createCountryRepository ();
209
+ final userSettingsRepository = _createUserAppSettingsRepository (); // New
210
+ final userContentPreferencesRepository =
211
+ _createUserContentPreferencesRepository (); // New
212
+ final appConfigRepository = _createAppConfigRepository (); // New
213
+
165
214
final settingsClientImpl = HtAppSettingsInMemory ();
166
215
const uuid = Uuid ();
167
216
@@ -208,6 +257,13 @@ Handler middleware(Handler handler) {
208
257
const permissionService =
209
258
PermissionService (); // Instantiate PermissionService
210
259
260
+ // --- User Preference Limit Service --- // New
261
+ final userPreferenceLimitService = DefaultUserPreferenceLimitService (
262
+ appConfigRepository: appConfigRepository,
263
+ userContentPreferencesRepository: userContentPreferencesRepository,
264
+ );
265
+ print ('[MiddlewareSetup] DefaultUserPreferenceLimitService instantiated.' );
266
+
211
267
// ==========================================================================
212
268
// MIDDLEWARE CHAIN
213
269
// ==========================================================================
@@ -263,6 +319,22 @@ Handler middleware(Handler handler) {
263
319
(_) => emailRepository,
264
320
),
265
321
) // Used by AuthService
322
+ // New Repositories for User Settings and Preferences
323
+ .use (
324
+ provider <HtDataRepository <UserAppSettings >>(
325
+ (_) => userSettingsRepository,
326
+ ),
327
+ )
328
+ .use (
329
+ provider <HtDataRepository <UserContentPreferences >>(
330
+ (_) => userContentPreferencesRepository,
331
+ ),
332
+ )
333
+ .use (
334
+ provider <HtDataRepository <AppConfig >>(
335
+ (_) => appConfigRepository,
336
+ ),
337
+ )
266
338
267
339
// --- 4. Authentication Service Providers (Auth Logic Dependencies) ---
268
340
// PURPOSE: Provide the core services needed for authentication logic.
@@ -301,15 +373,25 @@ Handler middleware(Handler handler) {
301
373
// (e.g., authorizationMiddleware).
302
374
.use (provider <PermissionService >((_) => permissionService))
303
375
304
- // --- 6. Request Logger (Logging) ---
376
+ // --- 6. User Preference Limit Service Provider --- // New
377
+ // PURPOSE: Provides the service for enforcing user preference limits.
378
+ // ORDER: Must be provided before any handlers that use it (specifically
379
+ // the generic data route handlers for UserContentPreferences).
380
+ .use (
381
+ provider <UserPreferenceLimitService >(
382
+ (_) => userPreferenceLimitService,
383
+ ),
384
+ )
385
+
386
+ // --- 7. Request Logger (Logging) ---
305
387
// PURPOSE: Logs details about the incoming request and outgoing response.
306
388
// ORDER: Often placed late in the request phase / early in the response
307
389
// phase. Placing it here logs the request *before* the handler
308
390
// runs and the response *after* the handler (and error handler)
309
391
// completes. Can access `RequestId` and potentially `User?`.
310
392
.use (requestLogger ())
311
393
312
- // --- 7 . Error Handler (Catch-All) ---
394
+ // --- 8 . Error Handler (Catch-All) ---
313
395
// PURPOSE: Catches exceptions thrown by upstream middleware or route
314
396
// handlers and converts them into standardized JSON error responses.
315
397
// ORDER: MUST be placed *late* in the chain (typically last before the
0 commit comments