From 0bd71734e1d55bddb07bc39ca1068bbe887df847 Mon Sep 17 00:00:00 2001 From: 83585-Vanderlan Silva Date: Fri, 6 Oct 2023 18:17:13 -0300 Subject: [PATCH] Minor fixes --- Orion.Api/Controllers/CustomersController.cs | 2 +- .../Generic/BaseEntityRepository.cs | 3 ++ Orion.Data/UnitOfWork/UnitOfWork.cs | 1 - .../Implementation/CustomerService.cs | 16 ++++----- Orion.Domain/Implementation/UserService.cs | 36 +++++++------------ Orion.Test/Services/UserServiceTest.cs | 10 ++---- 6 files changed, 26 insertions(+), 42 deletions(-) diff --git a/Orion.Api/Controllers/CustomersController.cs b/Orion.Api/Controllers/CustomersController.cs index 9d66584..481108b 100644 --- a/Orion.Api/Controllers/CustomersController.cs +++ b/Orion.Api/Controllers/CustomersController.cs @@ -14,7 +14,7 @@ namespace Orion.Api.Controllers { [ApiVersion("1.0")] [Route("api/[controller]")] - [AuthorizeFor(Roles.Customer)] + [AuthorizeFor(Roles.Admin, Roles.Customer)] public class CustomersController : ApiController { private readonly ICustomerService _customerService; diff --git a/Orion.Data/Repository/Generic/BaseEntityRepository.cs b/Orion.Data/Repository/Generic/BaseEntityRepository.cs index fce7dbc..66de50d 100644 --- a/Orion.Data/Repository/Generic/BaseEntityRepository.cs +++ b/Orion.Data/Repository/Generic/BaseEntityRepository.cs @@ -28,8 +28,10 @@ public async Task AddAsync(T entity) public async Task DeleteAsync(string publicId) { var existing = await GetByIdAsync(publicId); + if (existing != null) { + DataContext.ChangeTracker.Clear(); DataContext.Set().Remove(existing); } } @@ -46,6 +48,7 @@ public async Task> SearchByAsync(Expression> predic public void Update(T entity) { + DataContext.ChangeTracker.Clear(); DataContext.Entry(entity).State = EntityState.Modified; DataContext.Set().Update(entity); } diff --git a/Orion.Data/UnitOfWork/UnitOfWork.cs b/Orion.Data/UnitOfWork/UnitOfWork.cs index a120e9d..a1539b1 100644 --- a/Orion.Data/UnitOfWork/UnitOfWork.cs +++ b/Orion.Data/UnitOfWork/UnitOfWork.cs @@ -66,7 +66,6 @@ protected virtual void Dispose(bool disposing) { if (!_disposed && disposing) { - DbContext.Dispose(); _disposed = true; } } diff --git a/Orion.Domain/Implementation/CustomerService.cs b/Orion.Domain/Implementation/CustomerService.cs index 9ebef97..39303da 100644 --- a/Orion.Domain/Implementation/CustomerService.cs +++ b/Orion.Domain/Implementation/CustomerService.cs @@ -19,18 +19,14 @@ public CustomerService(IUnitOfWork unitOfWork) public async Task AddAsync(Customer entity) { - using var unitOfWork = _unitOfWork; - - var added = await unitOfWork.CustomerRepository.AddAsync(entity); - await unitOfWork.CommitAsync(); + var added = await _unitOfWork.CustomerRepository.AddAsync(entity); + await _unitOfWork.CommitAsync(); return added; } public async Task DeleteAsync(string publicId) { - using var unitOfWork = _unitOfWork; - var item = await FindByIdAsync(publicId); if (item == null) @@ -38,8 +34,8 @@ public async Task DeleteAsync(string publicId) throw new NotFoundException(publicId); } - await unitOfWork.CustomerRepository.DeleteAsync(publicId); - await unitOfWork.CommitAsync(); + await _unitOfWork.CustomerRepository.DeleteAsync(publicId); + await _unitOfWork.CommitAsync(); } public async Task FindByIdAsync(string publicId) @@ -61,8 +57,8 @@ public async Task UpdateAsync(Customer entity) entitySaved.Name = entity.Name; entitySaved.PublicId = entity.PublicId; - unitOfWork.CustomerRepository.Update(entitySaved); - await unitOfWork.CommitAsync(); + _unitOfWork.CustomerRepository.Update(entitySaved); + await _unitOfWork.CommitAsync(); } } } diff --git a/Orion.Domain/Implementation/UserService.cs b/Orion.Domain/Implementation/UserService.cs index 900a0b0..caa8fd0 100644 --- a/Orion.Domain/Implementation/UserService.cs +++ b/Orion.Domain/Implementation/UserService.cs @@ -26,14 +26,12 @@ public UserService(IUnitOfWork unitOfWork, IStringLocalizer reso public async Task AddAsync(User user) { - using var unitOfWork = _unitOfWork; - await ValidateUser(user); user.Password = user.Password.ToSha512(); - var added = await unitOfWork.UserRepository.AddAsync(user); - await unitOfWork.CommitAsync(); + var added = await _unitOfWork.UserRepository.AddAsync(user); + await _unitOfWork.CommitAsync(); return added; } @@ -51,10 +49,8 @@ private async Task ValidateUser(User user) public async Task DeleteAsync(string publicId) { - using var unitOfWork = _unitOfWork; - - await unitOfWork.UserRepository.DeleteAsync(publicId); - await unitOfWork.CommitAsync(); + await _unitOfWork.UserRepository.DeleteAsync(publicId); + await _unitOfWork.CommitAsync(); } public async Task FindByIdAsync(string publicId) @@ -71,8 +67,6 @@ public async Task LoginAsync(string email, string password) public async Task UpdateAsync(User user) { - using var unitOfWork = _unitOfWork; - var entitySaved = await FindByIdAsync(user.PublicId); await ValidateUser(user); @@ -80,22 +74,20 @@ public async Task UpdateAsync(User user) entitySaved.Email = user.Email; entitySaved.Name = user.Name; - unitOfWork.UserRepository.Update(entitySaved); + _unitOfWork.UserRepository.Update(entitySaved); - await unitOfWork.CommitAsync(); + await _unitOfWork.CommitAsync(); } public async Task AddRefreshTokenAsync(RefreshToken refreshToken) { - using var unitOfWork = _unitOfWork; - - var existantRefresToken = await unitOfWork.RefreshTokenRepository.SearchByAsync(x => x.Email == refreshToken.Email); + var existantRefresToken = await _unitOfWork.RefreshTokenRepository.SearchByAsync(x => x.Email == refreshToken.Email); if (existantRefresToken.Any()) return existantRefresToken.First(); - var added = await unitOfWork.RefreshTokenRepository.AddAsync(refreshToken); - await unitOfWork.CommitAsync(); + var added = await _unitOfWork.RefreshTokenRepository.AddAsync(refreshToken); + await _unitOfWork.CommitAsync(); return added; } @@ -110,18 +102,16 @@ public async Task GetUserByRefreshTokenAsync(string refreshToken) ); } - using var unitOfWork = _unitOfWork; - - var token = await unitOfWork.RefreshTokenRepository.SearchByAsync(x => x.Refreshtoken.Equals(refreshToken)); + var token = await _unitOfWork.RefreshTokenRepository.SearchByAsync(x => x.Refreshtoken.Equals(refreshToken)); if (token != null && token.Any()) { - var user = await unitOfWork.UserRepository.SearchByAsync(x => x.Email == token.First().Email); + var user = await _unitOfWork.UserRepository.SearchByAsync(x => x.Email == token.First().Email); if (user.Any()) { - await unitOfWork.RefreshTokenRepository.DeleteAsync(token.First().PublicId); - await unitOfWork.CommitAsync(); + await _unitOfWork.RefreshTokenRepository.DeleteAsync(token.First().PublicId); + await _unitOfWork.CommitAsync(); return user?.First() ?? throw new UnauthorizedUserException(_messages[UserMessages.InvalidRefreshToken], _messages[ExceptionsTitles.AuthenticationError]); } diff --git a/Orion.Test/Services/UserServiceTest.cs b/Orion.Test/Services/UserServiceTest.cs index 9ca82f1..775a46f 100644 --- a/Orion.Test/Services/UserServiceTest.cs +++ b/Orion.Test/Services/UserServiceTest.cs @@ -53,17 +53,13 @@ public async Task AddAsync_DuplicatedUser_ThrowsConflictException() var userService = scope.ServiceProvider.GetService(); var userSaved = await userService.AddAsync(UserMotherObject.ValidAdminUser()); - var userFound = await userService.FindByIdAsync(userSaved.PublicId); - - Assert.NotNull(userFound); - Assert.Equal(UserMotherObject.ValidAdminUser().Password.ToSha512(), userFound.Password); - Assert.Equal(userFound.Name, UserMotherObject.ValidAdminUser().Name); //act && assert await Assert.ThrowsAsync(() => userService.AddAsync(UserMotherObject.ValidAdminUser())); - await userService.DeleteAsync(userFound.PublicId); + await userService.DeleteAsync(userSaved.PublicId); } + [Fact] public async Task ListPaginateAsync_WithFilterByName_GetAllMatchedUsers() { @@ -149,7 +145,7 @@ public async Task UpdateAsync_WithValidData_UpdateUserAsSuccess() //assert Assert.Equal(userFound.Email, userEdited.Email); - Assert.Equal(userFound.Password, userEdited.Password); + Assert.Equal(userFound.Password.ToSha512(), userEdited.Password); Assert.Equal(userFound.Name, userEdited.Name); await userService.DeleteAsync(userFound.PublicId);