diff --git a/web/backend/Database/Database.Seed/Database.Seed.csproj b/web/backend/Database/Database.Seed/Database.Seed.csproj index 887c52c3..4dc3954b 100644 --- a/web/backend/Database/Database.Seed/Database.Seed.csproj +++ b/web/backend/Database/Database.Seed/Database.Seed.csproj @@ -8,26 +8,29 @@ - - - - - + + + + + - + - + + + + - + diff --git a/web/backend/Database/Database.Seed/DatabaseHelpers/ConnectionStrings.cs b/web/backend/Database/Database.Seed/DatabaseHelpers/ConnectionStrings.cs index 6554b980..32bb3242 100644 --- a/web/backend/Database/Database.Seed/DatabaseHelpers/ConnectionStrings.cs +++ b/web/backend/Database/Database.Seed/DatabaseHelpers/ConnectionStrings.cs @@ -4,25 +4,28 @@ namespace Database.Seed.DatabaseHelpers; public class ConnectionStrings { + public static string SqlServerConnectionString => GetSqlServerConnectionString(); + public static string SqliteConnectionString => "Data Source=SeedData/seed.sqlite"; + private static string GetSqlServerConnectionString() { - string server = + var server = Environment.GetEnvironmentVariable("DB_SERVER") ?? throw new InvalidOperationException("DB_SERVER environment variable is not set"); - string dbName = + var dbName = Environment.GetEnvironmentVariable("DB_NAME") ?? throw new InvalidOperationException("DB_NAME environment variable is not set"); - string user = + var user = Environment.GetEnvironmentVariable("DB_USER") ?? throw new InvalidOperationException("DB_USER environment variable is not set"); - string password = + var password = Environment.GetEnvironmentVariable("DB_PASSWORD") ?? throw new InvalidOperationException("DB_PASSWORD environment variable is not set"); - string trustServerCertificate = + var trustServerCertificate = Environment.GetEnvironmentVariable("DB_TRUST_SERVER_CERTIFICATE") ?? "True"; SqlConnectionStringBuilder builder = new() @@ -32,12 +35,9 @@ public class ConnectionStrings UserID = user, Password = password, TrustServerCertificate = bool.Parse(trustServerCertificate), - Encrypt = true, + Encrypt = true }; return builder.ConnectionString; } - - public static string SqlServerConnectionString => GetSqlServerConnectionString(); - public static string SqliteConnectionString => "Data Source=SeedData/seed.sqlite"; -} +} \ No newline at end of file diff --git a/web/backend/Database/Database.Seed/Dockerfile b/web/backend/Database/Database.Seed/Dockerfile index 1bfaf890..90846b20 100644 --- a/web/backend/Database/Database.Seed/Dockerfile +++ b/web/backend/Database/Database.Seed/Dockerfile @@ -10,7 +10,7 @@ FROM build AS publish ARG BUILD_CONFIGURATION=Release RUN dotnet publish "./Database/Database.Seed/Database.Seed.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false -FROM mcr.microsoft.com/dotnet/runtime:10.0 AS final +FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "Database.Seed.dll"] diff --git a/web/backend/Database/Database.Seed/PipelineData/BreweryAddress.cs b/web/backend/Database/Database.Seed/PipelineData/BreweryAddress.cs index b6902bb1..7e3628fa 100644 --- a/web/backend/Database/Database.Seed/PipelineData/BreweryAddress.cs +++ b/web/backend/Database/Database.Seed/PipelineData/BreweryAddress.cs @@ -5,4 +5,4 @@ public sealed record BreweryAddress public required City City { get; init; } public required string PostalCode { get; init; } -} +} \ No newline at end of file diff --git a/web/backend/Database/Database.Seed/PipelineData/BreweryRecord.cs b/web/backend/Database/Database.Seed/PipelineData/BreweryRecord.cs index 93c67e9a..04245a9a 100644 --- a/web/backend/Database/Database.Seed/PipelineData/BreweryRecord.cs +++ b/web/backend/Database/Database.Seed/PipelineData/BreweryRecord.cs @@ -5,4 +5,4 @@ public sealed record BreweryRecord public required BreweryAddress Address { get; init; } public required BreweryResult Brewery { get; init; } -} +} \ No newline at end of file diff --git a/web/backend/Database/Database.Seed/PipelineData/BreweryResult.cs b/web/backend/Database/Database.Seed/PipelineData/BreweryResult.cs index 13d42f5f..0b10b48b 100644 --- a/web/backend/Database/Database.Seed/PipelineData/BreweryResult.cs +++ b/web/backend/Database/Database.Seed/PipelineData/BreweryResult.cs @@ -9,4 +9,4 @@ public sealed record BreweryResult public required string NameLocal { get; init; } public required string DescriptionLocal { get; init; } -} +} \ No newline at end of file diff --git a/web/backend/Database/Database.Seed/PipelineData/City.cs b/web/backend/Database/Database.Seed/PipelineData/City.cs index 88de9e2e..e8d3cf04 100644 --- a/web/backend/Database/Database.Seed/PipelineData/City.cs +++ b/web/backend/Database/Database.Seed/PipelineData/City.cs @@ -15,4 +15,4 @@ public sealed record City public IReadOnlyList LocalLanguages { get; init; } = []; public required PostalCodeSpec PostalCode { get; init; } -} +} \ No newline at end of file diff --git a/web/backend/Database/Database.Seed/PipelineData/PostalCodeSpec.cs b/web/backend/Database/Database.Seed/PipelineData/PostalCodeSpec.cs index d367da6b..d5581e31 100644 --- a/web/backend/Database/Database.Seed/PipelineData/PostalCodeSpec.cs +++ b/web/backend/Database/Database.Seed/PipelineData/PostalCodeSpec.cs @@ -7,4 +7,4 @@ public sealed record PostalCodeSpec public IReadOnlyList CityRegexes { get; init; } = []; public IReadOnlyList Examples { get; init; } = []; -} +} \ No newline at end of file diff --git a/web/backend/Database/Database.Seed/PipelineData/UserAddress.cs b/web/backend/Database/Database.Seed/PipelineData/UserAddress.cs index 6b63a17c..090a130f 100644 --- a/web/backend/Database/Database.Seed/PipelineData/UserAddress.cs +++ b/web/backend/Database/Database.Seed/PipelineData/UserAddress.cs @@ -5,4 +5,4 @@ public sealed record UserAddress public required City City { get; init; } public required string PostalCode { get; init; } -} +} \ No newline at end of file diff --git a/web/backend/Database/Database.Seed/PipelineData/UserRecord.cs b/web/backend/Database/Database.Seed/PipelineData/UserRecord.cs index fa8f0dc2..7d04f8be 100644 --- a/web/backend/Database/Database.Seed/PipelineData/UserRecord.cs +++ b/web/backend/Database/Database.Seed/PipelineData/UserRecord.cs @@ -9,4 +9,4 @@ public sealed record UserRecord public required string Email { get; init; } public required string DateOfBirth { get; init; } -} +} \ No newline at end of file diff --git a/web/backend/Database/Database.Seed/PipelineData/UserResult.cs b/web/backend/Database/Database.Seed/PipelineData/UserResult.cs index 0e53881d..4aee2333 100644 --- a/web/backend/Database/Database.Seed/PipelineData/UserResult.cs +++ b/web/backend/Database/Database.Seed/PipelineData/UserResult.cs @@ -13,4 +13,4 @@ public sealed record UserResult public required string Bio { get; init; } public required float ActivityWeight { get; init; } -} +} \ No newline at end of file diff --git a/web/backend/Database/Database.Seed/Program.cs b/web/backend/Database/Database.Seed/Program.cs index 6a025994..2ead26d6 100644 --- a/web/backend/Database/Database.Seed/Program.cs +++ b/web/backend/Database/Database.Seed/Program.cs @@ -1,7 +1,14 @@ using Database.Seed.DatabaseHelpers; using Database.Seed.PipelineData; using Database.Seed.Sqlite; +using Features.Auth.DependencyInjection; +using Features.Auth.Repository; +using Features.Breweries.DependencyInjection; +using Features.Breweries.Repository; +using Infrastructure.Sql; using Microsoft.Data.Sqlite; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; namespace Database.Seed; @@ -25,6 +32,28 @@ public class Program return 1; } + ServiceCollection services = new(); + services.AddSingleton(new ConfigurationBuilder().Build()); + services.AddSingleton(); + services.AddFeaturesBreweries(); + services.AddFeaturesAuth(); + + using ServiceProvider provider = services.BuildServiceProvider(); + + IBreweryRepository breweryRepository; + IAuthRepository authRepository; + try + { + breweryRepository = provider.GetRequiredService(); + authRepository = provider.GetRequiredService(); + } + catch (InvalidOperationException ex) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"Error configuring database connection: {ex.Message}"); + Console.ResetColor(); + return 1; + } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine( @@ -32,22 +61,23 @@ public class Program ); Console.ResetColor(); - for (int i = 0; i < breweries.Count; i++) + for (var i = 0; i < breweries.Count; i++) { BreweryRecord brewery = breweries[i]; - Console.WriteLine($"{i + 1}:\t{brewery.Brewery.NameEn}\t({brewery.Address.City.CityName}, {brewery.Address.City.Country})"); + Console.WriteLine( + $"{i + 1}:\t{brewery.Brewery.NameEn}\t({brewery.Address.City.CityName}, {brewery.Address.City.Country})"); } + Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"Loaded {users.Count} users."); Console.ResetColor(); - for (int i = 0; i < users.Count; i++) + for (var i = 0; i < users.Count; i++) { UserRecord user = users[i]; Console.WriteLine($"{i + 1}:\t{user.User.FirstName} {user.User.LastName}\t ({user.Email})"); } return 0; - } -} +} \ No newline at end of file diff --git a/web/backend/Database/Database.Seed/Sqlite/PipelineSeedDataReader.cs b/web/backend/Database/Database.Seed/Sqlite/PipelineSeedDataReader.cs index 8657d121..16b33d68 100644 --- a/web/backend/Database/Database.Seed/Sqlite/PipelineSeedDataReader.cs +++ b/web/backend/Database/Database.Seed/Sqlite/PipelineSeedDataReader.cs @@ -21,11 +21,11 @@ public sealed class PipelineSeedDataReader IReadOnlyDictionary cities = ReadCities(connection); const string sql = """ - SELECT b.name_en, b.description_en, b.name_local, b.description_local, - ba.city_id, ba.postal_code - FROM breweries b - JOIN brewery_addresses ba ON ba.brewery_id = b.id; - """; + SELECT b.name_en, b.description_en, b.name_local, b.description_local, + ba.city_id, ba.postal_code + FROM breweries b + JOIN brewery_addresses ba ON ba.brewery_id = b.id; + """; using SqliteCommand command = new(sql, connection); using SqliteDataReader reader = command.ExecuteReader(); @@ -40,13 +40,13 @@ public sealed class PipelineSeedDataReader NameEn = reader.GetString(0), DescriptionEn = reader.GetString(1), NameLocal = reader.GetString(2), - DescriptionLocal = reader.GetString(3), + DescriptionLocal = reader.GetString(3) }, Address = new BreweryAddress { City = cities[reader.GetInt64(4)], - PostalCode = reader.GetString(5), - }, + PostalCode = reader.GetString(5) + } } ); @@ -61,12 +61,12 @@ public sealed class PipelineSeedDataReader IReadOnlyDictionary cities = ReadCities(connection); const string sql = """ - SELECT u.first_name, u.last_name, u.gender, u.username, u.bio, - u.activity_weight, u.email, u.date_of_birth, - ua.city_id, ua.postal_code - FROM users u - JOIN user_addresses ua ON ua.user_id = u.id; - """; + SELECT u.first_name, u.last_name, u.gender, u.username, u.bio, + u.activity_weight, u.email, u.date_of_birth, + ua.city_id, ua.postal_code + FROM users u + JOIN user_addresses ua ON ua.user_id = u.id; + """; using SqliteCommand command = new(sql, connection); using SqliteDataReader reader = command.ExecuteReader(); @@ -83,15 +83,15 @@ public sealed class PipelineSeedDataReader Gender = reader.GetString(2), Username = reader.GetString(3), Bio = reader.GetString(4), - ActivityWeight = (float)reader.GetDouble(5), + ActivityWeight = (float)reader.GetDouble(5) }, Email = reader.GetString(6), DateOfBirth = reader.GetString(7), Address = new UserAddress { City = cities[reader.GetInt64(8)], - PostalCode = reader.GetString(9), - }, + PostalCode = reader.GetString(9) + } } ); @@ -101,11 +101,11 @@ public sealed class PipelineSeedDataReader private static IReadOnlyDictionary ReadCities(SqliteConnection connection) { const string sql = """ - SELECT id, city, state_province, iso3166_2, country, iso3166_1, - local_languages_json, postal_code_country_format_regex, - postal_code_city_regex_json - FROM cities; - """; + SELECT id, city, state_province, iso3166_2, country, iso3166_1, + local_languages_json, postal_code_country_format_regex, + postal_code_city_regex_json + FROM cities; + """; using SqliteCommand command = new(sql, connection); using SqliteDataReader reader = command.ExecuteReader(); @@ -123,13 +123,15 @@ public sealed class PipelineSeedDataReader PostalCode = new PostalCodeSpec { CountryFormatRegex = reader.GetString(7), - CityRegexes = DeserializeStringArray(reader.GetString(8)), - }, + CityRegexes = DeserializeStringArray(reader.GetString(8)) + } }; return cities; } - private static IReadOnlyList DeserializeStringArray(string json) => - JsonSerializer.Deserialize>(json) ?? []; -} + private static IReadOnlyList DeserializeStringArray(string json) + { + return JsonSerializer.Deserialize>(json) ?? []; + } +} \ No newline at end of file