Skip to content

Commit

Permalink
refactor: Update repository update calls to use SaveChangesAsync()
Browse files Browse the repository at this point in the history
  • Loading branch information
foxminchan committed Aug 30, 2024
1 parent 7f91619 commit 23c8735
Show file tree
Hide file tree
Showing 12 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public async Task<Result> Handle(DeleteBookCommand request, CancellationToken ca

book.Delete();

await repository.UpdateAsync(book, cancellationToken);
await repository.SaveChangesAsync(cancellationToken);

return Result.Success();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public async Task<Result> Handle(RemoveBookImageCommand request, CancellationTok

await azurite.DeleteFileAsync(book.ImageUrl!, cancellationToken);
book.RemoveImage();
await repository.UpdateAsync(book, cancellationToken);
await repository.SaveChangesAsync(cancellationToken);

return Result.Success();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task<Result> Handle(UpdateBookCommand request, CancellationToken ca
book.Embed(embedding);
}

await repository.UpdateAsync(book, cancellationToken);
await repository.SaveChangesAsync(cancellationToken);

return Result.Success();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task Consume(ConsumeContext<FeedbackCreatedIntegrationEvent> contex

try
{
await repository.UpdateAsync(book);
await repository.SaveChangesAsync();
}
catch (Exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task Consume(ConsumeContext<FeedbackDeletedIntegrationEvent> contex

book.RemoveRating(@event.Rating);

await repository.UpdateAsync(book);
await repository.SaveChangesAsync();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public async Task<Result> Handle(CancelOrderCommand request, CancellationToken c

order.MarkAsCanceled();

await repository.UpdateAsync(order, cancellationToken);
await repository.SaveChangesAsync(cancellationToken);

var email = identityService.GetEmail();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public async Task<Result> Handle(CompleteOrderCommand request, CancellationToken

order.MarkAsCompleted();

await repository.UpdateAsync(order, cancellationToken);
await repository.SaveChangesAsync(cancellationToken);

var email = identityService.GetEmail();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task GivenRequest_ShouldDeleteBook_WhenBookExists()
x => x.GetByIdAsync(It.IsAny<Guid>(), It.IsAny<CancellationToken>()),
Times.Once);
_bookRepositoryMock.Verify(
x => x.UpdateAsync(It.IsAny<Book>(), It.IsAny<CancellationToken>()),
x => x.SaveChangesAsync(It.IsAny<CancellationToken>()),
Times.Once);
}

Expand All @@ -63,7 +63,7 @@ public async Task GivenRequest_ShouldReturnNotFound_WhenBookDoesNotExist()
x => x.GetByIdAsync(It.IsAny<Guid>(), It.IsAny<CancellationToken>()),
Times.Once);
_bookRepositoryMock.Verify(
x => x.UpdateAsync(It.IsAny<Book>(), It.IsAny<CancellationToken>()),
x => x.SaveChangesAsync(It.IsAny<CancellationToken>()),
Times.Never);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ public async Task GivenValidRemoveBookImageCommand_ShouldRemoveBookImageAndRetur
_azuriteMock.Setup(a => a.DeleteFileAsync(book.ImageUrl!, It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask);

_repositoryMock.Setup(r => r.UpdateAsync(book, It.IsAny<CancellationToken>()))
.Returns(Task.CompletedTask);

var command = new RemoveBookImageCommand(bookId);

// Act
Expand Down Expand Up @@ -61,7 +58,7 @@ public async Task GivenNonExistentBookId_ShouldThrowNotFoundException()

_repositoryMock.Verify(r => r.GetByIdAsync(bookId, It.IsAny<CancellationToken>()), Times.Once);
_azuriteMock.Verify(a => a.DeleteFileAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never);
_repositoryMock.Verify(r => r.UpdateAsync(It.IsAny<Book>(), It.IsAny<CancellationToken>()), Times.Never);
_repositoryMock.Verify(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Never);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task GivenValidUpdateBookCommand_ShouldReturnSuccess()
// Assert
result.IsSuccess.Should().BeTrue();
_repositoryMock.Verify(r => r.GetByIdAsync(bookId, It.IsAny<CancellationToken>()), Times.Once);
_repositoryMock.Verify(r => r.UpdateAsync(book, It.IsAny<CancellationToken>()), Times.Once);
_repositoryMock.Verify(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);

book.Name.Should().Be("Updated Name");
book.Description.Should().Be("Updated Description");
Expand Down Expand Up @@ -73,7 +73,7 @@ public async Task GivenNonExistentBookId_ShouldThrowNotFoundException()
await act.Should().ThrowAsync<NotFoundException>();

_repositoryMock.Verify(r => r.GetByIdAsync(bookId, It.IsAny<CancellationToken>()), Times.Once);
_repositoryMock.Verify(r => r.UpdateAsync(It.IsAny<Book>(), It.IsAny<CancellationToken>()), Times.Never);
_repositoryMock.Verify(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Never);
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task GivenValidOrderId_ShouldCancelOrderAndPublishEvent()
// Assert
result.IsSuccess.Should().BeTrue();
order.Status.Should().Be(Status.Canceled);
_repository.Verify(x => x.UpdateAsync(order, It.IsAny<CancellationToken>()), Times.Once);
_repository.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);
_publishEndpoint.Verify(
x => x.Publish(It.IsAny<OrderCancelledIntegrationEvent>(), It.IsAny<CancellationToken>()), Times.Once);
}
Expand All @@ -50,7 +50,7 @@ public async Task GivenInvalidOrderId_ShouldThrowNotFoundException_WhenOrderNotF

// Assert
await act.Should().ThrowAsync<NotFoundException>();
_repository.Verify(x => x.UpdateAsync(It.IsAny<Order>(), It.IsAny<CancellationToken>()), Times.Never);
_repository.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Never);
_publishEndpoint.Verify(
x => x.Publish(It.IsAny<OrderCancelledIntegrationEvent>(), It.IsAny<CancellationToken>()), Times.Never);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task GivenValidOrderId_ShouldCompleteOrderAndPublishEvent()
// Assert
result.IsSuccess.Should().BeTrue();
order.Status.Should().Be(Status.Completed);
_repository.Verify(x => x.UpdateAsync(order, It.IsAny<CancellationToken>()), Times.Once);
_repository.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);
_publishEndpoint.Verify(
x => x.Publish(It.IsAny<OrderCompletedIntegrationEvent>(), It.IsAny<CancellationToken>()), Times.Once);
}
Expand All @@ -50,7 +50,7 @@ public async Task GivenInvalidOrderId_ShouldThrowNotFoundException_WhenOrderNotF

// Assert
await act.Should().ThrowAsync<NotFoundException>();
_repository.Verify(x => x.UpdateAsync(It.IsAny<Order>(), It.IsAny<CancellationToken>()), Times.Never);
_repository.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Never);
_publishEndpoint.Verify(
x => x.Publish(It.IsAny<OrderCompletedIntegrationEvent>(), It.IsAny<CancellationToken>()), Times.Never);
}
Expand Down

0 comments on commit 23c8735

Please sign in to comment.