public async Task <IActionResult> Save(OrderDTO orderDto) { var order = _orderRepository.GetOrderInclById(orderDto.Id); if (order == null) { return(BadRequest("Order not found.")); } // Ensure that email is provided for order created by guest user if (String.IsNullOrEmpty(order.CustomerId) && String.IsNullOrEmpty(orderDto.DeliveryDetails.Email)) { ModelState.AddModelError("", "Email is not provided."); } if (!ModelState.IsValid) { _breadcrumbNavBuilder.CreateForNode("CPanelOrderEdit", new { orderId = order.Id, orderName = ("Order #" + order.Id).ToString() }, this); return(View("Edit", orderDto)); } var admin = await _userManager.GetUserAsync(User); // Preserve original user comment var tempComment = order.DeliveryDetails.Comment; // Set delivery & payment details Object2ObjectMappings.OrderDetailsDtoToOrder(orderDto.DeliveryDetails, order); // Recalculate delivery fee order.DeliveryFee = DeliveryFeeCalculator.Calculate(order.DeliveryDetails.DeliveryMethodType); order.DeliveryDetails.Comment = tempComment; // Retireve changes and log var changeLogs = _auditTrailService.RetrieveAndLogChanges(); foreach (var changeLog in changeLogs) { order.ChangeHistory.Add(new OrderChangeLog { ChangeLog = changeLog, Order = order }); } // Update last edit timestamp order.LastModifiedById = admin.Id; order.LastModifiedOn = DateTime.UtcNow; _orderRepository.SaveChanges(); return(RedirectToAction("AdminView", new { id = orderDto.Id })); }
public async Task <IActionResult> CheckoutConfirm([FromRoute] string confirmationKey) { try { var userId = _userManager.GetUserId(User); // validate confirmation key if (String.IsNullOrEmpty(confirmationKey)) { return(BadRequest("Missing confirmation key.")); } // try to retrieve temp order from cache var tempOrder = ReadFromCache(confirmationKey); if (tempOrder == null) { return(BadRequest("Temp order not found.")); } // create order var order = new Order(tempOrder.DeliveryDetails.DeliveryMethodType) { CreatedOn = DateTime.UtcNow, CustomerId = userId, Status = OrderStatus.Created }; // append all items foreach (var tempItem in tempOrder.Items) { var productInDb = _productRepository.GetProductById(tempItem.Product.Id); if (productInDb != null && productInDb.NumberInStock >= tempItem.Quantity && productInDb.IsActive && productInDb.IsAvailable) { order.Items.Add(new OrderItem { Product = productInDb, ProductId = productInDb.Id, PurchasePrice = productInDb.SalePrice, Quantity = tempItem.Quantity }); } } // check if email has been provided for guest user if (String.IsNullOrEmpty(userId)) { if (String.IsNullOrEmpty(tempOrder.DeliveryDetails.Email)) { return(BadRequest("No email address provided for guest user's order.")); } order.DeliveryDetails.EmailAddress = tempOrder.DeliveryDetails.Email; } // set basic delivery details order.DeliveryFee = DeliveryFeeCalculator.Calculate(tempOrder.DeliveryDetails.DeliveryMethodType); // set delivery-specific details Object2ObjectMappings.OrderDetailsDtoToOrder(tempOrder.DeliveryDetails, order); // check consistency or order amount (prevents user from accidentally ordering items for which the price has just changed) if (order.OrderAmountTotal != tempOrder.OrderAmountTotal) { return(BadRequest("Price of some items you are trying to order might have changed.")); } // persist changes _orderRepository.AddOrder(order); if (_orderRepository.SaveChanges() > 0) { _cartManager.ResetCart(); _memoryCache.Remove(confirmationKey); await SendOrderConfirmation(order); } // return order id return(Ok(new { orderId = order.Id })); } catch (Exception ex) { var userId = _userManager.GetUserId(User); _logger.LogError("Order validation failed (user ID: " + userId + "). " + ex.ToString()); return(BadRequest("Unexpected error has occured. Please try again later.")); } }