-
-
Save dcomartin/7c0ba4470ddeee47b7400ba334d152f7 to your computer and use it in GitHub Desktop.
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
public class OrderService : IOrderService | |
{ | |
private readonly IRepository<Order> _orderRepository; | |
private readonly IUriComposer _uriComposer; | |
private readonly UtcNow _utcNow; | |
private readonly IRepository<Basket> _basketRepository; | |
private readonly IRepository<CatalogItem> _itemRepository; | |
public OrderServiceDelegate(IRepository<Basket> basketRepository, | |
IRepository<CatalogItem> itemRepository, | |
IRepository<Order> orderRepository, | |
IUriComposer uriComposer, | |
UtcNow utcNow) | |
{ | |
_orderRepository = orderRepository; | |
_uriComposer = uriComposer; | |
_utcNow = utcNow; | |
_basketRepository = basketRepository; | |
_itemRepository = itemRepository; | |
} | |
public async Task<Order> CreateOrderAsync(int basketId, Address shippingAddress) | |
{ | |
var basket = await _basketRepository.GetByIdAsync(basketId); | |
Guard.Against.NullBasket(basketId, basket); | |
Guard.Against.EmptyBasketOnCheckout(basket.Items); | |
var catalogItemsSpecification = new CatalogItemsSpecification(basket.Items.Select(item => item.CatalogItemId).ToArray()); | |
var catalogItems = await _itemRepository.ListAsync(catalogItemsSpecification); | |
var items = basket.Items.Select(basketItem => | |
{ | |
var catalogItem = catalogItems.First(c => c.Id == basketItem.CatalogItemId); | |
var itemOrdered = new CatalogItemOrdered(catalogItem.Id, catalogItem.Name, _uriComposer.ComposePicUri(catalogItem.PictureUri)); | |
var orderItem = new OrderItem(itemOrdered, basketItem.UnitPrice, basketItem.Quantity); | |
return orderItem; | |
}).ToList(); | |
var now = _utcNow(); | |
var order = new Order(basket.BuyerId, shippingAddress, items, now); | |
await _orderRepository.AddAsync(order); | |
return order; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment