public ActionResult <BaseViewModel <OrderViewModel> > PostOrder(CreateOrderRequestViewModel order) { var result = _orderService.CreateOrderNoValidate(order); this.HttpContext.Response.StatusCode = (int)result.StatusCode; return(result); }
public BaseViewModel <OrderViewModel> CreateOrder(CreateOrderRequestViewModel order) { decimal totalPrice = 0; int totalQuantity = 0; var listOrderDetail = new HashSet <OrderDetail>(); var username = _orderRepository.GetUsername(); var orderEntity = _mapper.Map <Order>(order); orderEntity.SetDefaultInsertValue(_orderRepository.GetUsername()); if (order.PaymentId != null) { var paymentMethod = _paymentMethodRepository.GetMany(_ => _.IsDelete == false && _.Id.Equals(new Guid(order.PaymentId))); if (paymentMethod == null) { return(new BaseViewModel <OrderViewModel>() { StatusCode = HttpStatusCode.BadRequest, Code = ErrMessageConstants.PAYMENT_METHOD_NOT_FOUND, Description = MessageHandler.CustomErrMessage(ErrMessageConstants.PAYMENT_METHOD_NOT_FOUND), }); } } else { orderEntity.IsCash = true; } var location = _locationRepository.GetMany(_ => _.IsDelete == false && _.Id.Equals(new Guid(order.LocationId))); if (location == null) { return(new BaseViewModel <OrderViewModel>() { StatusCode = HttpStatusCode.BadRequest, Code = ErrMessageConstants.LOCATION_NOT_FOUND, Description = MessageHandler.CustomErrMessage(ErrMessageConstants.LOCATION_NOT_FOUND), }); } foreach (var product in order.Products) { var productId = new Guid(product.Id); var productEntity = _productRepository.GetMany(_ => _.IsDelete == false && _.Id.Equals(productId)).FirstOrDefault(); if (productEntity == null) { return(new BaseViewModel <OrderViewModel>() { StatusCode = HttpStatusCode.BadRequest, Code = ErrMessageConstants.PRODUCT_NOT_FOUND, Description = MessageHandler.CustomErrMessage(ErrMessageConstants.PRODUCT_NOT_FOUND), }); } if (productEntity?.IsSale ?? false) { if (productEntity?.PriceSale != product?.Price) { return(new BaseViewModel <OrderViewModel>() { StatusCode = HttpStatusCode.BadRequest, Code = ErrMessageConstants.PRODUCT_PRICE_NOT_FOUND, Description = MessageHandler.CustomErrMessage(ErrMessageConstants.PRODUCT_PRICE_NOT_FOUND), }); } } else { if (productEntity?.Price != product?.Price) { return(new BaseViewModel <OrderViewModel>() { StatusCode = HttpStatusCode.BadRequest, Code = ErrMessageConstants.PRODUCT_PRICE_NOT_FOUND, Description = MessageHandler.CustomErrMessage(ErrMessageConstants.PRODUCT_PRICE_NOT_FOUND), }); } } if (productEntity.Quantity < product.Quantity) { return(new BaseViewModel <OrderViewModel>() { StatusCode = HttpStatusCode.BadRequest, Code = ErrMessageConstants.OUT_OF_STOCK, Description = MessageHandler.CustomErrMessage(ErrMessageConstants.OUT_OF_STOCK), }); } productEntity.Quantity = productEntity.Quantity - product.Quantity; _productRepository.Update(productEntity); var orderDetail = _mapper.Map <OrderDetail>(product); var totalDetailPrice = product.Price * product.Quantity; orderDetail.SetDefaultInsertValue(username); orderDetail.ProductId = productId; orderDetail.TotalPrice = totalDetailPrice; orderDetail.TotalPrice = totalDetailPrice; orderDetail.OrderId = orderEntity.Id; totalPrice += product.Price * product.Quantity; totalQuantity += product.Quantity; listOrderDetail.Add(orderDetail); } orderEntity.TotalPrice = totalPrice; orderEntity.TotalQuantity = totalQuantity; orderEntity.CreatedUserId = new Guid(_orderRepository.GetCurrentUserId()); // orderEntity.OrderDetail = listOrderDetail; _orderRepository.Add(orderEntity); _orderDetailRepository.Add(listOrderDetail); var result = new BaseViewModel <OrderViewModel>() { Data = _mapper.Map <OrderViewModel>(orderEntity), }; Save(); return(result); }