Fixed configurations of owned types

This commit is contained in:
2024-08-15 23:40:50 +03:00
parent 54e31d41fc
commit 72924fb037
18 changed files with 83 additions and 81 deletions

View File

@@ -1,18 +1,23 @@
using Domains.ApplicantDomain; using Domains;
using Domains.ApplicantDomain;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Infrastructure.Database.Applicants.Configuration; namespace Infrastructure.Database.Applicants.Configuration;
public class AddressConfiguration : IEntityTypeConfiguration<Address> public static class AddressConfiguration<T> where T : class, IEntity
{ {
public void Configure(EntityTypeBuilder<Address> entity) public static void Configure(OwnedNavigationBuilder<T, Address> entity)
{ {
entity.HasOne(a => a.Country).WithMany().OnDelete(DeleteBehavior.Restrict);
entity.HasOne(a => a.City).WithMany().OnDelete(DeleteBehavior.Restrict);
entity.Property(p => p.Street) entity.Property(p => p.Street)
.IsUnicode(false) .IsUnicode(false)
.HasMaxLength(100); .HasMaxLength(100);
entity.Property(p => p.Building) entity.Property(p => p.Building)
.IsUnicode(false) .IsUnicode(false)
.HasMaxLength(10); .HasMaxLength(10);
} }
} }

View File

@@ -9,11 +9,13 @@ public class ApplicantConfiguration : IEntityTypeConfiguration<Applicant>
public void Configure(EntityTypeBuilder<Applicant> entity) public void Configure(EntityTypeBuilder<Applicant> entity)
{ {
entity.ToTable("Applicants"); entity.ToTable("Applicants");
entity.OwnsOne(p => p.Name, NameConfiguration<Applicant>.Configure);
entity.OwnsOne(p => p.FatherName, NameConfiguration<Applicant>.Configure);
entity.OwnsOne(p => p.MotherName, NameConfiguration<Applicant>.Configure);
entity.OwnsOne(p => p.Passport, PassportConfiguration<Applicant>.Configure);
entity.OwnsOne(p => p.Name); entity.HasOne(a => a.CityOfBirth).WithMany().OnDelete(DeleteBehavior.Restrict);
entity.OwnsOne(p => p.FatherName); entity.HasOne(a => a.CountryOfBirth).WithMany().OnDelete(DeleteBehavior.Restrict);
entity.OwnsOne(p => p.MotherName);
entity.OwnsOne(p => p.Passport);
entity.Property(p => p.Citizenship) entity.Property(p => p.Citizenship)
.IsUnicode(false) .IsUnicode(false)
@@ -23,4 +25,4 @@ public class ApplicantConfiguration : IEntityTypeConfiguration<Applicant>
.IsUnicode(false) .IsUnicode(false)
.HasMaxLength(30); .HasMaxLength(30);
} }
} }

View File

@@ -1,12 +1,12 @@
using Domains.ApplicantDomain; using Domains;
using Microsoft.EntityFrameworkCore; using Domains.ApplicantDomain;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Infrastructure.Database.Applicants.Configuration; namespace Infrastructure.Database.Applicants.Configuration;
public class NameConfiguration : IEntityTypeConfiguration<Name> public static class NameConfiguration<T> where T : class, IEntity
{ {
public void Configure(EntityTypeBuilder<Name> entity) public static void Configure(OwnedNavigationBuilder<T, Name> entity)
{ {
entity.Property(p => p.FirstName) entity.Property(p => p.FirstName)
.IsUnicode(false) .IsUnicode(false)
@@ -20,4 +20,4 @@ public class NameConfiguration : IEntityTypeConfiguration<Name>
.IsUnicode(false) .IsUnicode(false)
.HasMaxLength(50); .HasMaxLength(50);
} }
} }

View File

