public async Task <IHttpActionResult> Create([FromBody] Order order) { try { var result = await OrderCore.CreateAsync(order).ConfigureAwait(false); return(Ok(result)); } catch (Exception e) { LogHelper.LogException <OrderController>(e); return(Ok(ResponseFactory <Order> .CreateResponse(false, ResponseCode.Error))); } }
public void InitializeMockData() { try { Task.Run(async() => { var existingProducts = await ProductCore.GetAllAsync().ConfigureAwait(false); if (existingProducts.Data != null && existingProducts.Data.Count != 0) { return; } var products = await ProductCore.CreateAsync(mProducts, true).ConfigureAwait(false); var existingClients = await ClientCore.GetAllAsync().ConfigureAwait(false); if (existingClients.Data != null && existingClients.Data.Count != 0) { return; } var currencies = await CurrencyCore.GetAllAsync().ConfigureAwait(false); if (!currencies.Success || currencies.Data == null || currencies.Data.Count == 0) { return; } var currency = currencies.Data.FirstOrDefault(c => c.Name == "RON"); if (currency == null) { return; } foreach (var client in mClients) { client.CurrencyId = currency.Id; } var clients = await ClientCore.CreateAsync(mClients, true).ConfigureAwait(false); var existingOrders = await OrderCore.GetAllAsync().ConfigureAwait(false); if (existingOrders.Data != null && existingOrders.Data.Count != 0) { return; } int nr = 0; List <Order> newOrders = new List <Order>(); foreach (var client in clients.Data) { if (products.Data.Count >= 6) { for (int i = 0; i < 2; i++) { nr++; var o = new Order() { Id = Guid.NewGuid(), CurrencyId = currency.Id, ClientId = client.Id, SaleType = i, Status = 0, Date = DateTime.Now.AddDays(-nr).AddHours(-nr), OrderItems = new List <OrderItem>() }; for (int k = i + nr / 2; k < 6; k += 1) { o.OrderItems.Add(new OrderItem() { Id = Guid.NewGuid(), OrderId = o.Id, ProductId = products.Data.ElementAt(k).Id, Price = products.Data.ElementAt(k).Price, Quantity = k + 1 }); } newOrders.Add(o); } } } var orders = await OrderCore.CreateAsync(newOrders, true, new[] { nameof(Order.OrderItems) }).ConfigureAwait(false); var payrolls = new List <Payroll>(); if (orders.Data.Count > 3) { for (int i = 0; i < 3; i++) { var payroll = new Payroll() { Id = Guid.NewGuid(), ClientId = clients.Data.ElementAt(i).Id, OrderId = orders.Data.ElementAt(i).Id, Date = DateTime.Now.AddDays(-i), Value = orders.Data.ElementAt(i).OrderItems.Sum(p => p.Price * p.Quantity), }; payrolls.Add(payroll); } } await PayrollCore.CreateAsync(payrolls, true).ConfigureAwait(false); }).ConfigureAwait(false).GetAwaiter().GetResult(); } catch (Exception ex) { // ignored } }