Skip to content

Commit e105645

Browse files
committed
Countries seeding logic
1 parent 4f25067 commit e105645

6 files changed

Lines changed: 405 additions & 0 deletions

File tree

src/KeeperData.Api/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ static void ConfigureBuilder(WebApplicationBuilder builder)
5454
}
5555
});
5656

57+
builder.Services.AddHostedService<KeeperData.Infrastructure.Services.JsonGenerationService>();
58+
builder.Services.AddHostedService<KeeperData.Infrastructure.Services.MongoDataSeeder>();
59+
5760
builder.Services.ConfigureApi(builder.Configuration);
5861
}
5962

src/KeeperData.Core/Documents/CountryDocument.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
using KeeperData.Core.Attributes;
12
using KeeperData.Core.Domain.Sites;
23
using KeeperData.Core.Repositories;
4+
using MongoDB.Bson;
35
using MongoDB.Bson.Serialization.Attributes;
46
using System.Text.Json.Serialization;
57

68
namespace KeeperData.Core.Documents;
79

10+
[CollectionName("refCountries")]
811
public class CountryDocument : INestedEntity
912
{
13+
[BsonId]
14+
[BsonRepresentation(BsonType.String)]
1015
[JsonPropertyName("id")]
1116
[BsonElement("id")]
1217
public required string IdentifierId { get; set; }
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
code,name
2+
AF,Afghanistan
3+
AL,Albania
4+
DZ,Algeria
5+
AD,Andorra
6+
AO,Angola
7+
AR,Argentina
8+
AM,Armenia
9+
AU,Australia
10+
AT,Austria
11+
AZ,Azerbaijan
12+
BS,Bahamas
13+
BH,Bahrain
14+
BD,Bangladesh
15+
BB,Barbados
16+
BY,Belarus
17+
BE,Belgium
18+
BZ,Belize
19+
BJ,Benin
20+
BT,Bhutan
21+
BO,Bolivia
22+
BA,Bosnia and Herzegovina
23+
BW,Botswana
24+
BR,Brazil
25+
BN,Brunei
26+
BG,Bulgaria
27+
BF,Burkina Faso
28+
BI,Burundi
29+
KH,Cambodia
30+
CM,Cameroon
31+
CA,Canada
32+
CV,Cape Verde
33+
CF,Central African Republic
34+
TD,Chad
35+
CL,Chile
36+
CN,China
37+
CO,Colombia
38+
KM,Comoros
39+
CG,Congo
40+
CR,Costa Rica
41+
HR,Croatia
42+
CU,Cuba
43+
CY,Cyprus
44+
CZ,Czech Republic
45+
CD,Democratic Republic of the Congo
46+
DK,Denmark
47+
DJ,Djibouti
48+
DM,Dominica
49+
DO,Dominican Republic
50+
EC,Ecuador
51+
EG,Egypt
52+
SV,El Salvador
53+
GQ,Equatorial Guinea
54+
ER,Eritrea
55+
EE,Estonia
56+
ET,Ethiopia
57+
FJ,Fiji
58+
FI,Finland
59+
FR,France
60+
GA,Gabon
61+
GM,Gambia
62+
GE,Georgia
63+
DE,Germany
64+
GH,Ghana
65+
GR,Greece
66+
GD,Grenada
67+
GT,Guatemala
68+
GN,Guinea
69+
GW,Guinea-Bissau
70+
GY,Guyana
71+
HT,Haiti
72+
HN,Honduras
73+
HU,Hungary
74+
IS,Iceland
75+
IN,India
76+
ID,Indonesia
77+
IR,Iran
78+
IQ,Iraq
79+
IE,Ireland
80+
IL,Israel
81+
IT,Italy
82+
CI,Ivory Coast
83+
JM,Jamaica
84+
JP,Japan
85+
JO,Jordan
86+
KZ,Kazakhstan
87+
KE,Kenya
88+
KI,Kiribati
89+
KW,Kuwait
90+
KG,Kyrgyzstan
91+
LA,Laos
92+
LV,Latvia
93+
LB,Lebanon
94+
LS,Lesotho
95+
LR,Liberia
96+
LY,Libya
97+
LI,Liechtenstein
98+
LT,Lithuania
99+
LU,Luxembourg
100+
MK,North Macedonia
101+
MG,Madagascar
102+
MW,Malawi
103+
MY,Malaysia
104+
MV,Maldives
105+
ML,Mali
106+
MT,Malta
107+
MH,Marshall Islands
108+
MR,Mauritania
109+
MU,Mauritius
110+
MX,Mexico
111+
FM,Micronesia
112+
MD,Moldova
113+
MC,Monaco
114+
MN,Mongolia
115+
ME,Montenegro
116+
MA,Morocco
117+
MZ,Mozambique
118+
MM,Myanmar
119+
NA,Namibia
120+
NR,Nauru
121+
NP,Nepal
122+
NL,Netherlands
123+
NZ,New Zealand
124+
NI,Nicaragua
125+
NE,Niger
126+
NG,Nigeria
127+
KP,North Korea
128+
NO,Norway
129+
OM,Oman
130+
PK,Pakistan
131+
PW,Palau
132+
PA,Panama
133+
PG,Papua New Guinea
134+
PY,Paraguay
135+
PE,Peru
136+
PH,Philippines
137+
PL,Poland
138+
PT,Portugal
139+
QA,Qatar
140+
RO,Romania
141+
RU,Russia
142+
RW,Rwanda
143+
KN,Saint Kitts and Nevis
144+
LC,Saint Lucia
145+
VC,Saint Vincent and the Grenadines
146+
WS,Samoa
147+
SM,San Marino
148+
ST,Sao Tome and Principe
149+
SA,Saudi Arabia
150+
SN,Senegal
151+
RS,Serbia
152+
SC,Seychelles
153+
SL,Sierra Leone
154+
SG,Singapore
155+
SK,Slovakia
156+
SI,Slovenia
157+
SB,Solomon Islands
158+
SO,Somalia
159+
ZA,South Africa
160+
KR,South Korea
161+
SS,South Sudan
162+
ES,Spain
163+
LK,Sri Lanka
164+
SD,Sudan
165+
SR,Suriname
166+
SZ,Eswatini
167+
SE,Sweden
168+
CH,Switzerland
169+
SY,Syria
170+
TW,Taiwan
171+
TJ,Tajikistan
172+
TZ,Tanzania
173+
TH,Thailand
174+
TL,Timor-Leste
175+
TG,Togo
176+
TO,Tonga
177+
TT,Trinidad and Tobago
178+
TN,Tunisia
179+
TR,Turkey
180+
TM,Turkmenistan
181+
TV,Tuvalu
182+
UG,Uganda
183+
UA,Ukraine
184+
AE,United Arab Emirates
185+
GB,United Kingdom
186+
US,United States
187+
UY,Uruguay
188+
UZ,Uzbekistan
189+
VU,Vanuatu
190+
VA,Vatican City
191+
VE,Venezuela
192+
VN,Vietnam
193+
YE,Yemen
194+
ZM,Zambia
195+
ZW,Zimbabwe

src/KeeperData.Infrastructure/KeeperData.Infrastructure.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,14 @@
2424
<ProjectReference Include="..\KeeperData.Application\KeeperData.Application.csproj" />
2525
</ItemGroup>
2626

27+
<ItemGroup>
28+
<Content Include="Data\Source\countries.csv">
29+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
30+
</Content>
31+
</ItemGroup>
32+
33+
<ItemGroup>
34+
<Folder Include="Data\Seed\" />
35+
<Folder Include="Data\Source\" />
36+
</ItemGroup>
2737
</Project>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
3+
using Microsoft.Extensions.Logging;
4+
using System;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Text.Json;
8+
using System.Text.Json.Serialization;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
using File = System.IO.File;
12+
13+
namespace KeeperData.Infrastructure.Services;
14+
15+
/// <summary>
16+
/// A startup service that generates a baseline JSON file from a CSV,
17+
/// but only if the JSON file does not already exist.
18+
/// </summary>
19+
public class JsonGenerationService : IHostedService
20+
{
21+
private readonly IWebHostEnvironment _env;
22+
private readonly ILogger<JsonGenerationService> _logger;
23+
24+
// Just the properties we need for now
25+
private record CountryJson(
26+
[property: JsonPropertyName("id")] string Id,
27+
[property: JsonPropertyName("code")] string Code,
28+
[property: JsonPropertyName("name")] string Name
29+
);
30+
31+
public JsonGenerationService(IWebHostEnvironment env, ILogger<JsonGenerationService> logger)
32+
{
33+
_env = env;
34+
_logger = logger;
35+
}
36+
37+
public Task StartAsync(CancellationToken cancellationToken)
38+
{
39+
_logger.LogInformation("JSON Generation Service is running.");
40+
41+
var sourceCsvPath = Path.Combine(_env.ContentRootPath, "Data", "Source", "countries.csv");
42+
var targetJsonPath = Path.Combine(_env.ContentRootPath, "Data", "Seed", "countries.json");
43+
44+
if (File.Exists(targetJsonPath))
45+
{
46+
_logger.LogInformation("'{FileName}' already exists. Skipping generation.", Path.GetFileName(targetJsonPath));
47+
return Task.CompletedTask;
48+
}
49+
50+
if (!File.Exists(sourceCsvPath))
51+
{
52+
_logger.LogWarning("Source file '{FileName}' not found. Cannot generate target JSON.", Path.GetFileName(sourceCsvPath));
53+
return Task.CompletedTask;
54+
}
55+
56+
try
57+
{
58+
_logger.LogInformation("'{TargetFile}' not found. Generating from '{SourceFile}'...", Path.GetFileName(targetJsonPath), Path.GetFileName(sourceCsvPath));
59+
60+
var lines = File.ReadAllLines(sourceCsvPath);
61+
var countries = lines
62+
.Skip(1) // Skip header
63+
.Where(line => !string.IsNullOrWhiteSpace(line))
64+
.Select(line =>
65+
{
66+
var parts = line.Split(',');
67+
return new CountryJson(
68+
Guid.NewGuid().ToString(),
69+
parts[0].Trim(),
70+
parts[1].Trim()
71+
);
72+
})
73+
.ToList();
74+
75+
var options = new JsonSerializerOptions { WriteIndented = true };
76+
var jsonString = JsonSerializer.Serialize(countries, options);
77+
78+
var outputDir = Path.GetDirectoryName(targetJsonPath);
79+
if (outputDir != null && !Directory.Exists(outputDir))
80+
{
81+
Directory.CreateDirectory(outputDir);
82+
}
83+
84+
File.WriteAllText(targetJsonPath, jsonString);
85+
86+
_logger.LogInformation("Successfully generated '{FileName}' with {Count} records.", Path.GetFileName(targetJsonPath), countries.Count);
87+
}
88+
catch (Exception ex)
89+
{
90+
_logger.LogError(ex, "An error occurred while generating JSON from CSV.");
91+
}
92+
93+
return Task.CompletedTask;
94+
}
95+
96+
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
97+
}

0 commit comments

Comments
 (0)