From c92855e7ce1723e78968b083d7c6d58998ac344a Mon Sep 17 00:00:00 2001 From: prtsie Date: Fri, 23 Aug 2024 20:09:41 +0300 Subject: [PATCH] Added mapper --- .../RegisterService/IRegisterService.cs | 2 +- .../RegisterService/RegisterService.cs | 14 +-- .../VisaApplicationRequestsHandler.cs | 85 ++++--------------- SchengenVisaApi/Domains/Domains.csproj | 4 + .../Automapper/Profiles/ApplicantProfile.cs | 14 +++ .../Automapper/Profiles/UserProfile.cs | 20 +++++ .../Profiles/VisaApplicationProfile.cs | 25 ++++++ .../Infrastructure/DependencyInjection.cs | 5 +- .../Controllers/UsersController.cs | 3 +- 9 files changed, 93 insertions(+), 79 deletions(-) create mode 100644 SchengenVisaApi/Infrastructure/Automapper/Profiles/ApplicantProfile.cs create mode 100644 SchengenVisaApi/Infrastructure/Automapper/Profiles/UserProfile.cs create mode 100644 SchengenVisaApi/Infrastructure/Automapper/Profiles/VisaApplicationProfile.cs diff --git a/SchengenVisaApi/ApplicationLayer/Services/AuthServices/RegisterService/IRegisterService.cs b/SchengenVisaApi/ApplicationLayer/Services/AuthServices/RegisterService/IRegisterService.cs index 5d1f407..52d69e4 100644 --- a/SchengenVisaApi/ApplicationLayer/Services/AuthServices/RegisterService/IRegisterService.cs +++ b/SchengenVisaApi/ApplicationLayer/Services/AuthServices/RegisterService/IRegisterService.cs @@ -6,7 +6,7 @@ namespace ApplicationLayer.Services.AuthServices.RegisterService public interface IRegisterService { /// Handle - Task Register(RegisterApplicantRequest request, CancellationToken cancellationToken); + Task RegisterApplicant(RegisterApplicantRequest request, CancellationToken cancellationToken); /// Handles and adds approving authority account Task RegisterAuthority(RegisterRequest request, CancellationToken cancellationToken); diff --git a/SchengenVisaApi/ApplicationLayer/Services/AuthServices/RegisterService/RegisterService.cs b/SchengenVisaApi/ApplicationLayer/Services/AuthServices/RegisterService/RegisterService.cs index 68cedc8..6315d9d 100644 --- a/SchengenVisaApi/ApplicationLayer/Services/AuthServices/RegisterService/RegisterService.cs +++ b/SchengenVisaApi/ApplicationLayer/Services/AuthServices/RegisterService/RegisterService.cs @@ -3,6 +3,7 @@ using ApplicationLayer.Services.Applicants.NeededServices; using ApplicationLayer.Services.AuthServices.NeededServices; using ApplicationLayer.Services.AuthServices.RegisterService.Exceptions; using ApplicationLayer.Services.AuthServices.Requests; +using AutoMapper; using Domains.ApplicantDomain; using Domains.Users; @@ -12,9 +13,10 @@ namespace ApplicationLayer.Services.AuthServices.RegisterService public class RegisterService( IUsersRepository users, IApplicantsRepository applicants, - IUnitOfWork unitOfWork) : IRegisterService + IUnitOfWork unitOfWork, + IMapper mapper) : IRegisterService { - async Task IRegisterService.Register(RegisterApplicantRequest request, CancellationToken cancellationToken) + async Task IRegisterService.RegisterApplicant(RegisterApplicantRequest request, CancellationToken cancellationToken) { //todo move to validation layer if (await users.FindByEmailAsync(request.Email, cancellationToken) is not null) @@ -22,8 +24,8 @@ namespace ApplicationLayer.Services.AuthServices.RegisterService throw new UserAlreadyExistsException(request); } - //TODO mapper - var user = new User { Email = request.Email, Password = request.Password, Role = Role.Applicant }; + var user = mapper.Map(request); + user.Role = Role.Applicant; var applicant = new Applicant { @@ -58,8 +60,8 @@ namespace ApplicationLayer.Services.AuthServices.RegisterService throw new UserAlreadyExistsException(request); } - //TODO mapper - var user = new User { Email = request.Email, Password = request.Password, Role = Role.ApprovingAuthority }; + var user = mapper.Map(request); + user.Role = Role.ApprovingAuthority; await users.AddAsync(user, cancellationToken); diff --git a/SchengenVisaApi/ApplicationLayer/Services/VisaApplications/Handlers/VisaApplicationRequestsHandler.cs b/SchengenVisaApi/ApplicationLayer/Services/VisaApplications/Handlers/VisaApplicationRequestsHandler.cs index 88a6b27..f2dba9a 100644 --- a/SchengenVisaApi/ApplicationLayer/Services/VisaApplications/Handlers/VisaApplicationRequestsHandler.cs +++ b/SchengenVisaApi/ApplicationLayer/Services/VisaApplications/Handlers/VisaApplicationRequestsHandler.cs @@ -5,6 +5,7 @@ using ApplicationLayer.Services.VisaApplications.Exceptions; using ApplicationLayer.Services.VisaApplications.Models; using ApplicationLayer.Services.VisaApplications.NeededServices; using ApplicationLayer.Services.VisaApplications.Requests; +using AutoMapper; using Domains.VisaApplicationDomain; namespace ApplicationLayer.Services.VisaApplications.Handlers; @@ -13,13 +14,14 @@ namespace ApplicationLayer.Services.VisaApplications.Handlers; public class VisaApplicationRequestsHandler( IVisaApplicationsRepository applications, IApplicantsRepository applicants, - IUnitOfWork unitOfWork) : IVisaApplicationRequestsHandler + IUnitOfWork unitOfWork, + IMapper mapper, + IDateTimeProvider dateTimeProvider) : IVisaApplicationRequestsHandler { async Task> IVisaApplicationRequestsHandler.GetAllAsync(CancellationToken cancellationToken) { var applicationsList = await applications.GetAllAsync(cancellationToken); - //todo mapper var applicationModels = applicationsList .Select(a => MapVisaApplicationToModelForAuthorities(a, cancellationToken).Result) .ToList(); @@ -30,85 +32,28 @@ public class VisaApplicationRequestsHandler( CancellationToken cancellationToken) { var applicant = await applicants.GetByIdAsync(visaApplication.ApplicantId, cancellationToken); - var applicantModel = new ApplicantModel - { - Citizenship = applicant.Citizenship, - Gender = applicant.Gender, - Name = applicant.Name, - Passport = applicant.Passport, - BirthDate = applicant.BirthDate, - FatherName = applicant.FatherName, - JobTitle = applicant.JobTitle, - MaritalStatus = applicant.MaritalStatus, - MotherName = applicant.MotherName, - CitizenshipByBirth = applicant.CitizenshipByBirth, - CityOfBirth = applicant.CityOfBirth, - CountryOfBirth = applicant.CountryOfBirth, - IsNonResident = applicant.IsNonResident, - PlaceOfWork = applicant.PlaceOfWork, - }; - return new VisaApplicationModelForAuthority - { - PastVisits = visaApplication.PastVisits, - ReentryPermit = visaApplication.ReentryPermit, - VisaCategory = visaApplication.VisaCategory, - PermissionToDestCountry = visaApplication.PermissionToDestCountry, - DestinationCountry = visaApplication.DestinationCountry, - PastVisas = visaApplication.PastVisas, - RequestDate = visaApplication.RequestDate, - ValidDaysRequested = visaApplication.ValidDaysRequested, - RequestedNumberOfEntries = visaApplication.RequestedNumberOfEntries, - ForGroup = visaApplication.ForGroup, - Applicant = applicantModel, - Id = visaApplication.Id, - Status = visaApplication.Status - }; + var applicantModel = mapper.Map(applicant); + + var model = mapper.Map(visaApplication); + model.Applicant = applicantModel; + + return model; } public async Task> GetForApplicantAsync(Guid userId, CancellationToken cancellationToken) { - //todo mapper var applicantId = await applicants.GetApplicantIdByUserId(userId, cancellationToken); var visaApplications = await applications.GetOfApplicantAsync(applicantId, cancellationToken); - return visaApplications.Select(va => new VisaApplicationModelForApplicant - { - DestinationCountry = va.DestinationCountry, - ValidDaysRequested = va.ValidDaysRequested, - ReentryPermit = va.ReentryPermit, - VisaCategory = va.VisaCategory, - RequestedNumberOfEntries = va.RequestedNumberOfEntries, - PermissionToDestCountry = va.PermissionToDestCountry, - ForGroup = va.ForGroup, - PastVisas = va.PastVisas, - RequestDate = va.RequestDate, - PastVisits = va.PastVisits, - Id = va.Id, - Status = va.Status - }) - .ToList(); + return mapper.Map>(visaApplications); } public async Task HandleCreateRequestAsync(Guid userId, VisaApplicationCreateRequest request, CancellationToken cancellationToken) { - //TODO mapper - var applicant = await applicants.FindByUserIdAsync(userId, cancellationToken); - var visaApplication = new VisaApplication - { - ApplicantId = applicant.Id, - RequestedNumberOfEntries = request.RequestedNumberOfEntries, - ValidDaysRequested = request.ValidDaysRequested, - ReentryPermit = request.ReentryPermit, - VisaCategory = request.VisaCategory, - PermissionToDestCountry = request.PermissionToDestCountry, - DestinationCountry = request.DestinationCountry, - PastVisas = request.PastVisas.ToList(), - PastVisits = request.PastVisits.ToList(), - ForGroup = request.IsForGroup, - RequestDate = DateTime.Today, - Status = ApplicationStatus.Pending - }; + var visaApplication = mapper.Map(request); + visaApplication.RequestDate = dateTimeProvider.Now(); + visaApplication.ApplicantId = applicant.Id; await applications.AddAsync(visaApplication, cancellationToken); @@ -138,7 +83,7 @@ public class VisaApplicationRequestsHandler( throw new ApplicationAlreadyProcessedException(); } - //todo mapper + //todo handle exception or not ApplicationStatus statusToSet = status switch { AuthorityRequestStatuses.Approved => ApplicationStatus.Approved, diff --git a/SchengenVisaApi/Domains/Domains.csproj b/SchengenVisaApi/Domains/Domains.csproj index 3a63532..c6dfae8 100644 --- a/SchengenVisaApi/Domains/Domains.csproj +++ b/SchengenVisaApi/Domains/Domains.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/SchengenVisaApi/Infrastructure/Automapper/Profiles/ApplicantProfile.cs b/SchengenVisaApi/Infrastructure/Automapper/Profiles/ApplicantProfile.cs new file mode 100644 index 0000000..c0f3d5c --- /dev/null +++ b/SchengenVisaApi/Infrastructure/Automapper/Profiles/ApplicantProfile.cs @@ -0,0 +1,14 @@ +using ApplicationLayer.Services.Applicants.Models; +using AutoMapper; +using Domains.ApplicantDomain; + +namespace Infrastructure.Automapper.Profiles +{ + public class ApplicantProfile : Profile + { + public ApplicantProfile() + { + CreateMap(MemberList.Destination); + } + } +} diff --git a/SchengenVisaApi/Infrastructure/Automapper/Profiles/UserProfile.cs b/SchengenVisaApi/Infrastructure/Automapper/Profiles/UserProfile.cs new file mode 100644 index 0000000..f995bfa --- /dev/null +++ b/SchengenVisaApi/Infrastructure/Automapper/Profiles/UserProfile.cs @@ -0,0 +1,20 @@ +using ApplicationLayer.Services.AuthServices.Requests; +using AutoMapper; +using Domains.Users; + +namespace Infrastructure.Automapper.Profiles +{ + public class UserProfile : Profile + { + public UserProfile() + { + CreateMap(MemberList.Destination) + .ForMember(u => u.Role, + opts => opts.Ignore()); + + CreateMap() + .ForMember(u => u.Role, + opts => opts.Ignore()); + } + } +} diff --git a/SchengenVisaApi/Infrastructure/Automapper/Profiles/VisaApplicationProfile.cs b/SchengenVisaApi/Infrastructure/Automapper/Profiles/VisaApplicationProfile.cs new file mode 100644 index 0000000..d274a03 --- /dev/null +++ b/SchengenVisaApi/Infrastructure/Automapper/Profiles/VisaApplicationProfile.cs @@ -0,0 +1,25 @@ +using ApplicationLayer.Services.VisaApplications.Models; +using ApplicationLayer.Services.VisaApplications.Requests; +using AutoMapper; +using Domains.VisaApplicationDomain; + +namespace Infrastructure.Automapper.Profiles +{ + public class VisaApplicationProfile : Profile + { + public VisaApplicationProfile() + { + CreateMap(MemberList.Destination); + + CreateMap(MemberList.Destination) + .ForMember(model => model.Applicant, + opts => opts.Ignore()); + + CreateMap(MemberList.Destination) + .ForMember(va => va.RequestDate, + opts => opts.Ignore()) + .ForMember(va => va.ApplicantId, + opts => opts.Ignore()); + } + } +} diff --git a/SchengenVisaApi/Infrastructure/DependencyInjection.cs b/SchengenVisaApi/Infrastructure/DependencyInjection.cs index a05b816..300168a 100644 --- a/SchengenVisaApi/Infrastructure/DependencyInjection.cs +++ b/SchengenVisaApi/Infrastructure/DependencyInjection.cs @@ -1,4 +1,5 @@ -using ApplicationLayer.InfrastructureServicesInterfaces; +using System.Reflection; +using ApplicationLayer.InfrastructureServicesInterfaces; using ApplicationLayer.Services.Applicants.NeededServices; using ApplicationLayer.Services.AuthServices.NeededServices; using ApplicationLayer.Services.VisaApplications.NeededServices; @@ -37,6 +38,8 @@ public static class DependencyInjection services.AddSingleton(); + services.AddAutoMapper(Assembly.GetExecutingAssembly()); + return services; } } diff --git a/SchengenVisaApi/SchengenVisaApi/Controllers/UsersController.cs b/SchengenVisaApi/SchengenVisaApi/Controllers/UsersController.cs index ac0201b..2bf2f55 100644 --- a/SchengenVisaApi/SchengenVisaApi/Controllers/UsersController.cs +++ b/SchengenVisaApi/SchengenVisaApi/Controllers/UsersController.cs @@ -24,7 +24,7 @@ namespace SchengenVisaApi.Controllers [Route("register")] public async Task Register(RegisterApplicantRequest request, CancellationToken cancellationToken) { - await registerService.Register(request, cancellationToken); + await registerService.RegisterApplicant(request, cancellationToken); return Ok(); } @@ -62,6 +62,7 @@ namespace SchengenVisaApi.Controllers [ProducesResponseType(StatusCodes.Status401Unauthorized)] [Route("authorities")] [Authorize(policy: PolicyConstants.AdminPolicy)] + //todo return models public async Task GetAuthorityAccounts(CancellationToken cancellationToken) { var result = await authorityService.GetAuthoritiesAccountsAsync(cancellationToken);