mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-14 17:54:42 +00:00
Refactor user registration process to use UserRegistrationDto;
implement user registration in seed app
This commit is contained in:
@@ -8,29 +8,29 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="idunno.Password.Generator" Version="1.0.1"/>
|
||||
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.1"/>
|
||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="7.0.2"/>
|
||||
<PackageReference Include="Microsoft.Data.Sqlite" Version="10.0.9"/>
|
||||
<PackageReference Include="dbup" Version="5.0.41"/>
|
||||
<PackageReference Include="idunno.Password.Generator" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="7.0.2" />
|
||||
<PackageReference Include="Microsoft.Data.Sqlite" Version="10.0.0" />
|
||||
|
||||
<!--
|
||||
remove this override once microsoft ships official fix to vulnerability
|
||||
https:
|
||||
-->
|
||||
<PackageReference Include="SQLitePCLRaw.lib.e_sqlite3" Version="3.50.3"/>
|
||||
<PackageReference Include="SQLitePCLRaw.lib.e_sqlite3" Version="3.50.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Domain\Domain.Entities\Domain.Entities.csproj"/>
|
||||
<ProjectReference Include="..\..\Infrastructure\Infrastructure.Sql\Infrastructure.Sql.csproj"/>
|
||||
<ProjectReference Include="..\..\Features\Features.Breweries\Features.Breweries.csproj"/>
|
||||
<ProjectReference Include="..\..\Features\Features.Auth\Features.Auth.csproj"/>
|
||||
<ProjectReference Include="..\..\Domain\Domain.Entities\Domain.Entities.csproj" />
|
||||
<ProjectReference
|
||||
Include="..\..\Infrastructure\Infrastructure.Sql\Infrastructure.Sql.csproj" />
|
||||
<ProjectReference Include="..\..\Features\Features.Breweries\Features.Breweries.csproj" />
|
||||
<ProjectReference Include="..\..\Features\Features.Auth\Features.Auth.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<!-- Pipeline-generated seed data, copied to the output directory so it can be opened via
|
||||
ConnectionStrings.SqliteConnectionString at runtime. -->
|
||||
<None CopyToOutputDirectory="PreserveNewest" Include="SeedData\seed.sqlite"/>
|
||||
<None CopyToOutputDirectory="PreserveNewest"
|
||||
Include="SeedData\biergarten_seed_2026-07-20T05-46-02.993511Z.sqlite" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Database.Seed.DatabaseHelpers;
|
||||
public class ConnectionStrings
|
||||
{
|
||||
public static string SqlServerConnectionString => GetSqlServerConnectionString();
|
||||
public static string SqliteConnectionString => "Data Source=SeedData/seed.sqlite";
|
||||
public static string SqliteConnectionString => "Data Source=SeedData/biergarten_seed_2026-07-20T05-46-02.993511Z.sqlite";
|
||||
|
||||
private static string GetSqlServerConnectionString()
|
||||
{
|
||||
@@ -44,4 +44,4 @@ public class ConnectionStrings
|
||||
|
||||
return builder.ConnectionString;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using Database.Seed.DatabaseHelpers;
|
||||
using Database.Seed.PipelineData;
|
||||
using Database.Seed.Sqlite;
|
||||
using Features.Auth.DependencyInjection;
|
||||
using Features.Auth.Dtos;
|
||||
using Features.Auth.Repository;
|
||||
using Features.Breweries.DependencyInjection;
|
||||
using Features.Breweries.Repository;
|
||||
@@ -11,68 +12,109 @@ using Infrastructure.Sql;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using idunno.Password;
|
||||
using Infrastructure.PasswordHashing;
|
||||
using Domain.Entities;
|
||||
|
||||
|
||||
IReadOnlyList<BreweryRecord> breweries;
|
||||
IReadOnlyList<UserRecord> users;
|
||||
try
|
||||
async static Task<int> main()
|
||||
{
|
||||
PipelineSeedDataReader reader = new(ConnectionStrings.SqliteConnectionString);
|
||||
breweries = reader.ReadBreweryRecords();
|
||||
users = reader.ReadUserRecords();
|
||||
}
|
||||
catch (SqliteException ex)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"Error opening database connection: {ex.Message}");
|
||||
Console.ResetColor();
|
||||
return 1;
|
||||
}
|
||||
IReadOnlyList<BreweryRecord> breweries;
|
||||
IReadOnlyList<UserRecord> users;
|
||||
try
|
||||
{
|
||||
PipelineSeedDataReader reader = new(ConnectionStrings.SqliteConnectionString);
|
||||
breweries = reader.ReadBreweryRecords();
|
||||
users = reader.ReadUserRecords();
|
||||
}
|
||||
catch (SqliteException ex)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"Error opening database connection: {ex.Message}");
|
||||
Console.ResetColor();
|
||||
return 1;
|
||||
}
|
||||
|
||||
ServiceCollection services = [];
|
||||
services.AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());
|
||||
services.AddSingleton<ISqlConnectionFactory, DefaultSqlConnectionFactory>();
|
||||
services.AddFeaturesBreweries();
|
||||
services.AddFeaturesAuth();
|
||||
ServiceCollection services = [];
|
||||
services.AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());
|
||||
services.AddSingleton<ISqlConnectionFactory, DefaultSqlConnectionFactory>();
|
||||
services.AddFeaturesBreweries();
|
||||
services.AddFeaturesAuth();
|
||||
|
||||
using ServiceProvider provider = services.BuildServiceProvider();
|
||||
using ServiceProvider provider = services.BuildServiceProvider();
|
||||
|
||||
IBreweryRepository breweryRepository;
|
||||
IAuthRepository authRepository;
|
||||
try
|
||||
{
|
||||
breweryRepository = provider.GetRequiredService<IBreweryRepository>();
|
||||
authRepository = provider.GetRequiredService<IAuthRepository>();
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"Error configuring database connection: {ex.Message}");
|
||||
Console.ResetColor();
|
||||
return 1;
|
||||
}
|
||||
IBreweryRepository breweryRepository;
|
||||
IAuthRepository authRepository;
|
||||
IPasswordInfrastructure passwordInfrastructure;
|
||||
try
|
||||
{
|
||||
breweryRepository = provider.GetRequiredService<IBreweryRepository>();
|
||||
authRepository = provider.GetRequiredService<IAuthRepository>();
|
||||
passwordInfrastructure = provider.GetRequiredService<IPasswordInfrastructure>();
|
||||
}
|
||||
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(
|
||||
$"Loaded {breweries.Count} breweries."
|
||||
);
|
||||
Console.ResetColor();
|
||||
|
||||
for (int i = 0; i < breweries.Count; i++)
|
||||
{
|
||||
BreweryRecord brewery = breweries[i];
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine(
|
||||
$"{i + 1}:\t{brewery.Brewery.NameEn}\t({brewery.Address.City.CityName}, {brewery.Address.City.Country})");
|
||||
$"Loaded {breweries.Count} breweries."
|
||||
);
|
||||
Console.ResetColor();
|
||||
|
||||
for (int 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.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine($"Loaded {users.Count} users.");
|
||||
Console.ResetColor();
|
||||
|
||||
foreach (UserRecord user in users)
|
||||
{
|
||||
string username = user.User.Username;
|
||||
string firstName = user.User.FirstName;
|
||||
string lastName = user.User.LastName;
|
||||
string email = user.Email;
|
||||
DateTime dateOfBirth = DateTime.Parse(user.DateOfBirth);
|
||||
|
||||
// Generate a password that is 64 characters long with 10 digits, 10 symbols,
|
||||
// allowing upper and lower case letters, disallowing repeat characters.
|
||||
string generatedPassword = PasswordGenerator.Generate(length: 64, numberOfDigits: 10, numberOfSymbols: 10);
|
||||
string hashedPassword = passwordInfrastructure.Hash(generatedPassword);
|
||||
|
||||
UserRegistrationDto registrationDto = new(
|
||||
username,
|
||||
firstName,
|
||||
lastName,
|
||||
email,
|
||||
dateOfBirth,
|
||||
hashedPassword
|
||||
);
|
||||
|
||||
try
|
||||
{
|
||||
UserAccount createdUser = await authRepository.RegisterUserAsync(registrationDto);
|
||||
Console.WriteLine($"Created user: {createdUser.Username} ({createdUser.UserAccountId})");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"Error creating user {username}: {ex.Message}");
|
||||
Console.ResetColor();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine($"Loaded {users.Count} users.");
|
||||
Console.ResetColor();
|
||||
|
||||
for (int 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;
|
||||
return await main();
|
||||
|
||||
@@ -105,9 +105,16 @@ public sealed class PipelineSeedDataReader
|
||||
private static IReadOnlyDictionary<long, City> 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
|
||||
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;
|
||||
""";
|
||||
|
||||
@@ -115,27 +122,38 @@ public sealed class PipelineSeedDataReader
|
||||
using SqliteDataReader reader = command.ExecuteReader();
|
||||
|
||||
Dictionary<long, City> cities = [];
|
||||
|
||||
const int cityNameBindIndex = 1;
|
||||
const int stateProvinceBindIndex = 2;
|
||||
const int iso31662BindIndex = 3;
|
||||
const int countryBindIndex = 4;
|
||||
const int iso31661BindIndex = 5;
|
||||
const int localLanguagesBindIndex = 6;
|
||||
const int countryFormatRegexBindIndex = 7;
|
||||
const int cityRegexesBindIndex = 8;
|
||||
|
||||
while (reader.Read())
|
||||
cities[reader.GetInt64(0)] = new City
|
||||
{
|
||||
CityName = reader.GetString(1),
|
||||
StateProvince = reader.GetString(2),
|
||||
Iso31662 = reader.GetString(3),
|
||||
Country = reader.GetString(4),
|
||||
Iso31661 = reader.GetString(5),
|
||||
LocalLanguages = DeserializeStringArray(reader.GetString(6)),
|
||||
CityName = reader.GetString(cityNameBindIndex),
|
||||
StateProvince = reader.GetString(stateProvinceBindIndex),
|
||||
Iso31662 = reader.GetString(iso31662BindIndex),
|
||||
Country = reader.GetString(countryBindIndex),
|
||||
Iso31661 = reader.GetString(iso31661BindIndex),
|
||||
LocalLanguages = DeserializeStringArray(reader.GetString(localLanguagesBindIndex)),
|
||||
PostalCode = new PostalCodeSpec
|
||||
{
|
||||
CountryFormatRegex = reader.GetString(7),
|
||||
CityRegexes = DeserializeStringArray(reader.GetString(8))
|
||||
CountryFormatRegex = reader.GetString(countryFormatRegexBindIndex),
|
||||
CityRegexes = DeserializeStringArray(reader.GetString(cityRegexesBindIndex))
|
||||
}
|
||||
};
|
||||
|
||||
return cities;
|
||||
}
|
||||
|
||||
|
||||
private static IReadOnlyList<string> DeserializeStringArray(string json)
|
||||
{
|
||||
return JsonSerializer.Deserialize<List<string>>(json) ?? [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user