Skip to content

Commit

Permalink
fix: upgrade bmb.domain.core package
Browse files Browse the repository at this point in the history
  • Loading branch information
italopessoa committed Nov 18, 2024
1 parent edb9c37 commit b705fc8
Show file tree
Hide file tree
Showing 55 changed files with 662 additions and 78 deletions.
14 changes: 14 additions & 0 deletions FIAP.TechChallenge.ByteMeBurger.sln
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FIAP.TechChallenge.ByteMeBu
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FIAP.TechChallenge.ByteMeBurger.Cognito.Gateway.Test", "tests\FIAP.TechChallenge.ByteMeBurger.Cognito.Gateway.Test\FIAP.TechChallenge.ByteMeBurger.Cognito.Gateway.Test.csproj", "{46B22CD7-BB91-4E80-870F-28DA476ED6A3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bmb.Orders.Domain", "src\Bmb.Orders.Domain\Bmb.Orders.Domain.csproj", "{291A60C8-8CD9-49DC-863E-8A7222285B68}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bmb.Orders.Domain.Test", "tests\Bmb.Orders.Domain.Test\Bmb.Orders.Domain.Test.csproj", "{55229833-4E62-4046-9B17-C4BD08C559BB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -109,6 +113,8 @@ Global
{B2898AA3-86A6-4AF1-B27F-A3706B2B0557} = {2F192BA8-59FC-4B0C-B59D-511EFA89F428}
{36FD200F-16AB-44A6-A103-E5EABD6EA263} = {5EC029C6-230A-4782-9B62-CB475C7D05F8}
{46B22CD7-BB91-4E80-870F-28DA476ED6A3} = {C244E1D6-E1EB-4314-9F39-BB59FA1F7C71}
{291A60C8-8CD9-49DC-863E-8A7222285B68} = {35A721E2-4C27-436D-A461-1BDB9D58DCC8}
{55229833-4E62-4046-9B17-C4BD08C559BB} = {C244E1D6-E1EB-4314-9F39-BB59FA1F7C71}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DBAD3C48-1539-4385-AC13-138C2874D7DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -167,5 +173,13 @@ Global
{46B22CD7-BB91-4E80-870F-28DA476ED6A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{46B22CD7-BB91-4E80-870F-28DA476ED6A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{46B22CD7-BB91-4E80-870F-28DA476ED6A3}.Release|Any CPU.Build.0 = Release|Any CPU
{291A60C8-8CD9-49DC-863E-8A7222285B68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{291A60C8-8CD9-49DC-863E-8A7222285B68}.Debug|Any CPU.Build.0 = Debug|Any CPU
{291A60C8-8CD9-49DC-863E-8A7222285B68}.Release|Any CPU.ActiveCfg = Release|Any CPU
{291A60C8-8CD9-49DC-863E-8A7222285B68}.Release|Any CPU.Build.0 = Release|Any CPU
{55229833-4E62-4046-9B17-C4BD08C559BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{55229833-4E62-4046-9B17-C4BD08C559BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55229833-4E62-4046-9B17-C4BD08C559BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55229833-4E62-4046-9B17-C4BD08C559BB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
26 changes: 26 additions & 0 deletions src/Bmb.Orders.Domain/Bmb.Orders.Domain.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="Bmb.Orders.Domain.Test" />
<InternalsVisibleTo Include="AutoFixture.Xunit2" />
<InternalsVisibleTo Include="FIAP.TechChallenge.ByteMeBurger.Application" />
<InternalsVisibleTo Include="FIAP.TechChallenge.ByteMeBurger.Api.Test" />
<InternalsVisibleTo Include="FIAP.TechChallenge.ByteMeBurger.Controllers.Test" />
<InternalsVisibleTo Include="FIAP.TechChallenge.ByteMeBurger.Application.Test" />
<InternalsVisibleTo Include="FIAP.TechChallenge.ByteMeBurger.Persistence.Test" />
<InternalsVisibleTo Include="Bmb.Payment.TechChallenge.ByteMeBurger.Persistence.Test" />
<InternalsVisibleTo Include="FIAP.TechChallenge.ByteMeBurger.Persistence.Test" />
<InternalsVisibleTo Include="FIAP.TechChallenge.ByteMeBurger.Persistence.Test" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Bmb.Domain.Core" Version="0.0.17" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions src/Bmb.Orders.Domain/Contracts/IOrderRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.ObjectModel;
using Bmb.Orders.Domain.Entities;

namespace Bmb.Orders.Domain.Contracts;

public interface IOrderRepository
{
Task<Order> CreateAsync(Order order);

Task<ReadOnlyCollection<Order>> GetAllAsync();

Task<Order?> GetAsync(Guid orderId);

Task<bool> UpdateOrderStatusAsync(Order order);

Task<bool> UpdateOrderPaymentAsync(Order order);
}
10 changes: 10 additions & 0 deletions src/Bmb.Orders.Domain/Contracts/IOrderTrackingCodeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Bmb.Orders.Domain.ValueObjects;

namespace Bmb.Orders.Domain.Contracts;

public interface IOrderTrackingCodeService
{
Task<OrderTrackingCode> GetNextAsync();

OrderTrackingCode GetNext();
}
154 changes: 154 additions & 0 deletions src/Bmb.Orders.Domain/Entities/Order.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
using Bmb.Domain.Core.Base;
using Bmb.Domain.Core.Entities;
using Bmb.Domain.Core.ValueObjects;
using OrderTrackingCode = Bmb.Orders.Domain.ValueObjects.OrderTrackingCode;

namespace Bmb.Orders.Domain.Entities;

public class Order : Entity<Guid>, IAggregateRoot
{
private List<OrderItem> _orderItems = Array.Empty<OrderItem>().ToList();

public Bmb.Domain.Core.Entities.Customer? Customer { get; private set; }

public OrderTrackingCode TrackingCode { get; set; }

public OrderStatus Status { get; private set; } = OrderStatus.PaymentPending;

public IReadOnlyList<OrderItem> OrderItems => _orderItems.AsReadOnly();

public decimal Total => _orderItems.Sum(o => o.UnitPrice * o.Quantity);

public PaymentId? PaymentId { get; set; }

public Order()

Check warning on line 24 in src/Bmb.Orders.Domain/Entities/Order.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'TrackingCode' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
: base(Guid.NewGuid())
{
Created = DateTime.UtcNow;
}

internal Order(Guid customerId)
: this(Guid.NewGuid(), new Bmb.Domain.Core.Entities.Customer(customerId))
{
Created = DateTime.UtcNow;
}

internal Order(Bmb.Domain.Core.Entities.Customer customer)

Check warning on line 36 in src/Bmb.Orders.Domain/Entities/Order.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'TrackingCode' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
: base(Guid.NewGuid())
{
Customer = customer;
Created = DateTime.UtcNow;
}

internal Order(Guid id, Bmb.Domain.Core.Entities.Customer customer)

Check warning on line 43 in src/Bmb.Orders.Domain/Entities/Order.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'TrackingCode' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
: base(id)
{
Customer = customer;
Created = DateTime.UtcNow;
}

public Order(Guid id, Bmb.Domain.Core.Entities.Customer? customer, OrderStatus status, OrderTrackingCode trackingCode, DateTime created,
DateTime? updated)
: base(id)
{
Customer = customer;
Status = status;
TrackingCode = trackingCode;
Created = created;
Updated = updated;
}

public Order(Bmb.Domain.Core.Entities.Customer? customer, OrderTrackingCode trackingCode, Dictionary<Product, int> selectedProducts)
: base(Guid.NewGuid())
{
Customer = customer;
TrackingCode = trackingCode;
Created = DateTime.UtcNow;

if (selectedProducts.Count == 0)
{
throw new DomainException("An Order must have at least one item");
}

foreach (var (product, quantity) in selectedProducts)
{
AddOrderItem(product.Id, product.Name, product.Price, quantity);
}
}

public void AddOrderItem(Guid productId, string productName, decimal unitPrice, int quantity)
{
if (Status == OrderStatus.PaymentPending)
_orderItems.Add(new OrderItem(Id, productId, productName, unitPrice, quantity));
else
throw new DomainException(
$"Cannot add items to an Order with status {Status}. Items can only be added when the status is PaymentPending.");
}

public void LoadItems(Guid productId, string productName, decimal unitPrice, int quantity)
{
_orderItems.Add(new OrderItem(Id, productId, productName, unitPrice, quantity));
}

public void SetPayment(PaymentId paymentId)
{
if (PaymentId is not null)
{
throw new DomainException("Order already has a payment intent.");
}

PaymentId = paymentId;
Update();
}

public void SetCustomer(Bmb.Domain.Core.Entities.Customer customer)
{
Customer = customer;
}

public void ConfirmPayment()
{
if (Status != OrderStatus.PaymentPending)
throw new DomainException($"Payment cannot be confirmed because of order status '{Status}'.");

Status = OrderStatus.Received;
Update();
}

public void InitiatePrepare()
{
if (Status != OrderStatus.Received)
throw new DomainException("Cannot start preparing if order isn't received.");

Status = OrderStatus.InPreparation;
Update();
}

public void FinishPreparing()
{
if (Status != OrderStatus.InPreparation)
throw new DomainException("Cannot Finish preparing order if it's not In Preparation yet.");

Status = OrderStatus.Ready;
Update();
}

public void DeliverOrder()
{
if (Status != OrderStatus.Ready)
throw new DomainException("Cannot Deliver order if it's not Ready yet.");

Status = OrderStatus.Completed;
Update();
}

public void SetTrackingCode(OrderTrackingCode code)
{
if (Status != OrderStatus.PaymentPending)
throw new DomainException("Cannot set status code for an existing Order.");

TrackingCode = code;
}

private void Update() => Updated = DateTime.UtcNow;
};
39 changes: 39 additions & 0 deletions src/Bmb.Orders.Domain/Entities/OrderItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Bmb.Domain.Core.Base;

namespace Bmb.Orders.Domain.Entities;

public class OrderItem : Entity<Guid>
{
public OrderItem()
{
}

public OrderItem(Guid orderId, Guid productId, string productName, decimal unitPrice, int quantity)
: base(Guid.NewGuid())
{
ArgumentException.ThrowIfNullOrWhiteSpace(productName);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(unitPrice);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(quantity);

if (Guid.Empty == orderId)
throw new ArgumentException("Invalid OrderId", nameof(orderId));
if (Guid.Empty == productId)
throw new ArgumentException("Invalid ProductId", nameof(productId));

OrderId = orderId;
ProductId = productId;
ProductName = productName;
UnitPrice = unitPrice;
Quantity = quantity;
}

public Guid OrderId { get; private set; }

public Guid ProductId { get; private set; }

public string ProductName { get; private set; } = string.Empty;

public decimal UnitPrice { get; private set; }

public int Quantity { get; private set; }
}
18 changes: 18 additions & 0 deletions src/Bmb.Orders.Domain/ValueObjects/OrderTrackingCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Bmb.Orders.Domain.ValueObjects;

public record OrderTrackingCode
{
public string Value { get; }

public OrderTrackingCode(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException("Order tracking code cannot be null or empty.", nameof(value));
}

Value = value;
}

public static implicit operator OrderTrackingCode(string code) => new (code);
}
3 changes: 3 additions & 0 deletions src/Bmb.Orders.Domain/ValueObjects/SelectedProduct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Bmb.Orders.Domain.ValueObjects;

public record SelectedProduct(Guid ProductId, int Quantity);
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System.Collections.ObjectModel;
using Bmb.Auth;
using Bmb.Domain.Core.ValueObjects;
using FIAP.TechChallenge.ByteMeBurger.Api.Model.Orders;
using FIAP.TechChallenge.ByteMeBurger.Controllers.Contracts;
using FIAP.TechChallenge.ByteMeBurger.Controllers.Dto;
using Bmb.Domain.Core.ValueObjects;
using FIAP.TechChallenge.ByteMeBurger.Api.Auth;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Hybrid;
using SelectedProduct = Bmb.Orders.Domain.ValueObjects.SelectedProduct;

namespace FIAP.TechChallenge.ByteMeBurger.Api.Controllers;

Expand Down
25 changes: 0 additions & 25 deletions src/FIAP.TechChallenge.ByteMeBurger.Api/DomainEventsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ public DomainEventsHandler(ILogger<DomainEventsHandler> logger, HybridCache cach
DomainEventTrigger.ProductCreated += OnProductCreated;
DomainEventTrigger.ProductDeleted += OnProductDeleted;
DomainEventTrigger.ProductUpdated += OnProductUpdated;
DomainEventTrigger.OrderCreated += OnOrderCreated;
// DomainEventTrigger.OrderPaymentConfirmed += OnOrderPaymentConfirmed;
DomainEventTrigger.OrderStatusChanged += OnOrderStatusChanged;
DomainEventTrigger.CustomerRegistered += OnCustomerRegistered;
// DomainEventTrigger.PaymentCreated += OnPaymentCreated;
}

private void OnCustomerRegistered(object? sender, CustomerRegistered e)
Expand All @@ -51,19 +48,6 @@ private void OnOrderStatusChanged(object? sender, OrderStatusChanged e)
InvalidateOrderCache(e.Payload.OrderId);
}

// private void OnOrderPaymentConfirmed(object? sender, OrderPaymentConfirmed e)
// {
// _logger.LogInformation("Order: {OrderId} payment confirmed", e.Payload);
// InvalidateOrderCache(e.Payload.OrderId);
// }

private void OnOrderCreated(object? sender, OrderCreated e)
{
InvalidateOrderList();
_publisher.PublishAsync(e).ConfigureAwait(false);
_logger.LogInformation("New {EventName} event: OrderId {OrderId}", nameof(OrderCreated), e.Payload.Id);
}

private void OnProductUpdated(object? sender, ProductUpdated e)
{
_logger.LogInformation("Product: {@oldProduct} updated {@newProduct}", e.Payload.newProduct,
Expand All @@ -81,12 +65,6 @@ private void OnProductCreated(object? sender, ProductCreated e)
_publisher.PublishAsync(e).ConfigureAwait(false);
}

// private void OnPaymentCreated(object? sender, PaymentCreated e)
// {
// _logger.LogInformation("Payment {PaymentId} created for Order: {OrderId}", e.Payload.Id.Value,
// e.Payload.OrderId);
// }

private void InvalidateOrderCache(Guid orderId)
{
_cache.RemoveAsync($"order-{orderId}").ConfigureAwait(false);
Expand All @@ -107,11 +85,8 @@ public void Dispose()
DomainEventTrigger.ProductCreated -= OnProductCreated;
DomainEventTrigger.ProductDeleted -= OnProductDeleted;
DomainEventTrigger.ProductUpdated -= OnProductUpdated;
DomainEventTrigger.OrderCreated -= OnOrderCreated;
// DomainEventTrigger.OrderPaymentConfirmed -= OnOrderPaymentConfirmed;
DomainEventTrigger.OrderStatusChanged -= OnOrderStatusChanged;
DomainEventTrigger.CustomerRegistered -= OnCustomerRegistered;
// DomainEventTrigger.PaymentCreated -= OnPaymentCreated;
GC.SuppressFinalize(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PackageReference Include="AspNetCore.HealthChecks.MySql" Version="8.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" />
<PackageReference Include="Bmb.Auth" Version="0.0.1" />
<PackageReference Include="Bmb.Domain.Core" Version="0.0.12" />
<PackageReference Include="Bmb.Domain.Core" Version="0.0.17" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.3"/>
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.0.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.2" />
Expand Down
2 changes: 1 addition & 1 deletion src/FIAP.TechChallenge.ByteMeBurger.Api/Mappers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Bmb.Orders.Domain.Entities;
using FIAP.TechChallenge.ByteMeBurger.Controllers;
using FIAP.TechChallenge.ByteMeBurger.Controllers.Dto;
using Bmb.Domain.Core.Entities;

namespace FIAP.TechChallenge.ByteMeBurger.Api;

Expand Down
Loading

0 comments on commit b705fc8

Please sign in to comment.