11import 'dart:convert' ;
22import 'package:ht_shared/ht_shared.dart' ;
3+ import 'package:ht_shared/src/fixtures/fixtures.dart' ;
34import 'package:logging/logging.dart' ;
45import 'package:postgres/postgres.dart' ;
56
@@ -175,12 +176,10 @@ class DatabaseSeedingService {
175176 _log.fine ('Seeding topics...' );
176177 for (final data in topicsFixturesData) {
177178 final topic = Topic .fromJson (data);
178- final params = topic.toJson ();
179-
180- // Ensure optional fields exist for the postgres driver.
181- params.putIfAbsent ('description' , () => null );
182- params.putIfAbsent ('icon_url' , () => null );
183- params.putIfAbsent ('updated_at' , () => null );
179+ final params = topic.toJson ()
180+ ..putIfAbsent ('description' , () => null )
181+ ..putIfAbsent ('icon_url' , () => null )
182+ ..putIfAbsent ('updated_at' , () => null );
184183
185184 await _connection.execute (
186185 Sql .named (
@@ -197,10 +196,8 @@ class DatabaseSeedingService {
197196 _log.fine ('Seeding countries...' );
198197 for (final data in countriesFixturesData) {
199198 final country = Country .fromJson (data);
200- final params = country.toJson ();
201-
202- // Ensure optional fields exist for the postgres driver.
203- params.putIfAbsent ('updated_at' , () => null );
199+ final params = country.toJson ()
200+ ..putIfAbsent ('updated_at' , () => null );
204201
205202 await _connection.execute (
206203 Sql .named (
@@ -217,22 +214,19 @@ class DatabaseSeedingService {
217214 _log.fine ('Seeding sources...' );
218215 for (final data in sourcesFixturesData) {
219216 final source = Source .fromJson (data);
220- final params = source.toJson ();
221-
222- // The `headquarters` field in the model is a nested `Country`
223- // object. We extract its ID to store in the
224- // `headquarters_country_id` column and then remove the original
225- // nested object from the parameters to avoid a "superfluous
226- // variable" error.
227- params['headquarters_country_id' ] = source.headquarters.id;
228- params.remove ('headquarters' );
229-
230- // Ensure optional fields exist for the postgres driver.
231- params.putIfAbsent ('description' , () => null );
232- params.putIfAbsent ('url' , () => null );
233- params.putIfAbsent ('language' , () => null );
234- params.putIfAbsent ('source_type' , () => null );
235- params.putIfAbsent ('updated_at' , () => null );
217+ final params = source.toJson ()
218+ // The `headquarters` field in the model is a nested `Country`
219+ // object. We extract its ID to store in the
220+ // `headquarters_country_id` column and then remove the original
221+ // nested object from the parameters to avoid a "superfluous
222+ // variable" error.
223+ ..['headquarters_country_id' ] = source.headquarters.id
224+ ..remove ('headquarters' )
225+ ..putIfAbsent ('description' , () => null )
226+ ..putIfAbsent ('url' , () => null )
227+ ..putIfAbsent ('language' , () => null )
228+ ..putIfAbsent ('source_type' , () => null )
229+ ..putIfAbsent ('updated_at' , () => null );
236230
237231 await _connection.execute (
238232 Sql .named (
@@ -251,21 +245,17 @@ class DatabaseSeedingService {
251245 _log.fine ('Seeding headlines...' );
252246 for (final data in headlinesFixturesData) {
253247 final headline = Headline .fromJson (data);
254- final params = headline.toJson ();
255-
256- // Extract IDs from nested objects and remove the objects to match schema.
257- params['source_id' ] = headline.source.id;
258- params['topic_id' ] = headline.topic.id;
259- params['event_country_id' ] = headline.eventCountry.id;
260- params.remove ('source' );
261- params.remove ('topic' );
262- params.remove ('eventCountry' );
263-
264- // Ensure optional fields exist for the postgres driver.
265- params.putIfAbsent ('excerpt' , () => null );
266- params.putIfAbsent ('updated_at' , () => null );
267- params.putIfAbsent ('image_url' , () => null );
268- params.putIfAbsent ('url' , () => null );
248+ final params = headline.toJson ()
249+ ..['source_id' ] = headline.source.id
250+ ..['topic_id' ] = headline.topic.id
251+ ..['event_country_id' ] = headline.eventCountry.id
252+ ..remove ('source' )
253+ ..remove ('topic' )
254+ ..remove ('eventCountry' )
255+ ..putIfAbsent ('excerpt' , () => null )
256+ ..putIfAbsent ('updated_at' , () => null )
257+ ..putIfAbsent ('image_url' , () => null )
258+ ..putIfAbsent ('url' , () => null );
269259
270260 await _connection.execute (
271261 Sql .named (
@@ -332,11 +322,11 @@ class DatabaseSeedingService {
332322 // Seed Admin User
333323 _log.fine ('Seeding admin user...' );
334324 // Find the admin user in the fixture data.
335- final adminUserData = usersFixturesData.firstWhere (
325+ final adminUserFixture = usersFixturesData.firstWhere (
336326 (data) => data['dashboard_role' ] == DashboardUserRole .admin.name,
337327 orElse: () => throw StateError ('Admin user not found in fixtures.' ),
338328 );
339- final adminUser = User .fromJson (adminUserData );
329+ final adminUser = User .fromJson (adminUserFixture );
340330
341331 // The `users` table has specific columns for roles and status.
342332 await _connection.execute (
@@ -346,16 +336,9 @@ class DatabaseSeedingService {
346336 '@dashboard_role, @feed_action_status) '
347337 'ON CONFLICT (id) DO NOTHING' ,
348338 ),
349- parameters: {
350- 'id' : adminUser.id,
351- 'email' : adminUser.email,
352- 'app_role' : adminUser.appRole.name,
353- 'dashboard_role' : adminUser.dashboardRole.name,
354- 'feed_action_status' : jsonEncode (
355- adminUser.feedActionStatus
356- .map ((key, value) => MapEntry (key.name, value.toJson ())),
357- ),
358- },
339+ parameters: adminUser.toJson ()
340+ ..['feed_action_status' ] =
341+ jsonEncode (adminUser.feedActionStatus.toJson ()),
359342 );
360343
361344 // Seed default settings and preferences for the admin user.
@@ -375,15 +358,10 @@ class DatabaseSeedingService {
375358 '@display_settings, @language, @feed_preferences) '
376359 'ON CONFLICT (id) DO NOTHING' ,
377360 ),
378- parameters: {
379- 'id' : adminSettings.id,
380- 'user_id' : adminUser.id,
381- 'display_settings' :
382- jsonEncode (adminSettings.displaySettings.toJson ()),
383- 'language' : adminSettings.language,
384- 'feed_preferences' :
385- jsonEncode (adminSettings.feedPreferences.toJson ()),
386- },
361+ parameters: adminSettings.toJson ()
362+ ..['user_id' ] = adminUser.id
363+ ..['display_settings' ] = jsonEncode (adminSettings.displaySettings)
364+ ..['feed_preferences' ] = jsonEncode (adminSettings.feedPreferences),
387365 );
388366
389367 await _connection.execute (
@@ -394,22 +372,12 @@ class DatabaseSeedingService {
394372 '@followed_sources, @followed_countries, @saved_headlines) '
395373 'ON CONFLICT (id) DO NOTHING' ,
396374 ),
397- parameters: {
398- 'id' : adminPreferences.id,
399- 'user_id' : adminUser.id,
400- 'followed_topics' : jsonEncode (
401- adminPreferences.followedTopics.map ((e) => e.toJson ()).toList (),
402- ),
403- 'followed_sources' : jsonEncode (
404- adminPreferences.followedSources.map ((e) => e.toJson ()).toList (),
405- ),
406- 'followed_countries' : jsonEncode (
407- adminPreferences.followedCountries.map ((e) => e.toJson ()).toList (),
408- ),
409- 'saved_headlines' : jsonEncode (
410- adminPreferences.savedHeadlines.map ((e) => e.toJson ()).toList (),
411- ),
412- },
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),
413381 );
414382
415383 await _connection.execute ('COMMIT' );
0 commit comments