public IActionResult AddOrder(AddOrderDTO dto) { Passenger passenger = _passengerRepo.GetPassengerBySeat(dto.SeatNumber); if (passenger == null) { return(BadRequest()); } Order order = new Order(passenger, dto.Time); foreach (AddOrderlineDTO olDTO in dto.OrderlineDTOs) { Product product = _productRepo.GetProductById(olDTO.ProductId); if (product == null) { return(BadRequest()); } Orderline ol = new Orderline(olDTO.Number, product, order); ol.CalculateTotalPrice(); order.AddOrderline(ol); } order.CalculateTotalPrice(); try { _orderRepo.AddOrder(order); _orderRepo.SaveChanges(); } catch (Exception) { return(StatusCode(500)); } return(Ok(dto)); }
private async void PostOrder() { try { AddOrderDTO dto = new AddOrderDTO(Cart.Order); HttpContent content = new StringContent(DTOToJson(dto), Encoding.UTF8, "application/json"); HttpResponseMessage result = await client.PostAsync(new Uri("http://localhost:5000/api/Order"), content); if (result.IsSuccessStatusCode) { ClearCart(); ErrorMessage = null; SuccesMessage = "Order was created, we will bring you order asap!"; } else { ErrorMessage = "Something went wrong! Order was not created!"; } } catch (Exception) { ErrorMessage = "Something went wrong! Please try again later."; } }
public async Task <ActionResult> CreateOrder(AddOrderDTO newAddOrderDTO) { Order newOrder = _mapper.Map <Order>(newAddOrderDTO); _context.Orders.Add(newOrder); await _context.SaveChangesAsync(); return(CreatedAtAction("CreateOrder", newOrder)); }
public IActionResult CreateOrder(AddOrderDTO newAddOrderDTO) { var newOrder = new Order { Id = newAddOrderDTO.Id, CustomerId = newAddOrderDTO.CustomerId, IsComplete = newAddOrderDTO.IsComplete, OrderTotal = newAddOrderDTO.OrderTotal, BillingAddressId = newAddOrderDTO.BillingAddressId, ShippingAddressId = newAddOrderDTO.ShippingAddressId, PaymentId = newAddOrderDTO.PaymentId }; var repo = new OrderRepository(); var orderThatGotCreated = repo.AddNewOrder(newAddOrderDTO); return(Created($"api/orders/{orderThatGotCreated.Id}", orderThatGotCreated)); }
public Order AddNewOrder(AddOrderDTO newOrder) { using (var db = new SqlConnection(_connectionString)) { var sql = @"INSERT INTO[dbo].[Order] ([CustomerId] ,[IsComplete] ,[OrderTotal] ,[BillingAddressId] ,[ShippingAddressId] ,[PaymentId]) output inserted.* VALUES (@customerId , @isComplete , @orderTotal , @billingAddressId , @shippingAddressId , @paymentId)"; return(db.QueryFirst <Order>(sql, newOrder)); } }
/// <summary> /// submitOrder(提交订单) /// </summary> /// <returns></returns> private string SubmitOrder() { MessageInfo messageInfo = new MessageInfo() { Status = 1 }; try { if (CurrentMemberWeiXinDTO == null) { messageInfo.Message = "未获取到微信用户信息"; WCFClient.LoggerService.Error(string.Format("未获取到微信用户信息")); return(XMS.Core.Json.JsonSerializer.Serialize(messageInfo)); } string submitOrderInfo = Request["orderInfo"]; if (string.IsNullOrEmpty(submitOrderInfo)) { messageInfo.Message = "网络异常,稍后再试"; WCFClient.LoggerService.Error(string.Format("未获取到提交的订单信息")); return(XMS.Core.Json.JsonSerializer.Serialize(messageInfo)); } //bool outOfTime = Request["outOfTime"] == "0" ? false : true; XMS.Core.Container.LogService.Info("orderInfo:" + submitOrderInfo); SubmitOrderDTO submitOrderDTO = XMS.Core.Json.JsonSerializer.Deserialize <SubmitOrderDTO>(submitOrderInfo); if (submitOrderDTO == null) { messageInfo.Message = "网络异常,稍后再试"; WCFClient.LoggerService.Error(string.Format("未获取到提交的订单信息")); return(XMS.Core.Json.JsonSerializer.Serialize(messageInfo)); } DateTime?deliveryMaxTimeDate = null; DateTime?deliveryMinTimeDate = null; if (submitOrderDTO.deliveryMaxTime > 0) { deliveryMaxTimeDate = submitOrderDTO.deliveryMaxTime.MilliSecondsFrom1970ToDateTime(); } if (submitOrderDTO.deliveryMinTime > 0) { deliveryMinTimeDate = submitOrderDTO.deliveryMinTime.MilliSecondsFrom1970ToDateTime(); } List <AddOrderDTO> listOrder = new List <AddOrderDTO>(); submitOrderDTO.commodity.ForEach(item => { AddOrderDTO addOrder = new AddOrderDTO() { cityId = "上海", memberId = CurrentMemberWeiXinDTO.Id, commodityId = item.commodityId, couponId = submitOrderDTO.couponId.HasValue ? submitOrderDTO.couponId.Value : 0, deliveryMaxTime = deliveryMaxTimeDate, deliveryMinTime = deliveryMinTimeDate, memberAddressId = submitOrderDTO.addrssId, remark = submitOrderDTO.remark, serverFee = submitOrderDTO.serverFee, skuList = item.skuList == null ? null : item.skuList.ToArray(), resId = submitOrderDTO.resId, quantity = item.quantity, deliveryType = submitOrderDTO.deliveryType, isOutOfTime = !submitOrderDTO.outOfTime.HasValue ? false : (submitOrderDTO.outOfTime.Value == 0 ? false : true) }; listOrder.Add(addOrder); }); XMS.Core.ReturnValue <AddOrderResultDTO> addOrderResult = WCFClient.CoffeeService.AddOrder(listOrder.ToArray()); if (addOrderResult.Code != 200 || addOrderResult.Value == null) { messageInfo.Message = "网络异常稍后再试"; WCFClient.LoggerService.Error(string.Format(addOrderResult.RawMessage)); return(XMS.Core.Json.JsonSerializer.Serialize(messageInfo)); } if (addOrderResult.Value.order == null) { messageInfo.Message = "网络异常稍后再试"; WCFClient.LoggerService.Error(string.Format("提交订单失败")); return(XMS.Core.Json.JsonSerializer.Serialize(messageInfo)); } if (addOrderResult.Value.payInfo == null) { messageInfo.Message = "网络异常稍后再试"; WCFClient.LoggerService.Error(string.Format("获取支付信息失败")); return(XMS.Core.Json.JsonSerializer.Serialize(messageInfo)); } SubmitOrderResult submitOrderResult = new SubmitOrderResult() { id = addOrderResult.Value.order.id, payInfo = addOrderResult.Value.payInfo.payUrl }; messageInfo.Status = 0; messageInfo.Message = "success"; messageInfo.Data = submitOrderResult; } catch (Exception ex) { messageInfo.Status = 1; messageInfo.Message = "网络异常,稍后重试"; WCFClient.LoggerService.Error(string.Format("提交订单错误,详细情况:{0}", ex.Message)); } return(XMS.Core.Json.JsonSerializer.Serialize(messageInfo)); }
private string DTOToJson(AddOrderDTO dto) { return(JsonConvert.SerializeObject(dto)); }
public HttpResponseMessage CreateOrder(AddOrderDTO request) { HttpResponseMessage responseMessage; var token = Request.Headers.SingleOrDefault(x => x.Key == "token").Value.First(); try { var userId = db.Tokens.First(u => u.TokenString.Equals(token))?.UserId; if (userId > 0) { var order = new Orders { Date = DateTime.Now, UserId = userId.Value, FirstName = request.UserDetails.FirstName, LastName = request.UserDetails.LastName, State = request.UserDetails.State, Address = request.UserDetails.StreetAddress, City = request.UserDetails.City, ZipCode = request.UserDetails.ZipCode, Phone = request.UserDetails.Phone, Email = request.UserDetails.Email, Currency = request.Currency, Lang = request.Lang, ProductsOrders = new List <ProductsOrders>() }; db.Orders.Add(order); db.SaveChanges(); double subtotal = 0; foreach (var requestProduct in request.CartProducts) { var product = db.Products.Find(requestProduct.Key); if (product?.ProductId > 0) { var productsOrders = new ProductsOrders { ProductId = product.ProductId, ProductPrice = GetCurrencyPrice(product, order.Currency), Amount = requestProduct.Value, OrderId = order.OrderId, Currency = order.Currency, ProductProduct = product, Code = product.StyleCode + " " + product.Colour }; subtotal += productsOrders.ProductPrice * productsOrders.Amount; order.ProductsOrders.Add(productsOrders); } } order.Subtotal = subtotal; order.Shipping = 0; db.Orders.Update(order); db.SaveChanges(); responseMessage = Request.CreateResponse(HttpStatusCode.OK, new CreateOrderDTO { OrderId = order.OrderId }); } else { responseMessage = Request.CreateResponse(HttpStatusCode.BadRequest); } } catch (Exception) { responseMessage = Request.CreateResponse(HttpStatusCode.BadRequest); } return(responseMessage); }
// POST: api/Orders public HttpResponseMessage PostOrders(AddOrderDTO request) { HttpResponseMessage responseMessage; var token = Request.Headers.SingleOrDefault(x => x.Key == "token").Value.First(); try { var userId = db.Tokens.First(u => u.TokenString.Equals(token))?.UserId; if (userId > 0) { var order = new Orders { Date = DateTime.Now, UserId = userId.Value, FirstName = request.UserDetails.FirstName, LastName = request.UserDetails.LastName, State = request.UserDetails.State, Address = request.UserDetails.StreetAddress, City = request.UserDetails.City, ZipCode = request.UserDetails.ZipCode, Phone = request.UserDetails.Phone, Email = request.UserDetails.Email, Currency = request.Currency, Lang = request.Lang, TransactionId = request.TransactionId, ProductsOrders = new List <ProductsOrders>() }; db.Orders.Add(order); db.SaveChanges(); double subtotal = 0; var productList = new List <Products>(); foreach (var requestProduct in request.CartProducts) { var product = db.Products.Find(requestProduct.Key); if (product?.ProductId > 0) { var productsOrders = new ProductsOrders { ProductId = product.ProductId, ProductPrice = GetCurrencyPrice(product, order.Currency), Amount = requestProduct.Value, OrderId = order.OrderId, Currency = order.Currency, ProductProduct = product, Code = product.StyleCode + " " + product.Colour }; subtotal += productsOrders.ProductPrice * productsOrders.Amount; order.ProductsOrders.Add(productsOrders); productList.Add(product); } } order.Subtotal = subtotal; order.Shipping = 0; order.PaymentMethod = request.PaymentMethod; db.Orders.Update(order); db.SaveChanges(); var InvoiceLogic = new InvoiceLogic(db); var invoice = InvoiceLogic.CreateInvoice(order); order.Invoice = invoice.ToArray(); db.Orders.Update(order); db.SaveChanges(); var OrderLogic = new OrderLogic(db); OrderLogic.SendOrderEmail(order, productList, invoice); order.MailSent = true; db.Orders.Update(order); db.SaveChanges(); OrderLogic.SendOrderEmailToAdmin(order, productList, invoice); } } catch (DbUpdateException) { responseMessage = Request.CreateResponse(HttpStatusCode.BadRequest); return(responseMessage); } var json = new JSendMessage("success", "Order successfully added"); responseMessage = Request.CreateResponse(HttpStatusCode.OK, json); return(responseMessage); }
public async Task <ActionResult <Unit> > AddOrder([FromBody] AddOrderDTO order) => await _mediator.Send(new AddNewOrder.Command() { CartId = order.CartId, ProductsList = order.Products });