Mediatr example (#325)

* Updates based on documentation

* Getting the build passing

* Getting app functioning

* A few cleanups to confirm it's working as expected

* Fixing functional tests

* Updating dockerfile for 3.0

* Functional Tests now run sequentially

* Updating to latest version of moq

* Adding migration for post 3.0 upgrades

* Removing commented out lines

* Moving address and catalogitemordered configuration in to classes that own them

* Installing MediatR nuget packages

* Configure MediatR in Startup

* Creating GetMyOrders MediatR query and handler

* Adding GetOrderDetails MediatR handler

* Refactoring out default values

* Added tests for GetOrderDetails mediator handler

* Defaulting values on Models for now

* Removing some spaces

* Splitting files

* Splitting out the GetOrderDetails files

* Adding test for GetMyOrders

* restructuing folders

* Using constant
This commit is contained in:
Eric Fleming
2019-11-15 13:36:51 -05:00
committed by Steve Smith
parent 1bae9e68d9
commit 29d1497a3f
12 changed files with 240 additions and 67 deletions

View File

@@ -1,9 +1,8 @@
using Microsoft.AspNetCore.Authorization;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Specifications;
using Microsoft.eShopWeb.Web.ViewModels;
using System.Linq;
using Microsoft.eShopWeb.Web.Features.MyOrders;
using Microsoft.eShopWeb.Web.Features.OrderDetails;
using System.Threading.Tasks;
namespace Microsoft.eShopWeb.Web.Controllers
@@ -13,66 +12,31 @@ namespace Microsoft.eShopWeb.Web.Controllers
[Route("[controller]/[action]")]
public class OrderController : Controller
{
private readonly IOrderRepository _orderRepository;
private readonly IMediator _mediator;
public OrderController(IOrderRepository orderRepository)
public OrderController(IMediator mediator)
{
_orderRepository = orderRepository;
_mediator = mediator;
}
[HttpGet()]
public async Task<IActionResult> MyOrders()
{
var orders = await _orderRepository.ListAsync(new CustomerOrdersWithItemsSpecification(User.Identity.Name));
var viewModel = await _mediator.Send(new GetMyOrders(User.Identity.Name));
var viewModel = orders
.Select(o => new OrderViewModel()
{
OrderDate = o.OrderDate,
OrderItems = o.OrderItems?.Select(oi => new OrderItemViewModel()
{
Discount = 0,
PictureUrl = oi.ItemOrdered.PictureUri,
ProductId = oi.ItemOrdered.CatalogItemId,
ProductName = oi.ItemOrdered.ProductName,
UnitPrice = oi.UnitPrice,
Units = oi.Units
}).ToList(),
OrderNumber = o.Id,
ShippingAddress = o.ShipToAddress,
Status = "Pending",
Total = o.Total()
});
return View(viewModel);
}
[HttpGet("{orderId}")]
public async Task<IActionResult> Detail(int orderId)
{
var customerOrders = await _orderRepository.ListAsync(new CustomerOrdersWithItemsSpecification(User.Identity.Name));
var order = customerOrders.FirstOrDefault(o => o.Id == orderId);
if (order == null)
var viewModel = await _mediator.Send(new GetOrderDetails(User.Identity.Name, orderId));
if (viewModel == null)
{
return BadRequest("No such order found for this user.");
}
var viewModel = new OrderViewModel()
{
OrderDate = order.OrderDate,
OrderItems = order.OrderItems.Select(oi => new OrderItemViewModel()
{
Discount = 0,
PictureUrl = oi.ItemOrdered.PictureUri,
ProductId = oi.ItemOrdered.CatalogItemId,
ProductName = oi.ItemOrdered.ProductName,
UnitPrice = oi.UnitPrice,
Units = oi.Units
}).ToList(),
OrderNumber = order.Id,
ShippingAddress = order.ShipToAddress,
Status = "Pending",
Total = order.Total()
};
return View(viewModel);
}
}