Skip to content

Commit

Permalink
Order barista orders by place order timestamp (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
aforesti authored May 26, 2024
1 parent 9555629 commit 599faa9
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/Barista/NCafe.Barista.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ await mediator.Send(new PlaceOrder(
// Notify clients
await hubContext.Clients.All.SendAsync(
"ReceiveOrder",
new Order(message.Id, message.OrderItems.Select(i => new NCafe.Shared.Hubs.OrderItem(i.Name, i.Quantity)).ToArray(), message.CustomerName));
new Order(message.Id,
message.OrderItems.Select(i => new NCafe.Shared.Hubs.OrderItem(i.Name, i.Quantity)).ToArray(),
message.CustomerName,
DateTimeOffset.Now));
});

app.MapDefaultEndpoints();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ public BaristaOrderProjectionService(IProjectionService<BaristaOrder> projection
{
Id = @event.Id,
CustomerName = @event.Customer,
OrderPlacedAt = @event.OrderPlacedAt,
Items = @event.OrderItems.Select(item => new BaristaOrderItem
{
Name = item.Name,
Quantity = item.Quantity
Quantity = item.Quantity,
}).ToArray()
});

projectionService.OnUpdate<OrderPrepared>(
order => order.Id,
(@event, order) => order.IsCompleted = true);
(@event, order) =>
{
order.IsCompleted = true;
order.OrderPreparedAt = @event.OrderPreparedAt;
});
}

protected async override Task ExecuteAsync(CancellationToken stoppingToken)
Expand Down
7 changes: 6 additions & 1 deletion src/Barista/NCafe.Barista.Domain/Entities/BaristaOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ public BaristaOrder(Guid id, ValueObjects.OrderItem[] orderItems, string custome
RaiseEvent(new OrderPlaced(id)
{
OrderItems = orderItems,
Customer = customer
Customer = customer,
OrderPlacedAt = DateTimeOffset.UtcNow,
});
}

public IReadOnlyCollection<ValueObjects.OrderItem> Items { get; private set; }
public string Customer { get; private set; }
public bool IsCompleted { get; private set; }
public DateTimeOffset? OrderPlacedAt { get; private set; }
public DateTimeOffset? OrderPreparedAt { get; private set; }

public void CompletePreparation()
{
Expand All @@ -42,11 +45,13 @@ private void Apply(OrderPlaced @event)
Id = @event.Id;
Items = @event.OrderItems;
Customer = @event.Customer;
OrderPlacedAt = @event.OrderPlacedAt;
IsCompleted = false;
}

private void Apply(OrderPrepared @event)
{
IsCompleted = true;
OrderPreparedAt = @event.OrderPreparedAt;
}
}
1 change: 1 addition & 0 deletions src/Barista/NCafe.Barista.Domain/Events/OrderPlaced.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public OrderPlaced(Guid id)
Id = id;
}

public DateTimeOffset OrderPlacedAt { get; init; }
public OrderItem[] OrderItems { get; init; }
public string Customer { get; init; }
}
3 changes: 3 additions & 0 deletions src/Barista/NCafe.Barista.Domain/Events/OrderPrepared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ public sealed record OrderPrepared : Event
public OrderPrepared(Guid id)
{
Id = id;
OrderPreparedAt = DateTimeOffset.UtcNow;
}

public DateTimeOffset OrderPreparedAt { get; init; }
}
6 changes: 4 additions & 2 deletions src/Barista/NCafe.Barista.Domain/Queries/GetOrders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ internal sealed class GetOrdersHandler(IReadModelRepository<BaristaOrder> orderR

public Task<BaristaOrder[]> Handle(GetOrders query, CancellationToken cancellationToken)
{
var products = _orderRepository.GetAll()
var orders = _orderRepository.GetAll()
.Where(order => !order.IsCompleted)
.OrderBy(order => order.OrderPlacedAt)
.ToArray();
return Task.FromResult(products);
return Task.FromResult(orders);
}
}
2 changes: 2 additions & 0 deletions src/Barista/NCafe.Barista.Domain/ReadModels/BaristaOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public sealed class BaristaOrder : ReadModel
{
public string CustomerName { get; set; }
public BaristaOrderItem[] Items { get; set; }
public DateTimeOffset OrderPlacedAt { get; set; }
public DateTimeOffset? OrderPreparedAt { get; set; }
public bool IsCompleted { get; set; }
}

Expand Down
2 changes: 1 addition & 1 deletion src/Common/NCafe.Shared/Hubs/Order.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace NCafe.Shared.Hubs;

public sealed record Order(Guid Id, OrderItem[] OrderItems, string Customer);
public sealed record Order(Guid Id, OrderItem[] OrderItems, string Customer, DateTimeOffset OrderPlacedAt);

public sealed record OrderItem(string Name, int Quantity);
2 changes: 1 addition & 1 deletion src/UI/NCafe.Web/Models/BaristaOrder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace NCafe.Web.Models;

public record BaristaOrder(Guid Id, string CustomerName, BaristaOrderItem[] Items, bool IsCompleted);
public record BaristaOrder(Guid Id, string CustomerName, BaristaOrderItem[] Items, DateTimeOffset OrderPlacedAt);

public record BaristaOrderItem(string Name, int Quantity);
10 changes: 5 additions & 5 deletions src/UI/NCafe.Web/Pages/Barista/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
{
<Spin />
}
else if (!orders.Any(o => !o.IsCompleted))
else if (!orders.Any())
{
<Result Title="No new orders for now." />
}
else
{
<Card Title="New Orders">
@foreach (var order in orders.Where(o => !o.IsCompleted))
@foreach (var order in orders.OrderBy(o => o.OrderPlacedAt))
{
<CardGrid Style="width:25%">
<h5>@order.CustomerName</h5>
Expand Down Expand Up @@ -58,17 +58,17 @@ else
var url = $"{Configuration["BaristaBaseAddress"]}/orders/prepared";
try
{
var order = orders.Single(o => o.Id == orderId);
var response = await Http.PostAsJsonAsync(url, new { OrderId = orderId });
response.EnsureSuccessStatusCode();

_ = NotificationService.Open(new NotificationConfig
{
Message = "Order Completed",
Message = $"{order.CustomerName}'s order Completed",
Description = $"Order completed successfully.",
NotificationType = NotificationType.Success
});

var order = orders.FirstOrDefault(o => o.Id == orderId);
orders.Remove(order);

StateHasChanged();
Expand Down Expand Up @@ -97,7 +97,7 @@ else
order.Id,
order.Customer,
order.OrderItems.Select(i => new BaristaOrderItem(i.Name, i.Quantity)).ToArray(),
false));
order.OrderPlacedAt));
StateHasChanged();
});

Expand Down

0 comments on commit 599faa9

Please sign in to comment.