We have one last action method to add, which, thankfully, is a very simple one. If you've closed it, open the Features/Orders/Controller.cs file up again, and add the following action method beneath the one we already defined:
[HttpGet]
public async Task<IActionResult> List()
{
var orders = await _db.Orders
.Where(x => x.User.UserName == User.Identity.Name)
.Select(x => new OrderListViewModel
{
Id = x.Id,
Placed = x.Placed,
Items = x.Items.Sum(i => i.Quantity),
Total = x.Items.Sum(i => i.ProductVariant.Price * i.Quantity),
PaymentStatus = Enum.GetName(typeof(PaymentStatus),
x.PaymentStatus)
})
.ToListAsync();
return Ok(orders);
}
We simply query the database for all orders where the associated user's UserName property matches the username of the currently logged in user...