Skip to content

Instantly share code, notes, and snippets.

@davepcallan
Created January 29, 2024 15:51
Show Gist options
  • Save davepcallan/68b6b6c54bc951309b8639fbd96caf05 to your computer and use it in GitHub Desktop.
Save davepcallan/68b6b6c54bc951309b8639fbd96caf05 to your computer and use it in GitHub Desktop.
GetMyOrders Vertical slice refactor example from eShopOnWeb
namespace Microsoft.eShopWeb.Features.Orders;
[Route("order/my-orders")]
public class GetMyOrdersController(CatalogContext db) : Controller
{
[HttpGet]
public async Task<IActionResult> MyOrders()
{
var userName = User.Identity.Name;
var orders = await db.Orders
.Where(x => x.BuyerId == userName)
.Select(o => new OrderSummary(o.OrderDate, o.Id, o.OrderItems.Sum(x => x.UnitPrice * x.Units)))
.ToArrayAsync();
var viewModel = new GetMyOrdersViewModel(orders);
return View("/Features/Orders/GetMyOrders.cshtml", viewModel);
}
}
public record GetMyOrdersViewModel(IEnumerable<OrderSummary> Orders);
public record OrderSummary(DateTimeOffset OrderDate, int OrderNumber, decimal Total)
{
private const string DEFAULT_STATUS = "Pending";
public string Status => DEFAULT_STATUS;
}
@dcomartin
Copy link

Nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment