-
-
Notifications
You must be signed in to change notification settings - Fork 803
Expand file tree
/
Copy pathCheckInventoryRequestHandler.cs
More file actions
38 lines (33 loc) · 1.25 KB
/
CheckInventoryRequestHandler.cs
File metadata and controls
38 lines (33 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using AotExample.Contracts.Requests;
using Mocha;
namespace AotExample.OrderService.Handlers;
public sealed class CheckInventoryRequestHandler(ILogger<CheckInventoryRequestHandler> logger)
: IEventRequestHandler<CheckInventoryRequest, CheckInventoryResponse>
{
public ValueTask<CheckInventoryResponse> HandleAsync(
CheckInventoryRequest request,
CancellationToken cancellationToken)
{
var quantityOnHand = Random.Shared.Next(0, 20);
var isAvailable = quantityOnHand >= request.Quantity;
logger.LogInventoryCheck(
request.ProductName,
quantityOnHand,
request.Quantity,
isAvailable ? "available" : "insufficient");
return new ValueTask<CheckInventoryResponse>(
new CheckInventoryResponse { IsAvailable = isAvailable, QuantityOnHand = quantityOnHand });
}
}
internal static partial class Logs
{
[LoggerMessage(
Level = LogLevel.Information,
Message = "Inventory check for {ProductName}: {QuantityOnHand} on hand, requested {Quantity} — {Result}")]
public static partial void LogInventoryCheck(
this ILogger logger,
string productName,
int quantityOnHand,
int quantity,
string result);
}