Fixed configurations of owned types
This commit is contained in:
		| @@ -1,18 +1,23 @@ | ||||
| using Domains.ApplicantDomain; | ||||
| using Domains; | ||||
| using Domains.ApplicantDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| 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) | ||||
|             .IsUnicode(false) | ||||
|             .HasMaxLength(100); | ||||
|  | ||||
|         entity.Property(p => p.Building) | ||||
|             .IsUnicode(false) | ||||
|             .HasMaxLength(10); | ||||
|     } | ||||
| } | ||||
| } | ||||
|   | ||||
| @@ -9,11 +9,13 @@ public class ApplicantConfiguration : IEntityTypeConfiguration<Applicant> | ||||
|     public void Configure(EntityTypeBuilder<Applicant> entity) | ||||
|     { | ||||
|         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.OwnsOne(p => p.FatherName); | ||||
|         entity.OwnsOne(p => p.MotherName); | ||||
|         entity.OwnsOne(p => p.Passport); | ||||
|         entity.HasOne(a => a.CityOfBirth).WithMany().OnDelete(DeleteBehavior.Restrict); | ||||
|         entity.HasOne(a => a.CountryOfBirth).WithMany().OnDelete(DeleteBehavior.Restrict); | ||||
|  | ||||
|         entity.Property(p => p.Citizenship) | ||||
|             .IsUnicode(false) | ||||
| @@ -23,4 +25,4 @@ public class ApplicantConfiguration : IEntityTypeConfiguration<Applicant> | ||||
|             .IsUnicode(false) | ||||
|             .HasMaxLength(30); | ||||
|     } | ||||
| } | ||||
| } | ||||
|   | ||||
| @@ -1,12 +1,12 @@ | ||||
| using Domains.ApplicantDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Domains; | ||||
| using Domains.ApplicantDomain; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| 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) | ||||
|             .IsUnicode(false) | ||||
| @@ -20,4 +20,4 @@ public class NameConfiguration : IEntityTypeConfiguration<Name> | ||||
|             .IsUnicode(false) | ||||
|             .HasMaxLength(50); | ||||
|     } | ||||
| } | ||||
| } | ||||
|   | ||||
| @@ -1,12 +1,12 @@ | ||||
| using Domains.ApplicantDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Domains; | ||||
| using Domains.ApplicantDomain; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| 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) | ||||
|             .IsUnicode(false) | ||||
| @@ -16,4 +16,4 @@ public class PassportConfiguration : IEntityTypeConfiguration<Passport> | ||||
|             .IsUnicode(false) | ||||
|             .HasMaxLength(200); | ||||
|     } | ||||
| } | ||||
| } | ||||
|   | ||||
| @@ -8,7 +8,7 @@ public class PlaceOfWorkConfiguration : IEntityTypeConfiguration<PlaceOfWork> | ||||
| { | ||||
|     public void Configure(EntityTypeBuilder<PlaceOfWork> entity) | ||||
|     { | ||||
|         entity.OwnsOne(p => p.Address); | ||||
|         entity.OwnsOne(p => p.Address, AddressConfiguration<PlaceOfWork>.Configure); | ||||
|  | ||||
|         entity.Property(p => p.Name) | ||||
|             .IsUnicode(false) | ||||
| @@ -18,4 +18,4 @@ public class PlaceOfWorkConfiguration : IEntityTypeConfiguration<PlaceOfWork> | ||||
|             .IsUnicode(false) | ||||
|             .HasMaxLength(20); | ||||
|     } | ||||
| } | ||||
| } | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| using ApplicationLayer.Applicants; | ||||
| using ApplicationLayer.Applicants.NeededServices; | ||||
| using Domains.ApplicantDomain; | ||||
| using Infrastructure.Database.Generic; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|   | ||||
| @@ -4,8 +4,7 @@ using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace Infrastructure.Database; | ||||
|  | ||||
| public class DbContext(DbContextOptions<DbContext> opts) | ||||
|     : Microsoft.EntityFrameworkCore.DbContext(opts), IGenericWriter, IGenericReader, IUnitOfWork | ||||
| public class DbContext(DbContextOptions<DbContext> opts) : Microsoft.EntityFrameworkCore.DbContext(opts), IGenericWriter, IGenericReader, IUnitOfWork | ||||
| { | ||||
|     protected override void OnModelCreating(ModelBuilder modelBuilder) | ||||
|     { | ||||
| @@ -36,4 +35,4 @@ public class DbContext(DbContextOptions<DbContext> opts) | ||||
|     { | ||||
|         await SaveChangesAsync(cancellationToken); | ||||
|     } | ||||
| } | ||||
| } | ||||
|   | ||||
| @@ -1,10 +1,9 @@ | ||||
| using Domains; | ||||
|  | ||||
| namespace Infrastructure.Database.GeneralExceptions | ||||
| { | ||||
|     /// Exception to throw when entity not found | ||||
|     /// <param name="id">Identifier of entity</param> | ||||
|     /// <typeparam name="T">Type of entity</typeparam> | ||||
|     public class EntityNotFoundByIdException<T>(Guid id) : EntityNotFoundException<T>($"Entity {typeof(T).Name} with id {id} not found.") | ||||
|         where T : class, IEntity; | ||||
| } | ||||
| namespace Infrastructure.Database.GeneralExceptions; | ||||
|  | ||||
| /// Exception to throw when entity not found | ||||
| /// <param name="id">Identifier of entity</param> | ||||
| /// <typeparam name="T">Type of entity</typeparam> | ||||
| public class EntityNotFoundByIdException<T>(Guid id) : EntityNotFoundException<T>($"Entity {typeof(T).Name} with id {id} not found.") | ||||
|     where T : class, IEntity; | ||||
| @@ -1,4 +1,4 @@ | ||||
| using ApplicationLayer.Common; | ||||
| using ApplicationLayer.GeneralNeededServices; | ||||
| using Domains; | ||||
| using Infrastructure.Database.GeneralExceptions; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| @@ -17,8 +17,8 @@ public abstract class GenericRepository<T>(IGenericReader reader, IGenericWriter | ||||
|     public async Task<List<T>> GetAllAsync(CancellationToken cancellationToken) | ||||
|         => await LoadDomain().ToListAsync(cancellationToken); | ||||
|  | ||||
|     /// <inheritdoc cref="IGenericRepository{T}.GetOneAsync"/> | ||||
|     public async Task<T> GetOneAsync(Guid id, CancellationToken cancellationToken) | ||||
|     /// <inheritdoc cref="IGenericRepository{T}.GetByIdAsync"/> | ||||
|     public async Task<T> GetByIdAsync(Guid id, CancellationToken cancellationToken) | ||||
|     { | ||||
|         var result = await LoadDomain().SingleOrDefaultAsync(a => a.Id == id, cancellationToken); | ||||
|         return result ?? throw new EntityNotFoundByIdException<T>(id); | ||||
| @@ -31,7 +31,7 @@ public abstract class GenericRepository<T>(IGenericReader reader, IGenericWriter | ||||
|     /// <inheritdoc cref="IGenericRepository{T}.UpdateAsync"/> | ||||
|     public async Task UpdateAsync(T entity, CancellationToken cancellationToken) | ||||
|     { | ||||
|         await GetOneAsync(entity.Id, cancellationToken); | ||||
|         await GetByIdAsync(entity.Id, cancellationToken); | ||||
|         writer.Update(entity); | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
| using ApplicationLayer.Locations; | ||||
| using ApplicationLayer.Locations.NeededServices; | ||||
| using Domains.LocationDomain; | ||||
| using Infrastructure.Database.Generic; | ||||
| using Infrastructure.Database.Locations.Repositories.Cities.Exceptions; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| 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); | ||||
|     } | ||||
|  | ||||
|     ///<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); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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."); | ||||
| } | ||||
| @@ -1,4 +1,4 @@ | ||||
| using ApplicationLayer.Locations; | ||||
| using ApplicationLayer.Locations.NeededServices; | ||||
| using Domains.LocationDomain; | ||||
| using Infrastructure.Database.Generic; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|   | ||||
| @@ -1,15 +1,15 @@ | ||||
| using Domains.VisaApplicationDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Domains; | ||||
| using Domains.VisaApplicationDomain; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| 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) | ||||
|             .IsUnicode(false) | ||||
|             .HasMaxLength(70); | ||||
|     } | ||||
| } | ||||
| } | ||||
|   | ||||
| @@ -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); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -1,15 +1,15 @@ | ||||
| using Domains.VisaApplicationDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Domains; | ||||
| using Domains.VisaApplicationDomain; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| 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) | ||||
|             .IsUnicode(false) | ||||
|             .HasMaxLength(200); | ||||
|     } | ||||
| } | ||||
| } | ||||
|   | ||||
| @@ -1,15 +1,15 @@ | ||||
| using Domains.VisaApplicationDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Domains; | ||||
| using Domains.VisaApplicationDomain; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| 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) | ||||
|             .IsUnicode(false) | ||||
|             .HasMaxLength(25); | ||||
|     } | ||||
| } | ||||
| } | ||||
|   | ||||
| @@ -10,14 +10,16 @@ public class VisaApplicationConfiguration : IEntityTypeConfiguration<VisaApplica | ||||
|     { | ||||
|         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) | ||||
|             .WithMany(a => a.VisaApplications) | ||||
|             .WithMany() | ||||
|             .HasForeignKey(va => va.ApplicantId) | ||||
|             .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"); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| using ApplicationLayer.VisaApplications; | ||||
| using ApplicationLayer.VisaApplications.NeededServices; | ||||
| using Domains.VisaApplicationDomain; | ||||
| using Infrastructure.Database.Generic; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user