public Task <ApiModels.OrderPost> PostOrder(ApiModels.OrderPost orderPost) { return(Task.Run(() => { var id = Guid.NewGuid(); int ticketNum; DatabaseModels.OrderItems orderItems; DatabaseModels.OrderDiscount orderDiscount; lock (ticketNumberLock) { if (ticketNumber > 99) { ticketNumber = 1; } ticketNum = ticketNumber; ticketNumber++; } DatabaseModels.Order order = _orderRepository.Insert(new DatabaseModels.Order(id, orderPost.CashierId, DatabaseModels.OrderStatus.Preparing, orderPost.CreatedAt, orderPost.Price, ticketNum)); foreach (var product in orderPost.Products) { double price = _productRepository.GetProductPrice(product.Id); orderItems = _orderItemsRepository.Insert(new DatabaseModels.OrderItems(id, product.Id, product.Count, price)); } foreach (var discount in orderPost.Discounts) { if (discount != null) { bool isAvailable = _discountRepository.GetDiscountStatus(discount.Id); if (isAvailable) { orderDiscount = _orderDiscountRepository.Insert(new DatabaseModels.OrderDiscount(id, discount.Id, discount.Count)); } else { throw new Exception(); //TODO: Use more descriptive exception } } } DatabaseModels.Cashier cashier = _cashierRepository.Get(orderPost.CashierId); cashier.Bilans += orderPost.Price; cashier = _cashierRepository.Update(cashier); var orderProductsItems = _productRepository.GetOrderProducts(order); var orderPrice = _productRepository.GetOrderProductPrices(order).Sum(); var orderDiscounts = _orderDiscountRepository.GetOrderDiscounts(order); List <ApiModels.DiscountBasic> discountBasics = new List <ApiModels.DiscountBasic>(); List <ApiModels.Product> orderProducts = new List <ApiModels.Product>(); foreach (var e in orderDiscounts) { discountBasics.Add(new ApiModels.DiscountBasic(e.DiscountId, e.Quantity)); } foreach (var e in orderProductsItems) { orderProducts.Add(new ApiModels.Product(e.id, e.name, e.quantity)); } return new ApiModels.OrderPost(cashier.Id, orderPrice, orderProducts, discountBasics, order.CreatedAt); })); }