Example #1
0
        public IActionResult LocationDetails(string id)
        {
            if (_unitOfWork.Location.GetFirstOrDefault(x => x.Id == id) == null)
            {
                return(RedirectToAction(nameof(Index)));
            }
            var locationFromDb = _unitOfWork.Location.GetFirstOrDefault(includeProperties: "Province,District,Ward,ApplicationUser,LocationType,ImageList", filter: x => x.Id == id);

            locationFromDb.RentTimeStartDate = DateTime.Now;
            locationFromDb.RentTimeEndDate   = DateTime.Now.AddDays(1);
            if (TempData["RentTimeEndDateError"] != null)
            {
                ViewBag.RentTimeEndDateError = TempData["RentTimeEndDateError"].ToString();
            }
            //List<UserAddress> userAddressList = new List<UserAddress>();
            //IEnumerable<SelectListItem> addressesList = null;
            //if (_signInManager.IsSignedIn(User)){
            //    var user = User.Identity.Name;
            //    var userId = _userManager.GetUserId(User);
            //    userAddressList.AddRange(_unitOfWork.UserAddress.GetAll().Where(x => x.UserId == userId));
            //    addressesList = _unitOfWork.UserAddress.GetUserAddressListForDropDownBaseOnUser(userId);
            //}

            CartLocationViewModel model = new CartLocationViewModel
            {
                locationId        = locationFromDb.Id,
                Location          = locationFromDb,
                RentTimeStartDate = DateTime.Now,
                RentTimeEndDate   = DateTime.Now.AddDays(1),
                LocationBooked    = false

                                    //UserAddressList = userAddressList,
                                    //UserAddressesList = addressesList
            };

            if (_signInManager.IsSignedIn(User))
            {
                var                 user            = User.Identity.Name;
                var                 userId          = _userManager.GetUserId(User);
                ApplicationUser     userObj         = _unitOfWork.User.Get(userId);
                List <OrderHeader>  orderHeaderList = _unitOfWork.OrderHeader.GetAll(filter: x => x.UserId == userId).ToList();
                List <OrderDetails> orderDetailList = new List <OrderDetails>();

                foreach (OrderHeader orderHeader in orderHeaderList)
                {
                    OrderDetails orderDetail = _unitOfWork.OrderDetails.FindBaseOnOrderHeaderId(orderHeader.Id);
                    orderDetailList.Add(orderDetail);
                }

                var orderDetailToCheck = orderDetailList.Find(x => (x.LocationId == locationFromDb.Id) && (x.Status != "Rejected") && (x.Status != "Cancelled"));
                if (orderDetailToCheck != null)
                {
                    model.LocationBooked = true;
                }
            }


            return(View(model));
        }
Example #2
0
        public IActionResult AddToCart(CartLocationViewModel cartItem)
        {
            ModelState.Remove("Location.Area");
            ModelState.Remove("Location.LocationTypeId");
            ModelState.Remove("Location.RentTimeEndDate");
            ModelState.Remove("Location.RentTimeStartDate");
            if (ModelState.IsValid)
            {
                if (cartItem.RentTimeEndDate < cartItem.RentTimeStartDate)
                {
                    //ViewBag.RentTimeEndDateError = "Rent Time End Date must be greater or equal to Rent Time Start Date";
                    TempData["RentTimeEndDateError"] = "Rent Time End Date must be greater or equal to Rent Time Start Date";
                    ModelState.AddModelError("RentTimeEndDate", "Rent Time End Date must be greater or equal to Rent Time Start Date");
                    return(RedirectToAction("LocationDetails", new { id = cartItem.Location.Id }));
                }
                TempData["RentTimeEndDateError"] = "";
                //List<string> sessionList = new List<string>();
                List <CartLocationViewModel> cartList  = new List <CartLocationViewModel>();
                CartLocationViewModel        cartModel = new CartLocationViewModel()
                {
                    locationId        = cartItem.Location.Id,
                    RentTimeStartDate = cartItem.RentTimeStartDate,
                    RentTimeEndDate   = cartItem.RentTimeEndDate
                                        //AddressId = cartItem.AddressId
                };

                if (HttpContext.Session.GetObject <List <CartLocationViewModel> >(SD.SessionCart) == null)
                {
                    cartList.Add(cartModel);
                    HttpContext.Session.SetObject(SD.SessionCart, cartList);
                }
                else
                {
                    cartList = HttpContext.Session.GetObject <List <CartLocationViewModel> >(SD.SessionCart);
                    bool itemInCart = false;
                    foreach (CartLocationViewModel model in cartList)
                    {
                        if (model.locationId == cartItem.Location.Id)
                        {
                            itemInCart = true;
                        }
                    }
                    if (!itemInCart)
                    {
                        cartList.Add(cartModel);
                    }
                    HttpContext.Session.SetObject(SD.SessionCart, cartList);
                }
                //if (string.IsNullOrEmpty(HttpContext.Session.GetString(SD.SessionCart)))
                //{
                //    sessionList.Add(locationId);
                //    HttpContext.Session.SetObject(SD.SessionCart, sessionList);
                //}

                //else
                //{
                //    sessionList = HttpContext.Session.GetObject<List<string>>(SD.SessionCart);
                //    if (!sessionList.Contains(locationId))
                //    {
                //        sessionList.Add(locationId);
                //        HttpContext.Session.SetObject(SD.SessionCart, sessionList);
                //    }
                //}
                //var test = location.RentTimeFrom;
            }
            return(RedirectToAction(nameof(Index)));
        }