public IActionResult Orders() { var userId = User.GetId(); var orders = _dbContext.Orders.Where(o => o.UserId == userId); IOrderStates toShopOrderManager = new ToShopDeliveryOrder(); IOrderStates toHomeOrderManager = new ToHomeDeliveryOrder(); var model = orders.Select(o => new OrderInfoViewModel() { OrderId = o.Id, CreateDate = o.CreateDate, TotalPrice = o.TotalPrice, State = o.DeliveryMethod == DeliveryMethods.DeliveryToHome.GetString? new OrderState() { State = o.State, CssClass = toHomeOrderManager.GetStateCssClass(o.State) }: new OrderState() { State = o.State, CssClass = toShopOrderManager.GetStateCssClass(o.State) }, }).AsEnumerable(); return(View(model)); }
public async Task <IActionResult> DeliveryToHome(DeliveryToHomeViewModel model) { var userId = User.GetId(); IOrderStates orderStates = new ToHomeDeliveryOrder(); var entry = _dbContext.ShoppingCartEntries.Where(u => u.UserId == userId).Include(e => e.Product).ToList(); var order = new Order() { UserId = userId, DeliveryMethod = DeliveryMethods.DeliveryToHome.GetString, CreateDate = DateTime.Now, State = orderStates.GetDefaultState(), OrderItems = new Collection <OrderItems>(entry.Select(s => new OrderItems() { ProductName = s.Product.Name, ProductPrice = s.Product.Price, ProductQuantity = s.Quantity, ProductId = s.ProductId, }).ToList()), TotalCount = entry.Sum(s => s.Quantity), TotalPrice = entry.Sum(s => s.Quantity * s.Product.Price), Address = model.Address, }; _dbContext.Orders.Add(order); _dbContext.ShoppingCartEntries.RemoveRange(entry); await _dbContext.SaveChangesAsync(); return(RedirectToAction("Orders", "Profile")); }
public IActionResult OrderPage(int orderId) { var order = _dbContext.Orders.Include(o => o.OrderItems).FirstOrDefault(o => o.Id == orderId); var user = _dbContext.Users.FirstOrDefault(u => u.Id == order.UserId); IOrderStates toHomeOrderStatesManager = new ToHomeDeliveryOrder(); IOrderStates toShopOrderStatesManager = new ToShopDeliveryOrder(); var orderStates = DeliveryMethods.DeliveryToHome.GetString == order.DeliveryMethod ? toHomeOrderStatesManager.GetAllStates() : toShopOrderStatesManager.GetAllStates(); var selectList = orderStates.Select(o => new SelectListItem(o, o)); var model = new AdminPanelOrderPageViewModel() { OrderId = orderId, OrderState = order.State, DeliveryMethod = order.DeliveryMethod, CreateDate = order.CreateDate, Address = order.Address, OrderItems = order.OrderItems, TotalPrice = order.TotalPrice, TotalCount = order.TotalCount, Email = user.Email, OrderStates = selectList, }; return(View(model)); }
public async Task <IActionResult> OrderPage(AdminPanelOrderPageViewModel model) { var order = _dbContext.Orders.Include(o => o.OrderItems).FirstOrDefault(o => o.Id == model.OrderId); if (order.State != model.OrderState) { order.State = model.OrderState; _dbContext.SaveChanges(); var success = await _emailSender.SendEmailAsync( model.Email, "Смена статуса заказа", string.Format("Ваш заказ номер {0} сменил статус на \"{1}\"", model.OrderId, model.OrderState)); if (!success) { IOrderStates toHomeOrderStatesManager = new ToHomeDeliveryOrder(); IOrderStates toShopOrderStatesManager = new ToShopDeliveryOrder(); var orderStates = DeliveryMethods.DeliveryToHome.GetString == order.DeliveryMethod ? toHomeOrderStatesManager.GetAllStates() : toShopOrderStatesManager.GetAllStates(); ModelState.AddModelError("", $"Уведомление о смене статуса заказа не может быть отправлено, т.к оно заблокированно по подозрению в спаме.\n"); var selectList = orderStates.Select(o => new SelectListItem(o, o)); model.OrderStates = selectList; model.OrderItems = order.OrderItems; model.Address = order.Address; model.CreateDate = order.CreateDate; model.TotalCount = order.TotalCount; model.TotalPrice = order.TotalPrice; return(View("OrderPage", model)); } } return(RedirectToAction("Orders")); }
public IActionResult Orders(AdminPanelOrdersViewModel model) { var orders = _dbContext.Orders.Where(x => x == x); if (model.QueryId != 0) { if (model.StringSearchBy == ((int)SearchByEnum.Order).ToString()) { orders = orders.Where(o => o.Id == model.QueryId); } if (model.StringSearchBy == ((int)SearchByEnum.User).ToString()) { orders = orders.Where(o => o.UserId == model.QueryId); } } var ordersInMemory = orders.ToList(); IOrderStates toHomeDeliveryOrderManager = new ToHomeDeliveryOrder(); IOrderStates toShopDeliveryOrderManager = new ToShopDeliveryOrder(); var ordersResult = ordersInMemory.Select(o => new AdminPanelOrderInfoViewModel() { OrderId = o.Id, OwnerId = o.UserId, CreateDate = o.CreateDate, OrderState = o.DeliveryMethod == DeliveryMethods.DeliveryToHome.GetString ? new OrderState { State = o.State, CssClass = toHomeDeliveryOrderManager.GetStateCssClass(o.State) } : new OrderState { State = o.State, CssClass = toShopDeliveryOrderManager.GetStateCssClass(o.State) } }); var newModel = new AdminPanelOrdersViewModel() { Orders = ordersResult, QueryId = model.QueryId == 0 ? 1 : model.QueryId, StringSearchBy = model.StringSearchBy, SearchBy = model.SearchBy == null ? new List <SelectListItem>() { new SelectListItem("Номеру заказа", ((int)SearchByEnum.Order).ToString(), ((int)SearchByEnum.Order).ToString() == model.StringSearchBy? true : false), new SelectListItem("Идентификатору пользователя", ((int)SearchByEnum.User).ToString(), ((int)SearchByEnum.User).ToString() == model.StringSearchBy? true : false) } : model.SearchBy }; ModelState.Clear(); TryValidateModel(newModel, nameof(newModel)); return(View(newModel)); }