public async Task <IActionResult> AddItems(OrderAddItemInputModel orderfromView) { var userName = this.User.Identity.Name; var result = await this.ordersService.AddItemAsync(orderfromView, userName, this.ModelState); result.ItemList = await this.itemsService.GetAllItemsAsync <OrderOrderItemItemAddItemsViewModel>().Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name }).ToListAsync(); result.OrderItems = await this.orderItemsService .GetAllAsNoTrackingWithDeteletedAsync <OrderOrderItemAddItemsViewModel>() .Where(oi => oi.OrderId == result.Id && !oi.IsDeleted) .ToListAsync(); foreach (var item in result.OrderItems) { item.TotalPrice = this.GetTotalAmount(item); } if (!this.ModelState.IsValid) { return(this.View(result)); } return(this.Redirect($"/{GlobalConstants.AreaAdministrationName}/{GlobalConstants.ContollerOrdersName}/{GlobalConstants.ActionAddItemsName}/{result.Id}")); }
public async Task <OrderAddItemsViewModel> AddItemAsync(OrderAddItemInputModel orderFromView, string username, ModelStateDictionary modelState) { if (!modelState.IsValid) { var errorModel = await this.orderRepository.AllAsNoTracking().Where(x => x.Id == orderFromView.Id).To <OrderAddItemsViewModel>().FirstOrDefaultAsync(); return(errorModel); } var order = await this.orderRepository.AllWithDeleted().FirstOrDefaultAsync(x => x.Id == orderFromView.Id && !x.IsDeleted); if (order == null) { // TODO: exception } var orderItemDb = order.OrderItems .Where(x => !x.IsDeleted) .ToList(); order.OrderItems = orderItemDb; var isItemExist = await this.itemsService.GetAllItemsAsync <Item>().AnyAsync(x => x.Id == orderFromView.ItemId); if (!isItemExist) { // TODO: exception throw new NullReferenceException(); } var orderItem = new OrderItem { ItemId = orderFromView.ItemId, OrderId = order.Id, ItemWidth = orderFromView.ItemWidth, ItemHeight = orderFromView.ItemHeight, Description = orderFromView.Description, }; order.OrderItems.Add(orderItem); this.orderRepository.Update(order); await this.orderRepository.SaveChangesAsync(); return(order.To <OrderAddItemsViewModel>()); }