Tye is a tool that makes developing, testing, and deploying microservices and distributed applications easier. Project Tye includes a local orchestrator to make developing microservices easier and the ability to deploy microservices to Kubernetes with min
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

82 lines
2.5 KiB

using System;
using System.Threading.Tasks;
using Dapr;
using Dapr.Client;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace orders.Controllers
{
[ApiController]
public class OrdersController : ControllerBase
{
[Topic("messagebus", "orderplaced")]
[HttpPost("orderplaced")]
public async Task PlaceOrder(Order order, [FromServices] DaprClient dapr, [FromServices] ILogger<OrdersController> logger)
{
logger.LogInformation("Got order {OrderId} for product {ProductId}", order.OrderId, order.ProductId);
var state = await dapr.GetStateEntryAsync<InventoryState>("default", order.ProductId.ToString());
if (state.Value == null || state.Value.Remaining < -10)
{
// For demo purposes, assume we have 5 of these in stock :)
state.Value = new InventoryState() { Remaining = 5, };
}
state.Value.Remaining--;
await state.SaveAsync();
logger.LogInformation("Updated inventory for product {ProductId} to {Inventory}", order.ProductId, state.Value.Remaining);
OrderConfirmation confirmation;
if (state.Value.Remaining >= 0)
{
confirmation = new OrderConfirmation()
{
OrderId = order.OrderId,
Confirmed = true,
DeliveryDate = DateTime.Now.AddYears(1),
RemainingCount = state.Value.Remaining,
};
}
else
{
confirmation = new OrderConfirmation()
{
OrderId = order.OrderId,
Confirmed = false,
BackorderCount = -1 * state.Value.Remaining,
};
}
await dapr.PublishEventAsync("messagebus", "orderprocessed", confirmation);
logger.LogInformation("Sent confirmation for order {OrderId}", order.OrderId);
}
}
public class Order
{
public string OrderId { get; set; } = default!;
public int ProductId { get; set ; }
}
public class InventoryState
{
public int Remaining { get; set; }
}
public class OrderConfirmation
{
public string OrderId { get; set; } = default!;
public DateTime? DeliveryDate { get; set; }
public bool Confirmed { get; set; }
public int BackorderCount { get; set; }
public int RemainingCount { get; set; }
}
}