-
-
Save dcomartin/29c9699de89ea20e2328d5671557354e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| [ApiExplorerSettings(IgnoreApi = true)] | |
| [Authorize] // Controllers that mainly require Authorization still use Controller/View; other pages use Pages | |
| [Route("[controller]/[action]")] | |
| public class OrderController : Controller | |
| { | |
| private readonly IMediator _mediator; | |
| public OrderController(IMediator mediator) | |
| { | |
| _mediator = mediator; | |
| } | |
| [HttpGet] | |
| public async Task<IActionResult> MyOrders() | |
| { | |
| Guard.Against.Null(User?.Identity?.Name, nameof(User.Identity.Name)); | |
| var viewModel = await _mediator.Send(new GetMyOrders(User.Identity.Name)); | |
| return View(viewModel); | |
| } | |
| } | |
| { | |
| private readonly IReadRepository<Order> _orderRepository; | |
| public GetMyOrdersHandler(IReadRepository<Order> orderRepository) | |
| { | |
| _orderRepository = orderRepository; | |
| } | |
| public async Task<IEnumerable<OrderViewModel>> Handle(GetMyOrders request, | |
| CancellationToken cancellationToken) | |
| { | |
| var specification = new CustomerOrdersSpecification(request.UserName); | |
| var orders = await _orderRepository.ListAsync(specification, cancellationToken); | |
| return orders.Select(o => new OrderViewModel | |
| { | |
| OrderDate = o.OrderDate, | |
| OrderNumber = o.Id, | |
| ShippingAddress = o.ShipToAddress, | |
| Total = o.Total() | |
| }); | |
| } | |
| } | |
| public class CustomerOrdersSpecification : Specification<Order> | |
| { | |
| public CustomerOrdersSpecification(string buyerId) | |
| { | |
| Query.Where(o => o.BuyerId == buyerId) | |
| .Include(o => o.OrderItems); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment