11import 'dart:convert' ;
22import 'package:ht_shared/ht_shared.dart' ;
3- import 'package:ht_shared/src/fixtures/fixtures.dart' ;
43import 'package:logging/logging.dart' ;
54import 'package:postgres/postgres.dart' ;
65
@@ -174,8 +173,7 @@ class DatabaseSeedingService {
174173 try {
175174 // Seed Topics
176175 _log.fine ('Seeding topics...' );
177- for (final data in topicsFixturesData) {
178- final topic = Topic .fromJson (data);
176+ for (final topic in topicsFixturesData) {
179177 final params = topic.toJson ()
180178 ..putIfAbsent ('description' , () => null )
181179 ..putIfAbsent ('icon_url' , () => null )
@@ -194,8 +192,7 @@ class DatabaseSeedingService {
194192
195193 // Seed Countries (must be done before sources and headlines)
196194 _log.fine ('Seeding countries...' );
197- for (final data in countriesFixturesData) {
198- final country = Country .fromJson (data);
195+ for (final country in countriesFixturesData) {
199196 final params = country.toJson ()
200197 ..putIfAbsent ('updated_at' , () => null );
201198
@@ -212,8 +209,7 @@ class DatabaseSeedingService {
212209
213210 // Seed Sources
214211 _log.fine ('Seeding sources...' );
215- for (final data in sourcesFixturesData) {
216- final source = Source .fromJson (data);
212+ for (final source in sourcesFixturesData) {
217213 final params = source.toJson ()
218214 // The `headquarters` field in the model is a nested `Country`
219215 // object. We extract its ID to store in the
@@ -243,8 +239,7 @@ class DatabaseSeedingService {
243239
244240 // Seed Headlines
245241 _log.fine ('Seeding headlines...' );
246- for (final data in headlinesFixturesData) {
247- final headline = Headline .fromJson (data);
242+ for (final headline in headlinesFixturesData) {
248243 final params = headline.toJson ()
249244 ..['source_id' ] = headline.source.id
250245 ..['topic_id' ] = headline.topic.id
@@ -296,7 +291,7 @@ class DatabaseSeedingService {
296291 try {
297292 // Seed RemoteConfig
298293 _log.fine ('Seeding RemoteConfig...' );
299- final remoteConfig = RemoteConfig . fromJson (remoteConfigFixtureData) ;
294+ final remoteConfig = remoteConfigFixture ;
300295 // The `remote_config` table has multiple JSONB columns. We must
301296 // provide an explicit map with JSON-encoded values to avoid a
302297 // "superfluous variables" error from the postgres driver.
@@ -322,11 +317,10 @@ class DatabaseSeedingService {
322317 // Seed Admin User
323318 _log.fine ('Seeding admin user...' );
324319 // Find the admin user in the fixture data.
325- final adminUserFixture = usersFixturesData.firstWhere (
326- (data ) => data[ 'dashboard_role' ] == DashboardUserRole .admin.name ,
320+ final adminUser = usersFixturesData.firstWhere (
321+ (user ) => user.dashboardRole == DashboardUserRole .admin,
327322 orElse: () => throw StateError ('Admin user not found in fixtures.' ),
328323 );
329- final adminUser = User .fromJson (adminUserFixture);
330324
331325 // The `users` table has specific columns for roles and status.
332326 await _connection.execute (
@@ -336,19 +330,20 @@ class DatabaseSeedingService {
336330 '@dashboard_role, @feed_action_status) '
337331 'ON CONFLICT (id) DO NOTHING' ,
338332 ),
339- parameters: adminUser.toJson ()
340- ..['feed_action_status' ] =
341- jsonEncode (adminUser.feedActionStatus.toJson ()),
333+ parameters: () {
334+ final params = adminUser.toJson ();
335+ params['feed_action_status' ] =
336+ jsonEncode (params['feed_action_status' ]);
337+ return params;
338+ }(),
342339 );
343340
344341 // Seed default settings and preferences for the admin user.
345- final adminSettings = UserAppSettings .fromJson (
346- userAppSettingsFixturesData
347- .firstWhere ((data) => data['id' ] == adminUser.id),
342+ final adminSettings = userAppSettingsFixturesData.firstWhere (
343+ (settings) => settings.id == adminUser.id,
348344 );
349- final adminPreferences = UserContentPreferences .fromJson (
350- userContentPreferencesFixturesData
351- .firstWhere ((data) => data['id' ] == adminUser.id),
345+ final adminPreferences = userContentPreferencesFixturesData.firstWhere (
346+ (prefs) => prefs.id == adminUser.id,
352347 );
353348
354349 await _connection.execute (
@@ -358,10 +353,13 @@ class DatabaseSeedingService {
358353 '@display_settings, @language, @feed_preferences) '
359354 'ON CONFLICT (id) DO NOTHING' ,
360355 ),
361- parameters: adminSettings.toJson ()
362- ..['user_id' ] = adminUser.id
363- ..['display_settings' ] = jsonEncode (adminSettings.displaySettings)
364- ..['feed_preferences' ] = jsonEncode (adminSettings.feedPreferences),
356+ parameters: () {
357+ final params = adminSettings.toJson ();
358+ params['user_id' ] = adminUser.id;
359+ params['display_settings' ] = jsonEncode (params['display_settings' ]);
360+ params['feed_preferences' ] = jsonEncode (params['feed_preferences' ]);
361+ return params;
362+ }(),
365363 );
366364
367365 await _connection.execute (
@@ -372,12 +370,15 @@ class DatabaseSeedingService {
372370 '@followed_sources, @followed_countries, @saved_headlines) '
373371 'ON CONFLICT (id) DO NOTHING' ,
374372 ),
375- parameters: adminPreferences.toJson ()
376- ..['user_id' ] = adminUser.id
377- ..['followed_topics' ] = jsonEncode (adminPreferences.followedTopics)
378- ..['followed_sources' ] = jsonEncode (adminPreferences.followedSources)
379- ..['followed_countries' ] = jsonEncode (adminPreferences.followedCountries)
380- ..['saved_headlines' ] = jsonEncode (adminPreferences.savedHeadlines),
373+ parameters: () {
374+ final params = adminPreferences.toJson ();
375+ params['user_id' ] = adminUser.id;
376+ params['followed_topics' ] = jsonEncode (params['followed_topics' ]);
377+ params['followed_sources' ] = jsonEncode (params['followed_sources' ]);
378+ params['followed_countries' ] = jsonEncode (params['followed_countries' ]);
379+ params['saved_headlines' ] = jsonEncode (params['saved_headlines' ]);
380+ return params;
381+ }(),
381382 );
382383
383384 await _connection.execute ('COMMIT' );
0 commit comments