Skip to content

Commit abad073

Browse files
authored
Merge pull request #55 from flutter-news-app-full-source-code/Integrate-LocalAd-Polymorphic-Collection-into-teh-data-route
Integrate local ad polymorphic collection into teh data route
2 parents 60fb335 + ea11a39 commit abad073

File tree

7 files changed

+81
-0
lines changed

7 files changed

+81
-0
lines changed

lib/src/config/app_dependencies.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class AppDependencies {
5959
late final DataRepository<UserContentPreferences>
6060
userContentPreferencesRepository;
6161
late final DataRepository<RemoteConfig> remoteConfigRepository;
62+
late final DataRepository<LocalAd> localAdRepository;
6263
late final EmailRepository emailRepository;
6364

6465
// Services
@@ -201,6 +202,16 @@ class AppDependencies {
201202

202203
emailRepository = EmailRepository(emailClient: emailClient);
203204

205+
final localAdClient = DataMongodb<LocalAd>(
206+
connectionManager: _mongoDbConnectionManager,
207+
modelName: 'local_ads',
208+
fromJson: LocalAd.fromJson,
209+
toJson: LocalAd.toJson,
210+
searchableFields: ['title'],
211+
logger: Logger('DataMongodb<LocalAd>'),
212+
);
213+
localAdRepository = DataRepository(dataClient: localAdClient);
214+
204215
// 5. Initialize Services
205216
tokenBlacklistService = MongoDbTokenBlacklistService(
206217
connectionManager: _mongoDbConnectionManager,

lib/src/rbac/permissions.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ abstract class Permissions {
6868
static const String userPreferenceBypassLimits =
6969
'user_preference.bypass_limits';
7070

71+
// Local Ad Permissions
72+
static const String localAdCreate = 'local_ad.create';
73+
static const String localAdRead = 'local_ad.read';
74+
static const String localAdUpdate = 'local_ad.update';
75+
static const String localAdDelete = 'local_ad.delete';
76+
7177
// General System Permissions
7278
static const String rateLimitingBypass = 'rate_limiting.bypass';
7379
}

lib/src/rbac/role_permissions.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ final Set<String> _appGuestUserPermissions = {
1414
Permissions.userContentPreferencesReadOwned,
1515
Permissions.userContentPreferencesUpdateOwned,
1616
Permissions.remoteConfigRead,
17+
Permissions.localAdRead,
1718
// Allows a user to update their own User object. This is essential for
1819
// features like updating the `feedActionStatus` (e.g., when a user
1920
// dismisses an in-feed prompt, etc). The endpoint handler ensures only
@@ -72,6 +73,11 @@ final Set<String> _dashboardAdminPermissions = {
7273
Permissions.remoteConfigUpdate,
7374
Permissions.remoteConfigDelete,
7475
Permissions.userPreferenceBypassLimits,
76+
// Added localAd CRUD permissions for admins
77+
Permissions.localAdCreate,
78+
Permissions.localAdRead,
79+
Permissions.localAdUpdate,
80+
Permissions.localAdDelete,
7581
};
7682

7783
/// Defines the mapping between user roles (both app and dashboard) and the

lib/src/registry/data_operation_registry.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ class DataOperationRegistry {
108108
.read(id: id, userId: null),
109109
'remote_config': (c, id) =>
110110
c.read<DataRepository<RemoteConfig>>().read(id: id, userId: null),
111+
'local_ad': (c, id) =>
112+
c.read<DataRepository<LocalAd>>().read(id: id, userId: null),
111113
'dashboard_summary': (c, id) =>
112114
c.read<DashboardSummaryService>().getSummary(),
113115
});
@@ -159,6 +161,9 @@ class DataOperationRegistry {
159161
sort: s,
160162
pagination: p,
161163
),
164+
'local_ad': (c, uid, f, s, p) => c
165+
.read<DataRepository<LocalAd>>()
166+
.readAll(userId: uid, filter: f, sort: s, pagination: p),
162167
});
163168

164169
// --- Register Item Creators ---
@@ -186,6 +191,10 @@ class DataOperationRegistry {
186191
'remote_config': (c, item, uid) => c
187192
.read<DataRepository<RemoteConfig>>()
188193
.create(item: item as RemoteConfig, userId: uid),
194+
'local_ad': (c, item, uid) => c.read<DataRepository<LocalAd>>().create(
195+
item: item as LocalAd,
196+
userId: uid,
197+
),
189198
});
190199

