public OrderDto PlaceOrder(OfferDto offerDto, SalesCustomerDto customerDto) { var offer = this.offersRepository.Get(offerDto.Id); var customer = new Customer(customerDto.CustomerId, customerDto.Name); var order = Order.Create(offer, customer); this.ordersRepository.Add(order); this.unitOfWork.Commit(); this.logger.Info($"Order created [OrderId = {order.Id}, CustomerId = {order.Customer.CustomerId}]"); return(new OrderDto { OrderId = order.Id }); }
private static void RunTest(IServiceScopeFactory scopeFactory) { using (var scope = scopeFactory.CreateScope()) { var offersQuery = scope.ServiceProvider.GetService <IOffersQuery>(); Console.WriteLine($"Offers count: {offersQuery.GetOffers().Count}{Environment.NewLine}"); } OfferDto offer; Customer customer; OrderDto order; #region Generating offer using (var scope = scopeFactory.CreateScope()) { var offersService = scope.ServiceProvider.GetRequiredService <IOffersApplicationService>(); var carConfiguration = CarConfigurationGenerator.GenerateConfiguration(); offer = offersService.CreateOffer(carConfiguration); } #endregion using (var scope = scopeFactory.CreateScope()) { var offersQuery = scope.ServiceProvider.GetService <IOffersQuery>(); DisplayOffers(offersQuery.GetOffers()); } #region Choosing customer using (var scope = scopeFactory.CreateScope()) { var customersContext = scope.ServiceProvider.GetRequiredService <CustomersDbContext>(); var customers = customersContext.Customers.ToList(); if (customers.Count == 0) { throw new Exception("Can't find any customer"); } var random = new Random(); customer = GetRandomElement(random, customers); } #endregion #region Placing order using (var scope = scopeFactory.CreateScope()) { var customerDto = new SalesCustomerDto { CustomerId = customer.Id, Name = $"{customer.FirstName} {customer.LastName}" }; var orderService = scope.ServiceProvider.GetRequiredService <IOrdersApplicationService>(); order = orderService.PlaceOrder(offer, customerDto); } #endregion #region Applying discount if (customer.Type == CustomerType.Vip || customer.Type == CustomerType.Business) { using (var scope = scopeFactory.CreateScope()) { var orderService = scope.ServiceProvider.GetRequiredService <IOrdersApplicationService>(); var discount = (customer.Type == CustomerType.Vip ? 0.2M : 0.1M) * offer.TotalPrice; orderService.ApplyDiscount(order.OrderId, discount, $"Discount applied for '{customer.Type}' customer type in order {order.OrderId}"); } } #endregion using (var scope = scopeFactory.CreateScope()) { var ordersQuery = scope.ServiceProvider.GetRequiredService <IOrdersQuery>(); DisplayOrders(ordersQuery.GetOrders()); } }