Skip to content

Commit

Permalink
refactor: group all databases in one #73
Browse files Browse the repository at this point in the history
  • Loading branch information
GenjiruSUchiwa authored Dec 1, 2024
1 parent a24d4c0 commit 2984aad
Show file tree
Hide file tree
Showing 45 changed files with 1,324 additions and 722 deletions.
4 changes: 4 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
<PackageVersion Include="NWebsec.AspNetCore.Core" Version="3.0.1" />
<PackageVersion Include="NWebsec.AspNetCore.Middleware" Version="3.0.0" />
<PackageVersion Include="Respawn" Version="6.2.1" />
<PackageVersion Include="runtime.unix.System.Private.Uri" Version="4.3.2" />
<PackageVersion Include="runtime.win7.System.Private.Uri" Version="4.3.2" />
<PackageVersion Include="Scalar.AspNetCore" Version="1.2.43" />
<PackageVersion Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageVersion Include="Serilog.Settings.Configuration" Version="8.0.4" />
Expand All @@ -62,6 +64,7 @@
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="System.Private.Uri" Version="4.3.2" />
<PackageVersion Include="Testcontainers" Version="3.10.0" />
<PackageVersion Include="Testcontainers.PostgreSql" Version="3.10.0" />
<PackageVersion Include="xunit" Version="2.5.3" />
Expand All @@ -77,5 +80,6 @@
<PackageVersion Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Update="Asp.Versioning.Mvc" Version="8.1.0" />
<PackageVersion Update="System.Private.Uri" Version="4.3.2" />
</ItemGroup>
</Project>
12 changes: 5 additions & 7 deletions src/Account/Account.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Ardalis.GuardClauses"/>
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" />
<PackageReference Include="ErrorOr"/>
Expand All @@ -15,12 +16,8 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Content Include="..\..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
<PackageReference Update="System.Private.Uri" />
<PackageReference Include="System.Private.Uri" />
</ItemGroup>

<ItemGroup>
Expand All @@ -33,6 +30,7 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Data\Migrations\" />
<Folder Include="Profile\Features\V1\" />
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 1 addition & 5 deletions src/Account/AccountModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ public static IServiceCollection AddAccountModule(
IConfiguration configuration
)
{
services.AddPlaceDbContext<AccountDbContext>(nameof(Account), configuration);
services.AddPlaceDbContext<IdentityApplicationDbContext>(
nameof(Core.Identity),
configuration
);
services.AddPlaceDbContext<AccountDbContext>("PlaceDb", configuration);
services.AddScoped<IDataSeeder<AccountDbContext>, AccountDataSeeder>();
return services;
}
Expand Down
9 changes: 3 additions & 6 deletions src/Account/Data/Configurations/AccountDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
using Account.Data.Models;
using Account.Profile.Models;
using Core.EF;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;

namespace Account.Data.Configurations;

public class AccountDbContext : AppDbContextBase
{
private IDbContextTransaction _currentTransaction;

public AccountDbContext(
DbContextOptions<AccountDbContext> options,
IHttpContextAccessor httpContextAccessor
)
: base(options, httpContextAccessor) { }

public DbSet<ProfileReadModel> Profiles => Set<ProfileReadModel>();
public DbSet<UserProfile> Profiles => Set<UserProfile>();

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new ProfileConfiguration());
modelBuilder.ApplyConfiguration(new UserProfileConfiguration());

base.OnModelCreating(modelBuilder);
}
Expand Down
77 changes: 0 additions & 77 deletions src/Account/Data/Configurations/ProfileConfiguration.cs

This file was deleted.

125 changes: 125 additions & 0 deletions src/Account/Data/Configurations/UserProfileConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using Account.Profile.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public class UserProfileConfiguration : IEntityTypeConfiguration<UserProfile>
{
public void Configure(EntityTypeBuilder<UserProfile> builder)
{
builder.ToTable("Profiles");

builder.HasKey(p => p.Id);
builder
.Property(p => p.Id)
.HasConversion(id => id.Value, value => new UserProfileId(value))
.ValueGeneratedNever();

builder.Property(p => p.UserId).IsRequired();

builder
.Property(p => p.Email)
.HasConversion(email => email.Value, value => Email.Create(value).Value)
.IsRequired();

builder.Property(p => p.CreatedAt).IsRequired().HasColumnType("timestamp with time zone");

builder.Property(p => p.CreatedBy).IsRequired();

builder.Property(p => p.LastModifiedAt).HasColumnType("timestamp with time zone");

builder.Property(p => p.LastModifiedBy);

builder.Property(p => p.DeletedAt).HasColumnType("timestamp with time zone");

builder.Property(p => p.DeletedBy);

builder.Property(p => p.IsDeleted).IsRequired().HasDefaultValue(false);

// Configuration de PersonalInfo comme Owned Entity
builder.OwnsOne(
p => p.PersonalInfo,
personalInfo =>
{
personalInfo
.Property(pi => pi.FirstName)
.HasConversion(ln => ln!.Value, value => FirstName.Create(value).Value)
.HasMaxLength(FirstName.MaxLength)
.HasColumnName("FirstName");

personalInfo
.Property(pi => pi.LastName)
.HasConversion(ln => ln!.Value, value => LastName.Create(value).Value)
.HasMaxLength(LastName.MaxLength)
.HasColumnName("LastName");

personalInfo
.Property(pi => pi.DateOfBirth)
.HasConversion(dob => dob!.Value, value => DateOfBirth.Create(value).Value)
.HasColumnType("timestamp with time zone")
.HasColumnName("DateOfBirth");

personalInfo
.Property(pi => pi.PhoneNumber)
.HasConversion(phone => phone!.Value, value => PhoneNumber.Parse(value).Value)
.HasColumnName("PhoneNumber");

personalInfo.Property(pi => pi.Gender).HasConversion<int>().HasColumnName("Gender");

personalInfo.OwnsOne(
pi => pi.Address,
address =>
{
address.Property(a => a.Street).HasColumnName("Street");

address.Property(a => a.ZipCode).HasColumnName("ZipCode");

address.Property(a => a.City).HasColumnName("City");

address.Property(a => a.Country).HasColumnName("Country");

address
.Property(a => a.AdditionalDetails)
.HasColumnName("AdditionalAddressDetails");

address.OwnsOne(
a => a.Coordinates,
coordinates =>
{
coordinates
.Property(c => c.Latitude)
.HasPrecision(9, 6)
.HasColumnName("Latitude");

coordinates
.Property(c => c.Longitude)
.HasPrecision(9, 6)
.HasColumnName("Longitude");
}
);
}
);
}
);

builder
.Property(p => p.DeletedAt)
.HasColumnName("DeletedAt")
.HasColumnType("timestamp with time zone");

builder.Property(p => p.DeletedBy).HasColumnName("DeletedBy");

builder
.Property(p => p.IsDeleted)
.HasColumnName("IsDeleted")
.IsRequired()
.HasDefaultValue(false);

builder.HasIndex(p => p.Email).IsUnique();

builder.HasIndex(p => p.IsDeleted);

builder.HasIndex(p => p.IsDeleted);

builder.HasQueryFilter(p => !p.IsDeleted);
}
}
18 changes: 0 additions & 18 deletions src/Account/Data/DesignTimeDbContextFactory.cs

This file was deleted.

Loading

0 comments on commit 2984aad

Please sign in to comment.