public ProviderResponseWrapper CreateOrderDocument(Order newOrder) { try { OrderDTO newOrderDTO = new OrderDTO { Order_Number = newOrder.Order_Number, Customer_First = newOrder.Customer_First, Customer_Last = newOrder.Customer_Last, SKU = newOrder.SKU, Status = newOrder.Status, Order_Created = DateTime.Now }; if (newOrderDTO.IsValid() == true) { string repositoryMessage = _ordersRepository.CreateOrderDocument(newOrderDTO); if (repositoryMessage == "New item Inserted") { return(prwBuilderHelper.PRWBuilder(repositoryMessage, HTTPResponseCodes.HTTP_OK_RESPONSE)); } return(prwBuilderHelper.PRWBuilder(repositoryMessage, HTTPResponseCodes.HTTP_SERVER_FAILURE_RESPONSE)); } else { return(prwBuilderHelper.PRWBuilder("The form has missing information or is incorrect. Please re-enter values again.", HTTPResponseCodes.HTTP_BAD_REQUEST)); } } catch (Exception ex1) { return(prwBuilderHelper.PRWBuilder(ex1.ToString(), HTTPResponseCodes.HTTP_SERVER_FAILURE_RESPONSE)); } }
/// <inheritdoc /> public async Task <int> PlaceOrder(string username, OrderDTO orderdto) { // Throw out invalid data first if (orderdto == null || !orderdto.IsValid()) { throw new ArgumentException("Order could not be found"); } var user = await users.SingleAsync(s => s.Name == username); // Create new order object var order = new Order { User = user, OrderTime = DateTime.Now }; // Create list of order items from dto var orderitems = orderdto.Items.Select(s => new OrderItem { Order = order, Item = items.SingleOrDefault(t => t.Id == s.Id), Amount = s.Amount }) .ToList(); // And get total price var totalPrice = orderitems.Select(s => (s.Amount * s.Item.Price)).Sum(); // Balance check if (!user.HasBalance(totalPrice)) { return(0); } var hasBev = orderitems.Any(s => s.Item.Type == ItemType.Beverage); var hasFood = orderitems.Any(s => s.Item.Type == ItemType.Food); // Set status to none if none are present order.StatusBeverage = (hasBev ? OrderStatus.Pending : OrderStatus.None); order.StatusFood = (hasFood ? OrderStatus.Pending : OrderStatus.None); order.Items = orderitems; var log = new Dictionary <Item, int>(); // Go over all items and change their count foreach (var item in orderdto.Items) { var dbItem = await items.SingleAsync(s => s.Id == item.Id); try { //temp.Amount -= item.Amount; if (await itemService.ChangeCount(user, new ItemAmountChangeRequest { Id = dbItem.Id, Amount = -item.Amount })) //Should it fail, the item is rejected. This will be notified to the catering. This will not affect the Users balance. { log.Add(dbItem, -item.Amount); } else { RevertUser(user, dbItem.Count * dbItem.Price);//Refunds the failed item. It will not show up in the history. } } catch (Exception) { throw; } items.Update(dbItem);//Updated Item } var result = await orders.AddAsync(order);//Order added await context.SaveChangesAsync(); await userService.ChangeBalance(user.Name, -totalPrice, true); return(result.Entity.Id); }