public async Task <ActionResult> PlaceOrder([FromQuery] string userId, [FromQuery] int addressId, [FromBody] IEnumerable <CartItemViewModel> cartItemViewModels) { if (!this.ModelState.IsValid) { return(this.BadRequest(OrderingConstants.InvalidOrderMessage)); } var order = new Order { Date = DateTime.Now, DeliveryAddressId = addressId, DeliveryCost = new Random().Next(1, 9), OrderItems = this.mapper.Map <IEnumerable <CartItemViewModel>, IEnumerable <OrderItem> >(cartItemViewModels).ToList(), UserId = userId }; var totalOrdersCount = await this.orderingDbContext .Orders .CountAsync(); var messageData = new OrderPlacedMessage { UserId = userId, Total = totalOrdersCount + 1 }; var message = new Message(messageData); await this.orderingDbContext .Messages .AddAsync(message); await this.orderingDbContext .Orders .AddAsync(order); await this.orderingDbContext .SaveChangesAsync(); var msg = await this.orderingDbContext .Messages .FindAsync(message.Id); await this.publisher.Publish(messageData); msg.MarkAsPublished(); await this.orderingDbContext .SaveChangesAsync(); return(this.Ok()); }
private void HandleOrderPlaced(OrderPlacedMessage message) { this.PlacedOrders.Add(message.PlacedOrderItem); }