private void CreateOrder(Billing billing, User loggedInUser) { Order order = new Order { User = loggedInUser, Billing = billing, Products = loggedInUser.Cart, Status = "Complete", Date = DateTime.Now }; orderLogic.AddOrder(order); }
public AlbelliMutation(IOrderLogic orderLogic) { Name = Constants.AlbelliMutation.Name; Field <OrderType>( Constants.AlbelliMutation.OrderTypeName, arguments: new QueryArguments( new QueryArgument <NonNullGraphType <ListGraphType <OrderLineInputType> > > { Name = Constants.AlbelliMutation.OrderTypeOrderlines } ), resolve: context => { var orderLines = context.GetArgument <IEnumerable <OrderLine> >(Constants.AlbelliMutation.OrderTypeOrderlines); return(orderLogic.AddOrder(new Order { OrderLine = orderLines })); }); }
public IActionResult AddOrder([FromBody] NewOrderModel model) { try { if (model == null) { return(BadRequest("Body content is not valid!")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!(model.StartDate.ToLocalTime() > DateTime.Now.AddDays(-1)) || !(model.EndDate > model.StartDate) ) { return(BadRequest("Inavalid dates range!")); } var requestHeaders = HttpContext.Request.Headers; int userId = 0; if (requestHeaders.TryGetValue("Authorization", out StringValues token)) { userId = JwtService.GetUserIdFromToken(JwtService.DecodeToken(token.ToString())); if (userId < 1) { return(Unauthorized()); } } else { return(Unauthorized()); } var entity = model.Map <OrderEntity>(); entity.StartDate = entity.StartDate.ToLocalTime(); entity.EndDate = entity.EndDate.ToLocalTime(); entity.UserId = userId; var vehicle = _vehicleLogic.GetVehicleById(entity.VehicleId); if (vehicle == null || vehicle.CarClass == null) { return(BadRequest()); } var userRole = JwtService.GetRoleFromToken(JwtService.DecodeToken(token.ToString())); if (userRole == Roles.user.GetEnumDescription() && (vehicle.IsActive == null || !(bool)vehicle.IsActive || !vehicle.IsAvailable || !vehicle.IsProper) ) { return(BadRequest()); } entity.Price = (decimal)((entity.EndDate - entity.StartDate).TotalDays * vehicle.CarClass.DailyPrice); entity.Price = decimal.Round(entity.Price, 2); entity = _orderLogic.AddOrder(entity); if (entity == null) { throw new Exception("Somthing went wrong while adding model to DB!"); } if (entity.User != null) { entity.User.Password = ""; } return(Ok(entity)); } catch (Exception e) { return(StatusCode(500, e)); } }