@@ -1,12 +1,12 @@
using Domains.ApplicantDomain; using Domains;
using Microsoft.EntityFrameworkCore; using Domains.ApplicantDomain;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Infrastructure.Database.Applicants.Configuration; namespace Infrastructure.Database.Applicants.Configuration;
public class PassportConfiguration : IEntityTypeConfiguration<Passport> public static class PassportConfiguration<T> where T : class, IEntity
{ {
public void Configure(EntityTypeBuilder<Passport> entity) public static void Configure(OwnedNavigationBuilder<T, Passport> entity)
{ {
entity.Property(p => p.Number) entity.Property(p => p.Number)
.IsUnicode(false) .IsUnicode(false)
@@ -16,4 +16,4 @@ public class PassportConfiguration : IEntityTypeConfiguration<Passport>
.IsUnicode(false) .IsUnicode(false)
.HasMaxLength(200); .HasMaxLength(200);
} }
} }

View File

@@ -8,7 +8,7 @@ public class PlaceOfWorkConfiguration : IEntityTypeConfiguration<PlaceOfWork>
{ {
public void Configure(EntityTypeBuilder<PlaceOfWork> entity) public void Configure(EntityTypeBuilder<PlaceOfWork> entity)
{ {
entity.OwnsOne(p => p.Address); entity.OwnsOne(p => p.Address, AddressConfiguration<PlaceOfWork>.Configure);
entity.Property(p => p.Name) entity.Property(p => p.Name)
.IsUnicode(false) .IsUnicode(false)
@@ -18,4 +18,4 @@ public class PlaceOfWorkConfiguration : IEntityTypeConfiguration<PlaceOfWork>
.IsUnicode(false) .IsUnicode(false)
.HasMaxLength(20); .HasMaxLength(20);
} }
} }

View File

@@ -1,4 +1,4 @@
using ApplicationLayer.Applicants; using ApplicationLayer.Applicants.NeededServices;
using Domains.ApplicantDomain; using Domains.ApplicantDomain;
using Infrastructure.Database.Generic; using Infrastructure.Database.Generic;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;

View File

@@ -4,8 +4,7 @@ using Microsoft.EntityFrameworkCore;
namespace Infrastructure.Database; namespace Infrastructure.Database;
public class DbContext(DbContextOptions<DbContext> opts) public class DbContext(DbContextOptions<DbContext> opts) : Microsoft.EntityFrameworkCore.DbContext(opts), IGenericWriter, IGenericReader, IUnitOfWork
: Microsoft.EntityFrameworkCore.DbContext(opts), IGenericWriter, IGenericReader, IUnitOfWork
{ {
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
@@ -36,4 +35,4 @@ public class DbContext(DbContextOptions<DbContext> opts)
{ {
await SaveChangesAsync(cancellationToken); await SaveChangesAsync(cancellationToken);
} }
} }

View File

@@ -1,10 +1,9 @@
using Domains; using Domains;
namespace Infrastructure.Database.GeneralExceptions namespace Infrastructure.Database.GeneralExceptions;
{
/// Exception to throw when entity not found /// Exception to throw when entity not found
/// <param name="id">Identifier of entity</param> /// <param name="id">Identifier of entity</param>
/// <typeparam name="T">Type of entity</typeparam> /// <typeparam name="T">Type of entity</typeparam>
public class EntityNotFoundByIdException<T>(Guid id) : EntityNotFoundException<T>($"Entity {typeof(T).Name} with id {id} not found.") public class EntityNotFoundByIdException<T>(Guid id) : EntityNotFoundException<T>($"Entity {typeof(T).Name} with id {id} not found.")
where T : class, IEntity; where T : class, IEntity;
}

View File

