Added interfaces to read and write data in DB and added DbContext implementing these
This commit is contained in:
		| @@ -0,0 +1,26 @@ | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| namespace Infrastructure.Database.Configuration.Applicant | ||||
| { | ||||
|     public class ApplicantConfiguration : IEntityTypeConfiguration<Domains.ApplicantDomain.Applicant> | ||||
|     { | ||||
|         public void Configure(EntityTypeBuilder<Domains.ApplicantDomain.Applicant> entity) | ||||
|         { | ||||
|             entity.ToTable("Applicants"); | ||||
|  | ||||
|             entity.OwnsOne(p => p.Name); | ||||
|             entity.OwnsOne(p => p.FatherName); | ||||
|             entity.OwnsOne(p => p.MotherName); | ||||
|             entity.OwnsOne(p => p.Passport); | ||||
|  | ||||
|             entity.Property(p => p.Citizenship) | ||||
|                 .IsUnicode(false) | ||||
|                 .HasMaxLength(30); | ||||
|  | ||||
|             entity.Property(p => p.CitizenshipByBirth) | ||||
|                 .IsUnicode(false) | ||||
|                 .HasMaxLength(30); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,24 @@ | ||||
| using Domains.ApplicantDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| namespace Infrastructure.Database.Configuration.Applicant | ||||
| { | ||||
|     public class NameConfiguration : IEntityTypeConfiguration<Name> | ||||
|     { | ||||
|         public void Configure(EntityTypeBuilder<Name> entity) | ||||
|         { | ||||
|             entity.Property(p => p.FirstName) | ||||
|                 .IsUnicode(false) | ||||
|                 .HasMaxLength(50); | ||||
|  | ||||
|             entity.Property(p => p.Surname) | ||||
|                 .IsUnicode(false) | ||||
|                 .HasMaxLength(50); | ||||
|  | ||||
|             entity.Property(p => p.Patronymic) | ||||
|                 .IsUnicode(false) | ||||
|                 .HasMaxLength(50); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,20 @@ | ||||
| using Domains.ApplicantDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| namespace Infrastructure.Database.Configuration.Applicant | ||||
| { | ||||
|     public class PassportConfiguration : IEntityTypeConfiguration<Passport> | ||||
|     { | ||||
|         public void Configure(EntityTypeBuilder<Passport> entity) | ||||
|         { | ||||
|             entity.Property(p => p.Number) | ||||
|                 .IsUnicode(false) | ||||
|                 .HasMaxLength(20); | ||||
|  | ||||
|             entity.Property(p => p.Issuer) | ||||
|                 .IsUnicode(false) | ||||
|                 .HasMaxLength(200); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,22 @@ | ||||
| using Domains.ApplicantDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| namespace Infrastructure.Database.Configuration.Applicant | ||||
| { | ||||
|     public class PlaceOfWorkConfiguration : IEntityTypeConfiguration<PlaceOfWork> | ||||
|     { | ||||
|         public void Configure(EntityTypeBuilder<PlaceOfWork> entity) | ||||
|         { | ||||
|             entity.OwnsOne(p => p.Address); | ||||
|  | ||||
|             entity.Property(p => p.Name) | ||||
|                 .IsUnicode(false) | ||||
|                 .HasMaxLength(200); | ||||
|  | ||||
|             entity.Property(p => p.PhoneNum) | ||||
|                 .IsUnicode(false) | ||||
|                 .HasMaxLength(20); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,19 @@ | ||||
| using Domains.Common; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| namespace Infrastructure.Database.Configuration.Common | ||||
| { | ||||
|     public class AddressConfiguration : IEntityTypeConfiguration<Address> | ||||
|     { | ||||
|         public void Configure(EntityTypeBuilder<Address> entity) | ||||
|         { | ||||
|             entity.Property(p => p.Street) | ||||
|                 .IsUnicode(false) | ||||
|                 .HasMaxLength(100); | ||||
|             entity.Property(p => p.Building) | ||||
|                 .IsUnicode(false) | ||||
|                 .HasMaxLength(10); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,16 @@ | ||||
| using Domains.Common; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| namespace Infrastructure.Database.Configuration.Common | ||||
| { | ||||
|     public class CityConfiguration : IEntityTypeConfiguration<City> | ||||
|     { | ||||
|         public void Configure(EntityTypeBuilder<City> entity) | ||||
|         { | ||||
|             entity.Property(p => p.Name) | ||||
|                 .IsUnicode(false) | ||||
|                 .HasMaxLength(70); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,16 @@ | ||||
| using Domains.Common; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| namespace Infrastructure.Database.Configuration.Common | ||||
| { | ||||
|     public class CountryConfiguration : IEntityTypeConfiguration<Country> | ||||
|     { | ||||
|         public void Configure(EntityTypeBuilder<Country> entity) | ||||
|         { | ||||
|             entity.Property(p => p.Name) | ||||
|                 .IsUnicode(false) | ||||
|                 .HasMaxLength(70); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,16 @@ | ||||
| using Domains.VisaApplicationDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| namespace Infrastructure.Database.Configuration.VisaApplication | ||||
| { | ||||
|     public class PastVisaConfiguration : IEntityTypeConfiguration<PastVisa> | ||||
|     { | ||||
|         public void Configure(EntityTypeBuilder<PastVisa> entity) | ||||
|         { | ||||
|             entity.Property(p => p.Name) | ||||
|                 .IsUnicode(false) | ||||
|                 .HasMaxLength(70); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,16 @@ | ||||
| using Domains.VisaApplicationDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| namespace Infrastructure.Database.Configuration.VisaApplication | ||||
| { | ||||
|     public class PermissionToDestCountryConfiguration : IEntityTypeConfiguration<PermissionToDestCountry> | ||||
|     { | ||||
|         public void Configure(EntityTypeBuilder<PermissionToDestCountry> entity) | ||||
|         { | ||||
|             entity.Property(p => p.Issuer) | ||||
|                 .IsUnicode(false) | ||||
|                 .HasMaxLength(200); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,16 @@ | ||||
| using Domains.VisaApplicationDomain; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||||
|  | ||||
| namespace Infrastructure.Database.Configuration.VisaApplication | ||||
| { | ||||
|     public class ReentryPermitConfiguration : IEntityTypeConfiguration<ReentryPermit> | ||||
|     { | ||||
|         public void Configure(EntityTypeBuilder<ReentryPermit> entity) | ||||
|         { | ||||
|             entity.Property(p => p.Number) | ||||
|                 .IsUnicode(false) | ||||
|                 .HasMaxLength(25); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,17 @@ | ||||
| using Domains.VisaApplicationDomain; | ||||
| 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); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										45
									
								
								SchengenVisaApi/Infrastructure/Database/DbContext.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								SchengenVisaApi/Infrastructure/Database/DbContext.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,45 @@ | ||||
| using System.Reflection; | ||||
| using Domains; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace Infrastructure.Database | ||||
| { | ||||
|     public class DbContext : Microsoft.EntityFrameworkCore.DbContext, IWriter, IReader, IUnitOfWork | ||||
|     { | ||||
|         protected override void OnModelCreating(ModelBuilder modelBuilder) | ||||
|         { | ||||
|             modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly()); | ||||
|         } | ||||
|  | ||||
|         async Task IWriter.AddAsync<T>(T entity, CancellationToken cancellationToken) | ||||
|         { | ||||
|             await AddAsync(entity, cancellationToken); | ||||
|         } | ||||
|  | ||||
|         void IWriter.Update<T>(T entity) | ||||
|         { | ||||
|             Update(entity); | ||||
|         } | ||||
|  | ||||
|         void IWriter.Remove<T>(T entity) | ||||
|         { | ||||
|             Remove(entity); | ||||
|         } | ||||
|  | ||||
|         IQueryable<T> IReader.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); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										18
									
								
								SchengenVisaApi/Infrastructure/Database/IReader.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								SchengenVisaApi/Infrastructure/Database/IReader.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| 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; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										9
									
								
								SchengenVisaApi/Infrastructure/Database/IUnitOfWork.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								SchengenVisaApi/Infrastructure/Database/IUnitOfWork.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| namespace Infrastructure.Database | ||||
| { | ||||
|     public interface IUnitOfWork | ||||
|     { | ||||
|         /// Save changes in data storage | ||||
|         /// <param name="cancellationToken">Cancellation Token</param> | ||||
|         Task SaveAsync(CancellationToken cancellationToken); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										25
									
								
								SchengenVisaApi/Infrastructure/Database/IWriter.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								SchengenVisaApi/Infrastructure/Database/IWriter.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | ||||
| using Domains; | ||||
|  | ||||
| namespace Infrastructure.Database | ||||
| { | ||||
|     /// Writes data to data storage | ||||
|     /// <remarks><see cref="IUnitOfWork"/> should be used to save changes</remarks> | ||||
|     public interface IWriter | ||||
|     { | ||||
|         /// Add <paramref name="entity"/> to data storage | ||||
|         /// <param name="entity">Entity to add</param> | ||||
|         /// <param name="cancellationToken">Cancellation Token</param> | ||||
|         /// <typeparam name="T">Entity type</typeparam> | ||||
|         Task AddAsync<T>(T entity, CancellationToken cancellationToken) where T : class, IEntity; | ||||
|  | ||||
|         /// Update <paramref name="entity"/> in data storage | ||||
|         /// <param name="entity">Entity to update</param> | ||||
|         /// <typeparam name="T">Entity type</typeparam> | ||||
|         void Update<T>(T entity) where T : class, IEntity; | ||||
|  | ||||
|         /// Remove <paramref name="entity"/> from data storage | ||||
|         /// <param name="entity">Entity to remove</param> | ||||
|         /// <typeparam name="T">Entity type</typeparam> | ||||
|         void Remove<T>(T entity) where T : class, IEntity; | ||||
|     } | ||||
| } | ||||
| @@ -10,4 +10,13 @@ | ||||
|       <ProjectReference Include="..\ApplicationLayer\ApplicationLayer.csproj" /> | ||||
|     </ItemGroup> | ||||
|  | ||||
|     <ItemGroup> | ||||
|       <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0-preview.7.24405.3" /> | ||||
|       <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