Skip to content

Instantly share code, notes, and snippets.

Created March 16, 2023 19:02
What would you like to do?
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);
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;
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