Skip to content

Commit

Permalink
Fix remaining code cleanup suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
fredimachado committed May 16, 2024
1 parent 964c49e commit 0a10bb8
Show file tree
Hide file tree
Showing 30 changed files with 126 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace NCafe.Admin.Api.Projections;

public class ProductProjectionService : BackgroundService
{
private readonly IProjectionService<Product> projectionService;
private readonly IProjectionService<Product> _projectionService;

public ProductProjectionService(IProjectionService<Product> projectionService)
{
this.projectionService = projectionService;
_projectionService = projectionService;

projectionService.OnCreate<ProductCreated>(@event => new Product
{
Expand All @@ -22,7 +22,7 @@ public ProductProjectionService(IProjectionService<Product> projectionService)

protected async override Task ExecuteAsync(CancellationToken stoppingToken)
{
await projectionService.Start(stoppingToken);
await _projectionService.Start(stoppingToken);

await Task.Delay(Timeout.Infinite, stoppingToken);
}
Expand Down
18 changes: 8 additions & 10 deletions src/Admin/NCafe.Admin.Domain.Tests/Commands/CreateProductTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ namespace NCafe.Admin.Domain.Tests.Commands;

public class CreateProductTests
{
private readonly CreateProductHandler sut;

private readonly IRepository repository;
private readonly CreateProductHandler _sut;
private readonly IRepository _repository;

public CreateProductTests()
{
repository = A.Fake<IRepository>();

sut = new CreateProductHandler(repository);
_repository = A.Fake<IRepository>();
_sut = new CreateProductHandler(_repository);
}

[Theory]
Expand All @@ -28,7 +26,7 @@ public async Task GivenInvalidName_ShouldThrowException(string name)
var command = new CreateProduct(name, 3);

// Act
var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command));
var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command));

// Assert
exception.ShouldBeOfType<InvalidProductNameException>();
Expand All @@ -43,7 +41,7 @@ public async Task GivenInvalidPrice_ShouldThrowException(decimal price)
var command = new CreateProduct("Flat White", price);

// Act
var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command));
var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command));

// Assert
exception.ShouldBeOfType<InvalidProductPriceException>();
Expand All @@ -56,11 +54,11 @@ public async Task GivenValidProductInformation_ShouldCreateAndStoreProduct()
var command = new CreateProduct("Flat White", 3.5m);

// Act
var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command));
var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command));

// Assert
exception.ShouldBeNull();
A.CallTo(() => repository.Save(A<Product>.That.Matches(p => p.Name == command.Name && p.Price == command.Price)))
A.CallTo(() => _repository.Save(A<Product>.That.Matches(p => p.Name == command.Name && p.Price == command.Price)))
.MustHaveHappenedOnceExactly(); ;
}
}
11 changes: 3 additions & 8 deletions src/Admin/NCafe.Admin.Domain/Commands/CreateProduct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ namespace NCafe.Admin.Domain.Commands;

public record CreateProduct(string Name, decimal Price) : ICommand;

internal sealed class CreateProductHandler : ICommandHandler<CreateProduct>
internal sealed class CreateProductHandler(IRepository repository) : ICommandHandler<CreateProduct>
{
private readonly IRepository repository;

public CreateProductHandler(IRepository repository)
{
this.repository = repository;
}
private readonly IRepository _repository = repository;

public async Task HandleAsync(CreateProduct command)
{
Expand All @@ -30,6 +25,6 @@ public async Task HandleAsync(CreateProduct command)

var product = new Product(Guid.NewGuid(), command.Name, command.Price);

await repository.Save(product);
await _repository.Save(product);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace NCafe.Admin.Domain.Exceptions;

public class InvalidProductPriceException : DomainException
public class InvalidProductPriceException(decimal price)
: DomainException($"The price '{price}' must be greater than zero.")
{
public InvalidProductPriceException(decimal price) : base($"The price '{price}' must be greater than zero.")
{
}
}
11 changes: 3 additions & 8 deletions src/Admin/NCafe.Admin.Domain/Queries/GetProducts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@ namespace NCafe.Admin.Domain.Queries;

public record GetProducts : IQuery<Product[]>;

internal sealed class GetProductsHandler : IQueryHandler<GetProducts, Product[]>
internal sealed class GetProductsHandler(IReadModelRepository<Product> productRepository) : IQueryHandler<GetProducts, Product[]>
{
private readonly IReadModelRepository<Product> productRepository;

public GetProductsHandler(IReadModelRepository<Product> productRepository)
{
this.productRepository = productRepository;
}
private readonly IReadModelRepository<Product> _productRepository = productRepository;

public Task<Product[]> HandleAsync(GetProducts query)
{
var products = productRepository.GetAll()
var products = _productRepository.GetAll()
.ToArray();
return Task.FromResult(products);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Barista/NCafe.Barista.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
builder.Services.AddResponseCompression(opts =>
{
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/octet-stream" });
["application/octet-stream"]);
});

var app = builder.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace NCafe.Barista.Api.Projections;

public class BaristaOrderProjectionService : BackgroundService
{
private readonly IProjectionService<BaristaOrder> projectionService;
private readonly IProjectionService<BaristaOrder> _projectionService;

public BaristaOrderProjectionService(IProjectionService<BaristaOrder> projectionService)
{
this.projectionService = projectionService;
_projectionService = projectionService;

projectionService.OnCreate<OrderPlaced>(@event => new BaristaOrder
{
Expand All @@ -24,9 +24,9 @@ public BaristaOrderProjectionService(IProjectionService<BaristaOrder> projection
(@event, order) => order.IsCompleted = true);
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
protected async override Task ExecuteAsync(CancellationToken stoppingToken)
{
await projectionService.Start(stoppingToken);
await _projectionService.Start(stoppingToken);

await Task.Delay(Timeout.Infinite, stoppingToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,27 @@ namespace NCafe.Barista.Domain.Tests.Commands;

public class CompleteOrderTests
{
private readonly CompleteOrderHandler sut;

private readonly IRepository repository;
private readonly CompleteOrderHandler _sut;
private readonly IRepository _repository;

public CompleteOrderTests()
{
repository = A.Fake<IRepository>();

sut = new CompleteOrderHandler(repository);
_repository = A.Fake<IRepository>();
_sut = new CompleteOrderHandler(_repository);
}

[Fact]
public async Task GivenOrderNotFound_ShouldThrowException()
{
// Arrange
var orderId = Guid.NewGuid();
A.CallTo(() => repository.GetById<BaristaOrder>(orderId))
A.CallTo(() => _repository.GetById<BaristaOrder>(orderId))
.Returns((BaristaOrder)null);

var command = new CompleteOrder(orderId);

// Act
var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command));
var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command));

// Assert
exception.ShouldBeOfType<OrderNotFoundException>();
Expand All @@ -40,16 +38,16 @@ public async Task ShouldSaveOrder()
{
// Arrange
var orderId = Guid.NewGuid();
A.CallTo(() => repository.GetById<BaristaOrder>(orderId))
A.CallTo(() => _repository.GetById<BaristaOrder>(orderId))
.Returns(new BaristaOrder(orderId, Guid.NewGuid(), 1));
var command = new CompleteOrder(orderId);

// Act
var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command));
var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command));

// Assert
exception.ShouldBeNull();
A.CallTo(() => repository.Save(A<BaristaOrder>.That.Matches(o => o.Id == orderId && o.IsCompleted == true)))
A.CallTo(() => _repository.Save(A<BaristaOrder>.That.Matches(o => o.Id == orderId && o.IsCompleted == true)))
.MustHaveHappenedOnceExactly();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ namespace NCafe.Barista.Domain.Tests.Commands;

public class PlaceOrderTests
{
private readonly PlaceOrderHandler sut;

private readonly IRepository repository;
private readonly PlaceOrderHandler _sut;
private readonly IRepository _repository;

public PlaceOrderTests()
{
repository = A.Fake<IRepository>();

sut = new PlaceOrderHandler(repository);
_repository = A.Fake<IRepository>();
_sut = new PlaceOrderHandler(_repository);
}

[Fact]
Expand All @@ -26,11 +24,11 @@ public async Task ShouldSaveOrder()
var command = new PlaceOrder(orderId, productId, 1);

// Act
var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command));
var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command));

// Assert
exception.ShouldBeNull();
A.CallTo(() => repository.Save(A<BaristaOrder>.That.Matches(o => o.Id == orderId && o.ProductId == productId && o.Quantity == 1)))
A.CallTo(() => _repository.Save(A<BaristaOrder>.That.Matches(o => o.Id == orderId && o.ProductId == productId && o.Quantity == 1)))
.MustHaveHappenedOnceExactly();
}
}
18 changes: 4 additions & 14 deletions src/Barista/NCafe.Barista.Domain/Commands/CompleteOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,16 @@ namespace NCafe.Barista.Domain.Commands;