191200
// --- Register Item Updaters ---
@@ -228,6 +237,9 @@ class DataOperationRegistry {
228237
'remote_config': (c, id, item, uid) => c
229238
.read<DataRepository<RemoteConfig>>()
230239
.update(id: id, item: item as RemoteConfig, userId: uid),
240+
'local_ad': (c, id, item, uid) => c
241+
.read<DataRepository<LocalAd>>()
242+
.update(id: id, item: item as LocalAd, userId: uid),
231243
});
232244

233245
// --- Register Item Deleters ---
@@ -251,6 +263,8 @@ class DataOperationRegistry {
251263
.delete(id: id, userId: uid),
252264
'remote_config': (c, id, uid) =>
253265
c.read<DataRepository<RemoteConfig>>().delete(id: id, userId: uid),
266+
'local_ad': (c, id, uid) =>
267+
c.read<DataRepository<LocalAd>>().delete(id: id, userId: uid),
254268
});
255269
}
256270
}

lib/src/registry/model_registry.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,31 @@ final modelRegistry = <String, ModelConfig<dynamic>>{
362362
type: RequiredPermissionType.unsupported,
363363
),
364364
),
365+
'local_ad': ModelConfig<LocalAd>(
366+
fromJson: LocalAd.fromJson,
367+
getId: (ad) => (ad as dynamic).id as String, // Corrected to access id
368+
getOwnerId: null, // LocalAd is a global resource, not user-owned
369+
getCollectionPermission: const ModelActionPermission(
370+
type: RequiredPermissionType.specificPermission,
371+
permission: Permissions.localAdRead,
372+
),
373+
getItemPermission: const ModelActionPermission(
374+
type: RequiredPermissionType.specificPermission,
375+
permission: Permissions.localAdRead,
376+
),
377+
postPermission: const ModelActionPermission(
378+
type: RequiredPermissionType.adminOnly,
379+
permission: Permissions.localAdCreate,
380+
),
381+
putPermission: const ModelActionPermission(
382+
type: RequiredPermissionType.adminOnly,
383+
permission: Permissions.localAdUpdate,
384+
),
385+
deletePermission: const ModelActionPermission(
386+
type: RequiredPermissionType.adminOnly,
387+
permission: Permissions.localAdDelete,
388+
),
389+
),
365390
};
366391

367392
/// Type alias for the ModelRegistry map for easier provider usage.

lib/src/services/database_seeding_service.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class DatabaseSeedingService {
4545
getId: (item) => item.id,
4646
toJson: (item) => item.toJson(),
4747
);
48+
await _seedCollection<LocalAd>(
49+
collectionName: 'local_ads',
50+
fixtureData: localAdsFixturesData,
51+
getId: (item) => (item as dynamic).id as String,
52+
// ignore: unnecessary_lambdas
53+
toJson: (item) => LocalAd.toJson(item),
54+
);
4855

4956
_log.info('Database seeding process completed.');
5057
}
@@ -131,6 +138,13 @@ class DatabaseSeedingService {
131138
.collection('countries')
132139
.createIndex(keys: {'name': 1}, name: 'countries_name_index');
133140

141+
/// Index for searching local ads by adType.
142+
/// This index supports efficient queries and filtering on the 'adType' field
143+
/// of local ad documents.
144+
await _db
145+
.collection('local_ads')
146+
.createIndex(keys: {'adType': 1}, name: 'local_ads_adType_index');
147+
134148
// --- TTL and Unique Indexes via runCommand ---
135149
// The following indexes are created using the generic `runCommand` because
136150
// they require specific options not exposed by the simpler `createIndex`

routes/_middleware.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ Handler middleware(Handler handler) {
127127
(_) => deps.remoteConfigRepository,
128128
),
129129
)
130+
.use(
131+
provider<DataRepository<LocalAd>>(
132+
(_) => deps.localAdRepository,
133+
),
134+
)
130135
.use(provider<EmailRepository>((_) => deps.emailRepository))
131136
.use(
132137
provider<TokenBlacklistService>(

0 commit comments

Comments
 (0)