-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
186 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Admin/NCafe.Admin.Domain.Tests/Commands/DeleteProductTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using NCafe.Admin.Domain.Commands; | ||
using NCafe.Admin.Domain.Entities; | ||
using NCafe.Admin.Domain.Exceptions; | ||
using NCafe.Core.Repositories; | ||
using System.Threading; | ||
|
||
namespace NCafe.Admin.Domain.Tests.Commands; | ||
|
||
public class DeleteProductTests | ||
{ | ||
private readonly DeleteProductHandler _sut; | ||
private readonly IRepository _repository; | ||
|
||
public DeleteProductTests() | ||
{ | ||
_repository = A.Fake<IRepository>(); | ||
_sut = new DeleteProductHandler(_repository); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenNonExistingProduct_ShouldThrowException() | ||
{ | ||
// Arrange | ||
var id = Guid.NewGuid(); | ||
A.CallTo(() => _repository.GetById<Product>(id)).Returns((Product)null); | ||
|
||
// Act | ||
var exception = await Record.ExceptionAsync(() => _sut.Handle(new DeleteProduct(id), CancellationToken.None)); | ||
|
||
// Assert | ||
exception.ShouldBeOfType<ProductNotFound>(); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenExistingProduct_ShouldDeleteProduct() | ||
{ | ||
// Arrange | ||
var id = Guid.NewGuid(); | ||
var product = new Product(id, "Latte", 3); | ||
A.CallTo(() => _repository.GetById<Product>(id)).Returns(product); | ||
|
||
// Act | ||
await _sut.Handle(new DeleteProduct(id), CancellationToken.None); | ||
|
||
// Assert | ||
A.CallTo(() => _repository.Save(A<Product>.That.Matches(p => p.Id == id && p.IsDeleted == true))) | ||
.MustHaveHappenedOnceExactly(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using MediatR; | ||
using NCafe.Admin.Domain.Entities; | ||
using NCafe.Admin.Domain.Exceptions; | ||
using NCafe.Core.Repositories; | ||
|
||
namespace NCafe.Admin.Domain.Commands; | ||
|
||
public record DeleteProduct(Guid Id) : IRequest; | ||
|
||
internal sealed class DeleteProductHandler(IRepository repository) : IRequestHandler<DeleteProduct> | ||
{ | ||
private readonly IRepository _repository = repository; | ||
|
||
public async Task Handle(DeleteProduct command, CancellationToken cancellationToken) | ||
{ | ||
var product = await _repository.GetById<Product>(command.Id); | ||
if (product is null || product.IsDeleted) | ||
{ | ||
throw new ProductNotFound(command.Id); | ||
} | ||
|
||
product.Delete(); | ||
await _repository.Save(product); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using NCafe.Core.Domain; | ||
|
||
namespace NCafe.Admin.Domain.Events; | ||
|
||
public sealed record ProductDeleted : Event | ||
{ | ||
public ProductDeleted(Guid id) | ||
{ | ||
Id = id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using NCafe.Core.Exceptions; | ||
|
||
namespace NCafe.Admin.Domain.Exceptions; | ||
|
||
public class ProductNotFound(Guid id) : DomainException($"Product with id '{id}' was not found."); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,4 +88,4 @@ | |
|
||
app.Run(); | ||
|
||
public partial class Program { } | ||
public partial class Program; |
11 changes: 11 additions & 0 deletions
11
src/Cashier/NCafe.Cashier.Api/Projections/ProductDeleted.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using NCafe.Core.Domain; | ||
|
||
namespace NCafe.Cashier.Api.Projections; | ||
|
||
public sealed record ProductDeleted : Event | ||
{ | ||
public ProductDeleted(Guid id) | ||
{ | ||
Id = id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters