1
1
import 'dart:convert' ;
2
2
import 'package:ht_shared/ht_shared.dart' ;
3
+ import 'package:ht_shared/src/fixtures/fixtures.dart' ;
3
4
import 'package:logging/logging.dart' ;
4
5
import 'package:postgres/postgres.dart' ;
5
6
@@ -175,12 +176,10 @@ class DatabaseSeedingService {
175
176
_log.fine ('Seeding topics...' );
176
177
for (final data in topicsFixturesData) {
177
178
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 );
184
183
185
184
await _connection.execute (
186
185
Sql .named (
@@ -197,10 +196,8 @@ class DatabaseSeedingService {
197
196
_log.fine ('Seeding countries...' );
198
197
for (final data in countriesFixturesData) {
199
198
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 );
204
201
205
202
await _connection.execute (
206
203
Sql .named (
@@ -217,22 +214,19 @@ class DatabaseSeedingService {
217
214
_log.fine ('Seeding sources...' );
218
215
for (final data in sourcesFixturesData) {
219
216
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 );
236
230
237
231
await _connection.execute (
238
232
Sql .named (
@@ -251,21 +245,17 @@ class DatabaseSeedingService {
251
245
_log.fine ('Seeding headlines...' );
252
246
for (final data in headlinesFixturesData) {
253
247
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 );
269
259
270
260
await _connection.execute (
271
261
Sql .named (
@@ -332,11 +322,11 @@ class DatabaseSeedingService {
332
322
// Seed Admin User
333
323
_log.fine ('Seeding admin user...' );
334
324
// Find the admin user in the fixture data.
335
- final adminUserData = usersFixturesData.firstWhere (
325
+ final adminUserFixture = usersFixturesData.firstWhere (
336
326
(data) => data['dashboard_role' ] == DashboardUserRole .admin.name,
337
327
orElse: () => throw StateError ('Admin user not found in fixtures.' ),
338
328
);
339
- final adminUser = User .fromJson (adminUserData );
329
+ final adminUser = User .fromJson (adminUserFixture );
340
330
341
331
// The `users` table has specific columns for roles and status.
342
332
await _connection.execute (
@@ -346,16 +336,9 @@ class DatabaseSeedingService {
346
336
'@dashboard_role, @feed_action_status) '
347
337
'ON CONFLICT (id) DO NOTHING' ,
348
338
),
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 ()),
359
342
);
360
343
361
344
// Seed default settings and preferences for the admin user.
@@ -375,15 +358,10 @@ class DatabaseSeedingService {
375
358
'@display_settings, @language, @feed_preferences) '
376
359
'ON CONFLICT (id) DO NOTHING' ,
377
360
),
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),
387
365
);
388
366
389
367
await _connection.execute (
@@ -394,22 +372,12 @@ class DatabaseSeedingService {
394
372
'@followed_sources, @followed_countries, @saved_headlines) '
395
373
'ON CONFLICT (id) DO NOTHING' ,
396
374
),
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),
413
381
);
414
382
415
383
await _connection.execute ('COMMIT' );
0 commit comments