Created
January 29, 2024 15:51
-
-
Save davepcallan/68b6b6c54bc951309b8639fbd96caf05 to your computer and use it in GitHub Desktop.
GetMyOrders Vertical slice refactor example from eShopOnWeb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice