Changed domains, configured links between VisaApplication and Applicant,created generic repository and repositories for domains
This commit is contained in:
		| @@ -1,8 +1,8 @@ | ||||
| using Domains.Common; | ||||
| using Domains.ApplicantDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
| 
 | ||||
| namespace Infrastructure.Database.Configuration.Common | ||||
| namespace Infrastructure.Database.Applicants.Configuration | ||||
| { | ||||
|     public class AddressConfiguration : IEntityTypeConfiguration<Address> | ||||
|     { | ||||
| @@ -1,11 +1,12 @@ | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Domains.ApplicantDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
| 
 | ||||
| namespace Infrastructure.Database.Configuration.Applicant | ||||
| namespace Infrastructure.Database.Applicants.Configuration | ||||
| { | ||||
|     public class ApplicantConfiguration : IEntityTypeConfiguration<Domains.ApplicantDomain.Applicant> | ||||
|     public class ApplicantConfiguration : IEntityTypeConfiguration<Applicant> | ||||
|     { | ||||
|         public void Configure(EntityTypeBuilder<Domains.ApplicantDomain.Applicant> entity) | ||||
|         public void Configure(EntityTypeBuilder<Applicant> entity) | ||||
|         { | ||||
|             entity.ToTable("Applicants"); | ||||
| 
 | ||||
| @@ -2,7 +2,7 @@ | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
| 
 | ||||
| namespace Infrastructure.Database.Configuration.Applicant | ||||
| namespace Infrastructure.Database.Applicants.Configuration | ||||
| { | ||||
|     public class NameConfiguration : IEntityTypeConfiguration<Name> | ||||
|     { | ||||
| @@ -2,7 +2,7 @@ | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
| 
 | ||||
| namespace Infrastructure.Database.Configuration.Applicant | ||||
| namespace Infrastructure.Database.Applicants.Configuration | ||||
| { | ||||
|     public class PassportConfiguration : IEntityTypeConfiguration<Passport> | ||||
|     { | ||||
| @@ -2,7 +2,7 @@ | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
| 
 | ||||
| namespace Infrastructure.Database.Configuration.Applicant | ||||
| namespace Infrastructure.Database.Applicants.Configuration | ||||
| { | ||||
|     public class PlaceOfWorkConfiguration : IEntityTypeConfiguration<PlaceOfWork> | ||||
|     { | ||||
| @@ -0,0 +1,18 @@ | ||||
| using Domains.ApplicantDomain; | ||||
| using Infrastructure.Database.Generic; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace Infrastructure.Database.Applicants.Repositories | ||||
| { | ||||
|     public class ApplicantsRepository(IGenericReader reader, IGenericWriter writer, IUnitOfWork unitOfWork) | ||||
|         : GenericRepository<Applicant>(writer, unitOfWork), IApplicantsRepository | ||||
|     { | ||||
|         protected override IQueryable<Applicant> LoadDomain() | ||||
|         { | ||||
|             return reader.GetAll<Applicant>() | ||||
|                 .Include(a => a.CountryOfBirth) | ||||
|                 .Include(a => a.CityOfBirth) | ||||
|                 .Include(a => a.PlaceOfWork); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,7 @@ | ||||
| using Domains.ApplicantDomain; | ||||
| using Infrastructure.Database.Generic; | ||||
|  | ||||
| namespace Infrastructure.Database.Applicants.Repositories | ||||
| { | ||||
|     public interface IApplicantsRepository : IGenericRepository<Applicant> { } | ||||
| } | ||||
| @@ -1,16 +0,0 @@ | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| namespace Infrastructure.Database.Configuration.VisaApplication | ||||
| { | ||||
|     public class VisaApplicationConfiguration : IEntityTypeConfiguration<Domains.VisaApplicationDomain.VisaApplication> | ||||
|     { | ||||
|         public void Configure(EntityTypeBuilder<Domains.VisaApplicationDomain.VisaApplication> entity) | ||||
|         { | ||||
|             entity.ToTable("VisaApplications"); | ||||
|  | ||||
|             entity.OwnsOne(p => p.ReentryPermit); | ||||
|             entity.OwnsOne(p => p.PermissionToDestCountry); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -1,41 +1,36 @@ | ||||
| using System.Reflection; | ||||
| using Infrastructure.Database.Generic; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace Infrastructure.Database | ||||
| { | ||||
|     public class DbContext : Microsoft.EntityFrameworkCore.DbContext, IWriter, IReader, IUnitOfWork | ||||
|     public class DbContext : Microsoft.EntityFrameworkCore.DbContext, IGenericWriter, IGenericReader, IUnitOfWork | ||||
|     { | ||||
|         protected override void OnModelCreating(ModelBuilder modelBuilder) | ||||
|         { | ||||
|             modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly()); | ||||
|         } | ||||
|  | ||||
|         async Task IWriter.AddAsync<T>(T entity, CancellationToken cancellationToken) | ||||
|         async Task IGenericWriter.AddAsync<T>(T entity, CancellationToken cancellationToken) | ||||
|         { | ||||
|             await AddAsync(entity, cancellationToken); | ||||
|         } | ||||
|  | ||||
|         void IWriter.Update<T>(T entity) | ||||
|         void IGenericWriter.Update<T>(T entity) | ||||
|         { | ||||
|             Update(entity); | ||||
|         } | ||||
|  | ||||
|         void IWriter.Remove<T>(T entity) | ||||
|         void IGenericWriter.Remove<T>(T entity) | ||||
|         { | ||||
|             Remove(entity); | ||||
|         } | ||||
|  | ||||
|         IQueryable<T> IReader.GetAll<T>() | ||||
|         IQueryable<T> IGenericReader.GetAll<T>() | ||||
|         { | ||||
|             return Set<T>(); | ||||
|         } | ||||
|  | ||||
|         async Task<T?> IReader.GetOneAsync<T>(Guid id, CancellationToken cancellationToken) | ||||
|             where T : class | ||||
|         { | ||||
|             return await Set<T>().FindAsync([id], cancellationToken: cancellationToken); | ||||
|         } | ||||
|  | ||||
|         async Task IUnitOfWork.SaveAsync(CancellationToken cancellationToken) | ||||
|         { | ||||
|             await SaveChangesAsync(cancellationToken); | ||||
|   | ||||
| @@ -0,0 +1,4 @@ | ||||
| namespace Infrastructure.Database.GeneralExceptions | ||||
| { | ||||
|     public class EntityNotFoundException<T>(Guid id) : Exception($"Entity {typeof(T).Name} with id '{id}' not found"); | ||||
| } | ||||
| @@ -0,0 +1,38 @@ | ||||
| using Domains; | ||||
| using Infrastructure.Database.GeneralExceptions; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace Infrastructure.Database.Generic | ||||
| { | ||||
|     public abstract class GenericRepository<T>(IGenericWriter writer, IUnitOfWork unitOfWork) : IGenericRepository<T> | ||||
|         where T : class, IEntity | ||||
|     { | ||||
|         public async Task<List<T>> GetAllAsync(CancellationToken cancellationToken) | ||||
|             => await LoadDomain().ToListAsync(cancellationToken); | ||||
|  | ||||
|         public async Task<T> GetOneAsync(Guid id, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var result = await LoadDomain().SingleOrDefaultAsync(a => a.Id == id, cancellationToken); | ||||
|             return result ?? throw new EntityNotFoundException<T>(id); | ||||
|         } | ||||
|  | ||||
|         public async Task AddAsync(T entity, CancellationToken cancellationToken) | ||||
|             => await writer.AddAsync(entity, cancellationToken); | ||||
|  | ||||
|         public async Task UpdateAsync(T entity, CancellationToken cancellationToken) | ||||
|         { | ||||
|             await GetOneAsync(entity.Id, cancellationToken); | ||||
|             writer.Update(entity); | ||||
|         } | ||||
|  | ||||
|         public void Remove(T entity) | ||||
|         { | ||||
|             writer.Remove(entity); | ||||
|         } | ||||
|  | ||||
|         public async Task SaveAsync(CancellationToken cancellationToken) | ||||
|             => await unitOfWork.SaveAsync(cancellationToken); | ||||
|  | ||||
|         protected abstract IQueryable<T> LoadDomain(); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,11 @@ | ||||
| using Domains; | ||||
|  | ||||
| namespace Infrastructure.Database.Generic | ||||
| { | ||||
|     public interface IGenericReader | ||||
|     { | ||||
|         /// Get all entities of type <typeparamref name="T"/> stored in storage | ||||
|         /// <typeparam name="T">Entity type to seek in storage</typeparam> | ||||
|         IQueryable<T> GetAll<T>() where T : class, IEntity; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,19 @@ | ||||
| using Domains; | ||||
|  | ||||
| namespace Infrastructure.Database.Generic | ||||
| { | ||||
|     public interface IGenericRepository<T> where T : class, IEntity | ||||
|     { | ||||
|         Task<List<T>> GetAllAsync(CancellationToken cancellationToken); | ||||
|  | ||||
|         Task<T> GetOneAsync(Guid id, CancellationToken cancellationToken); | ||||
|  | ||||
|         Task AddAsync(T entity, CancellationToken cancellationToken); | ||||
|  | ||||
|         Task UpdateAsync(T entity, CancellationToken cancellationToken); | ||||
|  | ||||
|         void Remove(T entity); | ||||
|  | ||||
|         Task SaveAsync(CancellationToken cancellationToken); | ||||
|     } | ||||
| } | ||||
| @@ -1,10 +1,10 @@ | ||||
| using Domains; | ||||
| 
 | ||||
| namespace Infrastructure.Database | ||||
| namespace Infrastructure.Database.Generic | ||||
| { | ||||
|     /// Writes data to data storage | ||||
|     /// <remarks><see cref="IUnitOfWork"/> should be used to save changes</remarks> | ||||
|     public interface IWriter | ||||
|     public interface IGenericWriter | ||||
|     { | ||||
|         /// Add <paramref name="entity"/> to data storage | ||||
|         /// <param name="entity">Entity to add</param> | ||||
| @@ -1,18 +0,0 @@ | ||||
| using Domains; | ||||
|  | ||||
| namespace Infrastructure.Database | ||||
| { | ||||
|     public interface IReader | ||||
|     { | ||||
|         /// Get all entities of type <typeparamref name="T"/> stored in storage | ||||
|         /// <typeparam name="T">Entity type to seek in storage</typeparam> | ||||
|         IQueryable<T> GetAll<T>() where T : class, IEntity; | ||||
|  | ||||
|         /// Get one entity with specific <paramref name="id"/> from storage | ||||
|         /// <param name="id">Identifier of entity</param> | ||||
|         /// <param name="cancellationToken">Cancellation Token</param> | ||||
|         /// <typeparam name="T">Type of entity</typeparam> | ||||
|         /// <returns>Entity or null if not found</returns> | ||||
|         Task<T?> GetOneAsync<T>(Guid id, CancellationToken cancellationToken) where T : class, IEntity; | ||||
|     } | ||||
| } | ||||
| @@ -1,8 +1,8 @@ | ||||
| using Domains.Common; | ||||
| using Domains.LocationDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
| 
 | ||||
| namespace Infrastructure.Database.Configuration.Common | ||||
| namespace Infrastructure.Database.Locations.Configuration | ||||
| { | ||||
|     public class CityConfiguration : IEntityTypeConfiguration<City> | ||||
|     { | ||||
| @@ -1,8 +1,8 @@ | ||||
| using Domains.Common; | ||||
| using Domains.LocationDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
| 
 | ||||
| namespace Infrastructure.Database.Configuration.Common | ||||
| namespace Infrastructure.Database.Locations.Configuration | ||||
| { | ||||
|     public class CountryConfiguration : IEntityTypeConfiguration<Country> | ||||
|     { | ||||
| @@ -0,0 +1,15 @@ | ||||
| using Domains.LocationDomain; | ||||
| using Infrastructure.Database.Generic; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace Infrastructure.Database.Locations.Repositories.Cities | ||||
| { | ||||
|     public class CitiesRepository(IGenericReader reader, IGenericWriter writer, IUnitOfWork unitOfWork) | ||||
|         : GenericRepository<City>(writer, unitOfWork), ICitiesRepository | ||||
|     { | ||||
|         protected override IQueryable<City> LoadDomain() | ||||
|         { | ||||
|             return reader.GetAll<City>().Include(c => c.Country); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,7 @@ | ||||
| using Domains.LocationDomain; | ||||
| using Infrastructure.Database.Generic; | ||||
|  | ||||
| namespace Infrastructure.Database.Locations.Repositories.Cities | ||||
| { | ||||
|     public interface ICitiesRepository : IGenericRepository<City> { } | ||||
| } | ||||
| @@ -0,0 +1,15 @@ | ||||
| using Domains.LocationDomain; | ||||
| using Infrastructure.Database.Generic; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace Infrastructure.Database.Locations.Repositories.Countries | ||||
| { | ||||
|     public class CountriesRepository(IGenericReader reader, IGenericWriter writer, IUnitOfWork unitOfWork) | ||||
|         : GenericRepository<Country>(writer, unitOfWork), ICountriesRepository | ||||
|     { | ||||
|         protected override IQueryable<Country> LoadDomain() | ||||
|         { | ||||
|             return reader.GetAll<Country>().Include(c => c.Cities); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,7 @@ | ||||
| using Domains.LocationDomain; | ||||
| using Infrastructure.Database.Generic; | ||||
|  | ||||
| namespace Infrastructure.Database.Locations.Repositories.Countries | ||||
| { | ||||
|     public interface ICountriesRepository : IGenericRepository<Country> { } | ||||
| } | ||||
| @@ -2,7 +2,7 @@ | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
| 
 | ||||
| namespace Infrastructure.Database.Configuration.VisaApplication | ||||
| namespace Infrastructure.Database.VisaApplications.Configuration | ||||
| { | ||||
|     public class PastVisaConfiguration : IEntityTypeConfiguration<PastVisa> | ||||
|     { | ||||
| @@ -2,7 +2,7 @@ | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
| 
 | ||||
| namespace Infrastructure.Database.Configuration.VisaApplication | ||||
| namespace Infrastructure.Database.VisaApplications.Configuration | ||||
| { | ||||
|     public class PermissionToDestCountryConfiguration : IEntityTypeConfiguration<PermissionToDestCountry> | ||||
|     { | ||||
| @@ -2,7 +2,7 @@ | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
| 
 | ||||
| namespace Infrastructure.Database.Configuration.VisaApplication | ||||
| namespace Infrastructure.Database.VisaApplications.Configuration | ||||
| { | ||||
|     public class ReentryPermitConfiguration : IEntityTypeConfiguration<ReentryPermit> | ||||
|     { | ||||
| @@ -0,0 +1,22 @@ | ||||
| using Domains.VisaApplicationDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| namespace Infrastructure.Database.VisaApplications.Configuration | ||||
| { | ||||
|     public class VisaApplicationConfiguration : IEntityTypeConfiguration<VisaApplication> | ||||
|     { | ||||
|         public void Configure(EntityTypeBuilder<VisaApplication> entity) | ||||
|         { | ||||
|             entity.ToTable("VisaApplications"); | ||||
|  | ||||
|             entity.HasOne(va => va.Applicant) | ||||
|                 .WithMany(a => a.VisaApplications) | ||||
|                 .HasForeignKey(va => va.ApplicantId) | ||||
|                 .IsRequired(); | ||||
|  | ||||
|             entity.OwnsOne(p => p.ReentryPermit); | ||||
|             entity.OwnsOne(p => p.PermissionToDestCountry); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,7 @@ | ||||
| using Domains.VisaApplicationDomain; | ||||
| using Infrastructure.Database.Generic; | ||||
|  | ||||
| namespace Infrastructure.Database.VisaApplications.Repositories | ||||
| { | ||||
|     public interface IVisaApplicationsRepository : IGenericRepository<VisaApplication> { } | ||||
| } | ||||
| @@ -0,0 +1,18 @@ | ||||
| using Domains.VisaApplicationDomain; | ||||
| using Infrastructure.Database.Generic; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace Infrastructure.Database.VisaApplications.Repositories | ||||
| { | ||||
|     public class VisaApplicationsRepository(IGenericReader reader, IGenericWriter writer, IUnitOfWork unitOfWork) | ||||
|         : GenericRepository<VisaApplication>(writer, unitOfWork), IVisaApplicationsRepository | ||||
|     { | ||||
|         protected override IQueryable<VisaApplication> LoadDomain() | ||||
|         { | ||||
|             return reader.GetAll<VisaApplication>() | ||||
|                 .Include(a => a.DestinationCountry) | ||||
|                 .Include(a => a.PastVisas) | ||||
|                 .Include(a => a.PastVisits); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -15,8 +15,4 @@ | ||||
|       <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.0-preview.7.24405.3" /> | ||||
|     </ItemGroup> | ||||
|  | ||||
|     <ItemGroup> | ||||
|       <Folder Include="Database\Repositories\" /> | ||||
|     </ItemGroup> | ||||
|  | ||||
| </Project> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user