public record CompleteOrder(Guid Id) : ICommand;

internal sealed class CompleteOrderHandler : ICommandHandler<CompleteOrder>
internal sealed class CompleteOrderHandler(IRepository repository) : ICommandHandler<CompleteOrder>
{
private readonly IRepository repository;

public CompleteOrderHandler(IRepository repository)
{
this.repository = repository;
}
private readonly IRepository _repository = repository;

public async Task HandleAsync(CompleteOrder command)
{
var order = await repository.GetById<BaristaOrder>(command.Id);

if (order == null)
{
throw new OrderNotFoundException(command.Id);
}
var order = await _repository.GetById<BaristaOrder>(command.Id) ?? throw new OrderNotFoundException(command.Id);

order.CompletePreparation();

await repository.Save(order);
await _repository.Save(order);
}
}
11 changes: 3 additions & 8 deletions src/Barista/NCafe.Barista.Domain/Commands/PlaceOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ namespace NCafe.Barista.Domain.Commands;

public record PlaceOrder(Guid Id, Guid ProductId, int Quantity) : ICommand;

internal sealed class PlaceOrderHandler : ICommandHandler<PlaceOrder>
internal sealed class PlaceOrderHandler(IRepository repository) : ICommandHandler<PlaceOrder>
{
private readonly IRepository repository;

public PlaceOrderHandler(IRepository repository)
{
this.repository = repository;
}
private readonly IRepository _repository = repository;

public async Task HandleAsync(PlaceOrder command)
{
var order = new BaristaOrder(command.Id, command.ProductId, command.Quantity);

await repository.Save(order);
await _repository.Save(order);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

namespace NCafe.Barista.Domain.Exceptions;

public class OrderNotFoundException : DomainException
public class OrderNotFoundException(Guid orderId)
: DomainException($"Order '{orderId}' was not found.")
{
public OrderNotFoundException(Guid orderId) : base($"Order '{orderId}' was not found.")
{
OrderId = orderId;
}

public Guid OrderId { get; }
public Guid OrderId { get; } = orderId;
}
12 changes: 4 additions & 8 deletions src/Barista/NCafe.Barista.Domain/Queries/GetOrders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ namespace NCafe.Barista.Domain.Queries;

public record GetOrders : IQuery<BaristaOrder[]>;

internal sealed class GetOrdersHandler : IQueryHandler<GetOrders, BaristaOrder[]>
internal sealed class GetOrdersHandler(IReadModelRepository<BaristaOrder> orderRepository)
: IQueryHandler<GetOrders, BaristaOrder[]>
{
private readonly IReadModelRepository<BaristaOrder> orderRepository;

public GetOrdersHandler(IReadModelRepository<BaristaOrder> orderRepository)
{
this.orderRepository = orderRepository;
}
private readonly IReadModelRepository<BaristaOrder> _orderRepository = orderRepository;

public Task<BaristaOrder[]> HandleAsync(GetOrders query)
{
var products = orderRepository.GetAll()
var products = _orderRepository.GetAll()
.ToArray();
return Task.FromResult(products);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace NCafe.Cashier.Api.Projections;

public class ProductProjectionService : BackgroundService
{
private readonly IProjectionService<Product> projectionService;
private readonly IProjectionService<Product> _projectionService;

public ProductProjectionService(IProjectionService<Product> projectionService)
{
this.projectionService = projectionService;
_projectionService = projectionService;

projectionService.OnCreate<ProductCreated>(@event => new Product
{
Expand All @@ -21,7 +21,7 @@ public ProductProjectionService(IProjectionService<Product> projectionService)

protected async override Task ExecuteAsync(CancellationToken stoppingToken)
{
await projectionService.Start(stoppingToken);
await _projectionService.Start(stoppingToken);

await Task.Delay(Timeout.Infinite, stoppingToken);
}
Expand Down
Loading

0 comments on commit 0a10bb8

Please sign in to comment.