@@ -1,4 +1,4 @@
using ApplicationLayer.Common; using ApplicationLayer.GeneralNeededServices;
using Domains; using Domains;
using Infrastructure.Database.GeneralExceptions; using Infrastructure.Database.GeneralExceptions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -17,8 +17,8 @@ public abstract class GenericRepository<T>(IGenericReader reader, IGenericWriter
public async Task<List<T>> GetAllAsync(CancellationToken cancellationToken) public async Task<List<T>> GetAllAsync(CancellationToken cancellationToken)
=> await LoadDomain().ToListAsync(cancellationToken); => await LoadDomain().ToListAsync(cancellationToken);
/// <inheritdoc cref="IGenericRepository{T}.GetOneAsync"/> /// <inheritdoc cref="IGenericRepository{T}.GetByIdAsync"/>
public async Task<T> GetOneAsync(Guid id, CancellationToken cancellationToken) public async Task<T> GetByIdAsync(Guid id, CancellationToken cancellationToken)
{ {
var result = await LoadDomain().SingleOrDefaultAsync(a => a.Id == id, cancellationToken); var result = await LoadDomain().SingleOrDefaultAsync(a => a.Id == id, cancellationToken);
return result ?? throw new EntityNotFoundByIdException<T>(id); return result ?? throw new EntityNotFoundByIdException<T>(id);
@@ -31,7 +31,7 @@ public abstract class GenericRepository<T>(IGenericReader reader, IGenericWriter
/// <inheritdoc cref="IGenericRepository{T}.UpdateAsync"/> /// <inheritdoc cref="IGenericRepository{T}.UpdateAsync"/>
public async Task UpdateAsync(T entity, CancellationToken cancellationToken) public async Task UpdateAsync(T entity, CancellationToken cancellationToken)
{ {
await GetOneAsync(entity.Id, cancellationToken); await GetByIdAsync(entity.Id, cancellationToken);
writer.Update(entity); writer.Update(entity);
} }

View File

@@ -1,7 +1,6 @@
using ApplicationLayer.Locations; using ApplicationLayer.Locations.NeededServices;
using Domains.LocationDomain; using Domains.LocationDomain;
using Infrastructure.Database.Generic; using Infrastructure.Database.Generic;
using Infrastructure.Database.Locations.Repositories.Cities.Exceptions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace Infrastructure.Database.Locations.Repositories.Cities; namespace Infrastructure.Database.Locations.Repositories.Cities;
@@ -13,12 +12,4 @@ public sealed class CitiesRepository(IGenericReader reader, IGenericWriter write
{ {
return base.LoadDomain().Include(c => c.Country); return base.LoadDomain().Include(c => c.Country);
} }
///<inheritdoc cref="ICitiesRepository.GetByNameAsync"/>
/// <exception cref="CityNotFoundByNameException">city with provided name and country not found</exception>
public async Task<City> GetByNameAsync(string name, string countryName, CancellationToken cancellationToken)
{
var result = await LoadDomain().Where(c => c.Name == name && c.Country.Name == countryName).SingleOrDefaultAsync(cancellationToken);
return result ?? throw new CityNotFoundByNameException(name, countryName);
}
} }

View File

@@ -1,11 +0,0 @@
using Domains.LocationDomain;
using Infrastructure.Database.GeneralExceptions;
namespace Infrastructure.Database.Locations.Repositories.Cities.Exceptions
{
/// Exception to throw when city cannot be found by its name and its country name
/// <param name="name">Name of the city</param>
/// <param name="countryName">name of the city's country</param>
public class CityNotFoundByNameException(string name, string countryName)
: EntityNotFoundException<City>($"{name} with country {countryName} not found.");
}

View File

@@ -1,4 +1,4 @@
using ApplicationLayer.Locations; using ApplicationLayer.Locations.NeededServices;
using Domains.LocationDomain; using Domains.LocationDomain;
using Infrastructure.Database.Generic; using Infrastructure.Database.Generic;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;

View File

@@ -1,15 +1,15 @@
using Domains.VisaApplicationDomain; using Domains;
using Microsoft.EntityFrameworkCore; using Domains.VisaApplicationDomain;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Infrastructure.Database.VisaApplications.Configuration; namespace Infrastructure.Database.VisaApplications.Configuration;
public class PastVisaConfiguration : IEntityTypeConfiguration<PastVisa> public static class PastVisaConfiguration<T> where T : class, IEntity
{ {
public void Configure(EntityTypeBuilder<PastVisa> entity) public static void Configure(OwnedNavigationBuilder<T, PastVisa> entity)
{ {
entity.Property(p => p.Name) entity.Property(p => p.Name)
.IsUnicode(false) .IsUnicode(false)
.HasMaxLength(70); .HasMaxLength(70);
} }
} }

View File

@@ -0,0 +1,15 @@
using Domains;
using Domains.VisaApplicationDomain;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Infrastructure.Database.VisaApplications.Configuration
{
public static class PastVisitConfiguration<T> where T : class, IEntity
{
public static void Configure(OwnedNavigationBuilder<T, PastVisit> entity)
{
entity.HasOne(p => p.DestinationCountry).WithMany().OnDelete(DeleteBehavior.Restrict);
}
}
}

View File

@@ -1,15 +1,15 @@
using Domains.VisaApplicationDomain; using Domains;
using Microsoft.EntityFrameworkCore; using Domains.VisaApplicationDomain;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Infrastructure.Database.VisaApplications.Configuration; namespace Infrastructure.Database.VisaApplications.Configuration;
public class PermissionToDestCountryConfiguration : IEntityTypeConfiguration<PermissionToDestCountry> public static class PermissionToDestCountryConfiguration<T> where T : class, IEntity
{ {
public void Configure(EntityTypeBuilder<PermissionToDestCountry> entity) public static void Configure(OwnedNavigationBuilder<T, PermissionToDestCountry> entity)
{ {
entity.Property(p => p.Issuer) entity.Property(p => p.Issuer)
.IsUnicode(false) .IsUnicode(false)
.HasMaxLength(200); .HasMaxLength(200);
} }
} }

View File

@@ -1,15 +1,15 @@
using Domains.VisaApplicationDomain; using Domains;
using Microsoft.EntityFrameworkCore; using Domains.VisaApplicationDomain;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Infrastructure.Database.VisaApplications.Configuration; namespace Infrastructure.Database.VisaApplications.Configuration;
public class ReentryPermitConfiguration : IEntityTypeConfiguration<ReentryPermit> public static class ReentryPermitConfiguration<T> where T : class, IEntity
{ {
public void Configure(EntityTypeBuilder<ReentryPermit> entity) public static void Configure(OwnedNavigationBuilder<T, ReentryPermit> entity)
{ {
entity.Property(p => p.Number) entity.Property(p => p.Number)
.IsUnicode(false) .IsUnicode(false)
.HasMaxLength(25); .HasMaxLength(25);
} }
} }

View File

@@ -10,14 +10,16 @@ public class VisaApplicationConfiguration : IEntityTypeConfiguration<VisaApplica
{ {
entity.ToTable("VisaApplications"); entity.ToTable("VisaApplications");
entity.OwnsOne(va => va.ReentryPermit, ReentryPermitConfiguration<VisaApplication>.Configure);
entity.OwnsOne(va => va.PermissionToDestCountry, PermissionToDestCountryConfiguration<VisaApplication>.Configure);
entity.OwnsMany(va => va.PastVisits, PastVisitConfiguration<VisaApplication>.Configure).ToTable("PastVisits");
entity.OwnsMany(va => va.PastVisas).ToTable("PastVisas");
entity.HasOne(va => va.DestinationCountry).WithMany().OnDelete(DeleteBehavior.Restrict);
entity.HasOne(va => va.Applicant) entity.HasOne(va => va.Applicant)
.WithMany(a => a.VisaApplications) .WithMany()
.HasForeignKey(va => va.ApplicantId) .HasForeignKey(va => va.ApplicantId)
.IsRequired(); .IsRequired();
entity.OwnsOne(p => p.ReentryPermit);
entity.OwnsOne(p => p.PermissionToDestCountry);
entity.OwnsMany(p => p.PastVisits).ToTable("PastVisits");
entity.OwnsMany(p => p.PastVisas).ToTable("PastVisas");
} }
} }

View File

@@ -1,4 +1,4 @@
using ApplicationLayer.VisaApplications; using ApplicationLayer.VisaApplications.NeededServices;
using Domains.VisaApplicationDomain; using Domains.VisaApplicationDomain;
using Infrastructure.Database.Generic; using Infrastructure.Database.Generic;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;