Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vanderlan committed Oct 6, 2023
1 parent b51a120 commit 0bd7173
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Orion.Api/Controllers/CustomersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions Orion.Data/Repository/Generic/BaseEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public async Task<T> AddAsync(T entity)
public async Task DeleteAsync(string publicId)
{
var existing = await GetByIdAsync(publicId);

if (existing != null)
{
DataContext.ChangeTracker.Clear();
DataContext.Set<T>().Remove(existing);
}
}
Expand All @@ -46,6 +48,7 @@ public async Task<IEnumerable<T>> SearchByAsync(Expression<Func<T, bool>> predic

public void Update(T entity)
{
DataContext.ChangeTracker.Clear();
DataContext.Entry(entity).State = EntityState.Modified;
DataContext.Set<T>().Update(entity);
}
Expand Down
1 change: 0 additions & 1 deletion Orion.Data/UnitOfWork/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ protected virtual void Dispose(bool disposing)
{
if (!_disposed && disposing)
{
DbContext.Dispose();
_disposed = true;
}
}
Expand Down
16 changes: 6 additions & 10 deletions Orion.Domain/Implementation/CustomerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,23 @@ public CustomerService(IUnitOfWork unitOfWork)

public async Task<Customer> 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)
{
throw new NotFoundException(publicId);
}

await unitOfWork.CustomerRepository.DeleteAsync(publicId);
await unitOfWork.CommitAsync();
await _unitOfWork.CustomerRepository.DeleteAsync(publicId);
await _unitOfWork.CommitAsync();
}

public async Task<Customer> FindByIdAsync(string publicId)
Expand All @@ -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();
}
}
}
36 changes: 13 additions & 23 deletions Orion.Domain/Implementation/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ public UserService(IUnitOfWork unitOfWork, IStringLocalizer<OrionResources> reso

public async Task<User> 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;
}
Expand All @@ -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<User> FindByIdAsync(string publicId)
Expand All @@ -71,31 +67,27 @@ public async Task<User> LoginAsync(string email, string password)

public async Task UpdateAsync(User user)
{
using var unitOfWork = _unitOfWork;

var entitySaved = await FindByIdAsync(user.PublicId);

await ValidateUser(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<RefreshToken> 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;
}
Expand All @@ -110,18 +102,16 @@ public async Task<User> 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]);
}
Expand Down
10 changes: 3 additions & 7 deletions Orion.Test/Services/UserServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,13 @@ public async Task AddAsync_DuplicatedUser_ThrowsConflictException()
var userService = scope.ServiceProvider.GetService<IUserService>();

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<ConflictException>(() => userService.AddAsync(UserMotherObject.ValidAdminUser()));

await userService.DeleteAsync(userFound.PublicId);
await userService.DeleteAsync(userSaved.PublicId);
}

[Fact]
public async Task ListPaginateAsync_WithFilterByName_GetAllMatchedUsers()
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 0bd7173

Please sign in to comment.