Admin controller, Locations controller, requests to add available countries, request to get available countries
This commit is contained in:
		| @@ -0,0 +1,28 @@ | ||||
| using ApplicationLayer.DataAccessingServices.AuthServices.LoginService.Exceptions; | ||||
| using ApplicationLayer.DataAccessingServices.AuthServices.NeededServices; | ||||
| using ApplicationLayer.DataAccessingServices.AuthServices.Requests; | ||||
| using Domains.Users; | ||||
|  | ||||
| namespace ApplicationLayer.DataAccessingServices.AuthServices.LoginService | ||||
| { | ||||
|     public class DevelopmentLoginService(IUsersRepository users, ITokenGenerator tokenGenerator) : ILoginService | ||||
|     { | ||||
|         async Task<string> ILoginService.LoginAsync(UserLoginRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             if (request is { Email: "admin@mail.ru", Password: "admin" }) | ||||
|             { | ||||
|                 var admin = new User { Role = Role.Admin }; | ||||
|  | ||||
|                 return tokenGenerator.CreateToken(admin); | ||||
|             } | ||||
|  | ||||
|             var user = await users.FindByEmailAsync(request.Email, cancellationToken); | ||||
|             if (user is null || user.Password != request.Password) | ||||
|             { | ||||
|                 throw new IncorrectLoginDataException(); | ||||
|             } | ||||
|  | ||||
|             return tokenGenerator.CreateToken(user); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,6 @@ | ||||
| using ApplicationLayer.GeneralExceptions; | ||||
|  | ||||
| namespace ApplicationLayer.DataAccessingServices.AuthServices.LoginService.Exceptions | ||||
| { | ||||
|     public class IncorrectLoginDataException() : ApiException("Incorrect email or password"); | ||||
| } | ||||
| @@ -0,0 +1,12 @@ | ||||
| using ApplicationLayer.DataAccessingServices.AuthServices.Requests; | ||||
|  | ||||
| namespace ApplicationLayer.DataAccessingServices.AuthServices.LoginService | ||||
| { | ||||
|     /// Handles <see cref="UserLoginRequest"/> | ||||
|     public interface ILoginService | ||||
|     { | ||||
|         /// Handle <see cref="UserLoginRequest"/> | ||||
|         /// <returns>JWT-token</returns> | ||||
|         Task<string> LoginAsync(UserLoginRequest request, CancellationToken cancellationToken); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,21 @@ | ||||
| using ApplicationLayer.DataAccessingServices.AuthServices.LoginService.Exceptions; | ||||
| using ApplicationLayer.DataAccessingServices.AuthServices.NeededServices; | ||||
| using ApplicationLayer.DataAccessingServices.AuthServices.Requests; | ||||
|  | ||||
| namespace ApplicationLayer.DataAccessingServices.AuthServices.LoginService | ||||
| { | ||||
|     /// <inheritdoc cref="ILoginService"/> | ||||
|     public class LoginService(IUsersRepository users, ITokenGenerator tokenGenerator) : ILoginService | ||||
|     { | ||||
|         async Task<string> ILoginService.LoginAsync(UserLoginRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var user = await users.FindByEmailAsync(request.Email, cancellationToken); | ||||
|             if (user is null || user.Password != request.Password) | ||||
|             { | ||||
|                 throw new IncorrectLoginDataException(); | ||||
|             } | ||||
|  | ||||
|             return tokenGenerator.CreateToken(user); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,9 @@ | ||||
| using Domains.Users; | ||||
|  | ||||
| namespace ApplicationLayer.DataAccessingServices.AuthServices.NeededServices | ||||
| { | ||||
|     public interface ITokenGenerator | ||||
|     { | ||||
|         string CreateToken(User user); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,15 @@ | ||||
| using ApplicationLayer.GeneralNeededServices; | ||||
| using Domains.Users; | ||||
|  | ||||
| namespace ApplicationLayer.DataAccessingServices.AuthServices.NeededServices | ||||
| { | ||||
|     /// Repository pattern for <see cref="User"/> | ||||
|     public interface IUsersRepository : IGenericRepository<User> | ||||
|     { | ||||
|         /// Find <see cref="User"/> by email | ||||
|         /// <param name="email"><see cref="User"/>'s email</param> | ||||
|         /// <param name="cancellationToken">Cancellation token</param> | ||||
|         /// <returns>User or null if not found</returns> | ||||
|         Task<User?> FindByEmailAsync(string email, CancellationToken cancellationToken); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,7 @@ | ||||
| using ApplicationLayer.DataAccessingServices.AuthServices.Requests; | ||||
| using ApplicationLayer.GeneralExceptions; | ||||
|  | ||||
| namespace ApplicationLayer.DataAccessingServices.AuthServices.RegisterService.Exceptions | ||||
| { | ||||
|     public class UserAlreadyExistsException(RegisterApplicantRequest request) : AlreadyExistsException($"User with email '{request.Email}' already exists"); | ||||
| } | ||||
| @@ -0,0 +1,11 @@ | ||||
| using ApplicationLayer.DataAccessingServices.AuthServices.Requests; | ||||
|  | ||||
| namespace ApplicationLayer.DataAccessingServices.AuthServices.RegisterService | ||||
| { | ||||
|     /// Handles <see cref="RegisterApplicantRequest"/> | ||||
|     public interface IRegisterService | ||||
|     { | ||||
|         /// Handle <see cref="RegisterApplicantRequest"/> | ||||
|         Task Register(RegisterApplicantRequest request, CancellationToken cancellationToken); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,71 @@ | ||||
| using ApplicationLayer.DataAccessingServices.Applicants.NeededServices; | ||||
| using ApplicationLayer.DataAccessingServices.AuthServices.NeededServices; | ||||
| using ApplicationLayer.DataAccessingServices.AuthServices.RegisterService.Exceptions; | ||||
| using ApplicationLayer.DataAccessingServices.AuthServices.Requests; | ||||
| using ApplicationLayer.DataAccessingServices.Locations.NeededServices; | ||||
| using ApplicationLayer.GeneralNeededServices; | ||||
| using Domains.ApplicantDomain; | ||||
| using Domains.Users; | ||||
|  | ||||
| namespace ApplicationLayer.DataAccessingServices.AuthServices.RegisterService | ||||
| { | ||||
|     /// <inheritdoc cref="IRegisterService"/> | ||||
|     public class RegisterService( | ||||
|         IUsersRepository users, | ||||
|         IApplicantsRepository applicants, | ||||
|         ICitiesRepository cities, | ||||
|         IUnitOfWork unitOfWork) : IRegisterService | ||||
|     { | ||||
|         async Task IRegisterService.Register(RegisterApplicantRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             if (await users.FindByEmailAsync(request.Email, cancellationToken) is not null) | ||||
|             { | ||||
|                 throw new UserAlreadyExistsException(request); | ||||
|             } | ||||
|  | ||||
|             //TODO mapper | ||||
|             var user = new User { Email = request.Email, Password = request.Password, Role = Role.Applicant }; | ||||
|  | ||||
|             var applicantCity = await cities.GetByIdAsync(request.CityOfBirthId, cancellationToken); | ||||
|             var placeOfWorkCity = await cities.GetByIdAsync(request.PlaceOfWork.Address.CityId, cancellationToken); | ||||
|             var placeOfWorkAddress = new Address | ||||
|             { | ||||
|                 Country = placeOfWorkCity.Country, | ||||
|                 City = placeOfWorkCity, | ||||
|                 Building = request.PlaceOfWork.Address.Building, | ||||
|                 Street = request.PlaceOfWork.Address.Street | ||||
|             }; | ||||
|  | ||||
|             var placeOfWork = new PlaceOfWork | ||||
|             { | ||||
|                 Name = request.PlaceOfWork.Name, | ||||
|                 Address = placeOfWorkAddress, | ||||
|                 PhoneNum = request.PlaceOfWork.PhoneNum | ||||
|             }; | ||||
|  | ||||
|             var applicant = new Applicant | ||||
|             { | ||||
|                 Citizenship = request.Citizenship, | ||||
|                 CitizenshipByBirth = request.CitizenshipByBirth, | ||||
|                 Gender = request.Gender, | ||||
|                 Name = request.ApplicantName, | ||||
|                 Passport = request.Passport, | ||||
|                 BirthDate = request.BirthDate, | ||||
|                 FatherName = request.FatherName, | ||||
|                 JobTitle = request.JobTitle, | ||||
|                 MaritalStatus = request.MaritalStatus, | ||||
|                 MotherName = request.MotherName, | ||||
|                 UserId = user.Id, | ||||
|                 CityOfBirth = applicantCity, | ||||
|                 CountryOfBirth = applicantCity.Country, | ||||
|                 IsNonResident = request.IsNonResident, | ||||
|                 PlaceOfWork = placeOfWork | ||||
|             }; | ||||
|  | ||||
|             await users.AddAsync(user, cancellationToken); | ||||
|             await applicants.AddAsync(applicant, cancellationToken); | ||||
|  | ||||
|             await unitOfWork.SaveAsync(cancellationToken); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,22 @@ | ||||
| using ApplicationLayer.DataAccessingServices.Applicants.Models; | ||||
| using Domains.ApplicantDomain; | ||||
|  | ||||
| namespace ApplicationLayer.DataAccessingServices.AuthServices.Requests | ||||
| { | ||||
|     public record RegisterApplicantRequest( | ||||
|         string Email, | ||||
|         string Password, | ||||
|         Name ApplicantName, | ||||
|         Passport Passport, | ||||
|         DateTime BirthDate, | ||||
|         Guid CityOfBirthId, | ||||
|         string Citizenship, | ||||
|         string CitizenshipByBirth, | ||||
|         Gender Gender, | ||||
|         MaritalStatus MaritalStatus, | ||||
|         Name FatherName, | ||||
|         Name MotherName, | ||||
|         string JobTitle, | ||||
|         PlaceOfWorkModel PlaceOfWork, | ||||
|         bool IsNonResident); | ||||
| } | ||||
| @@ -0,0 +1,4 @@ | ||||
| namespace ApplicationLayer.DataAccessingServices.AuthServices.Requests | ||||
| { | ||||
|     public record UserLoginRequest(string Email, string Password); | ||||
| } | ||||
		Reference in New Issue
	
	Block a user