Skip to content

Instantly share code, notes, and snippets.

Created March 22, 2023 19:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dcomartin/eb318479e183865d7bc59d580932c7a0 to your computer and use it in GitHub Desktop.
public async Task CreateOrderAsync(int basketId, Address shippingAddress)
var basketSpec = new BasketWithItemsSpecification(basketId);
var basket = await _basketRepository.GetBySpecAsync(basketSpec);
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 order = new Order(basket.BuyerId, shippingAddress, items);
var evnt = new OrderPlacedEvent(order.Id);
await _db.AddAsync(order);
await _db.SaveChangesAsync();
await _broker.Publish(evnt);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment