Esempio n. 1
0
        public ActionResult RemoveFromCart(int id)
        {
            string msg;
            // Remove the item from the cart
            RentalCart cart     = null;
            string     itemName = "";

            try
            {
                cart = RentalCart.GetCart(this.HttpContext);

                // Get the name of the Item to display confirmation
                itemName = market.CartItems
                           .Single(item => item.id == id).Item.Name;

                // Remove from cart
                cart.RemoveFromCart(id);
            }
            catch (Exception ex)
            {
                ErrorMessage.ErrorCode = ErrorCode.UNKNOWN;
                return(View("ErrorMessage", ErrorMessage));
            }

            if (cart.GetCartItems().Count == 0)
            {
                msg = "Your Rental Cart is Empty.";
            }
            else
            {
                msg = Server.HtmlEncode(itemName) +
                      " has been removed from your shopping cart.";
            }
            // Display the confirmation message
            var results = new RentalCartRemoveViewModel
            {
                Message   = msg,
                CartTotal = cart.GetTotal(),
                DeleteId  = id,
                CartCount = cart.GetCartItems().Count
            };

            Session["RentalCartItems"] = results.CartCount;
            return(Json(results));
        }
Esempio n. 2
0
        public ActionResult AddToCart(FormCollection collection)
        {
            string   id      = collection.Get("itemId");
            string   pickup  = collection.Get("pickupdate");
            string   dropoff = collection.Get("dropoffdate");
            DateTime pickupDate;
            DateTime dropoffDate;

            if (String.IsNullOrEmpty(id) || String.IsNullOrEmpty(pickup) || String.IsNullOrEmpty(dropoff))
            {
                string error = "Plase select accurate dates and try again.";
                return(RedirectToAction("Details", "Item", new { id = collection.Get("itemid"), errMsg = error }));
            }

            try
            {
                pickupDate  = DateTime.Parse(pickup);
                dropoffDate = DateTime.Parse(dropoff);
            }
            catch (Exception ex)
            {
                string error = "Please select accurate dates and try again.";
                return(RedirectToAction("Details", "Item", new { id = id, errMsg = error }));
            }
            //Invalid Dates
            if (pickupDate.Date < DateTime.Today.Date || dropoffDate.Date < DateTime.Today.Date)
            {
                string error = "Please select accurate dates and try again.";
                return(RedirectToAction("Details", "Item", new { id = id, errMsg = error }));
            }

            int rentalPeriod = (dropoffDate - pickupDate).Days;

            if (rentalPeriod <= 0)
            {
                string error = "Dropoff date cannot be earlier than or the same as the pickup date.";
                return(RedirectToAction("Details", "Item", new { id = id, errMsg = error }));
            }

            // Retrieve the album from the database
            var addedItem = market.Items.SingleOrDefault(m => m.Id == id);

            if (addedItem == null)
            {
                string error = "The item you specified doesn't exist on Rambla.";
                return(RedirectToAction("Details", "Item", new { id = id, errMsg = error }));
            }

            if (User.Identity.IsAuthenticated)
            {
                if (addedItem.Owner.Email == User.Identity.Name)
                {
                    string error = "You cannot add your own item to your Rental cart.";
                    return(RedirectToAction("Details", "Item", new { id = id, errMsg = error }));
                }
            }

            foreach (DateTime d in GetBlockedDates(id))
            {
                if (pickupDate.Date <= d.Date && dropoffDate.Date >= d.Date)
                {
                    string error = "Your proposed rental period includes blocked dates. Please select different dates and try again.";
                    return(RedirectToAction("Details", "Item", new { id = id, errMsg = error }));
                }

                if (pickupDate.AddDays(1).Date == d.Date)
                {
                    string error = "Your pickup date has to be atlest two days before a blocked date.";
                    return(RedirectToAction("Details", "Item", new { id = id, errMsg = error }));
                }
            }

            // Add it to the shopping cart
            RentalCart cart = null;

            try
            {
                cart = RentalCart.GetCart(this.HttpContext);
            }
            catch (Exception ex)
            {
                ErrorMessage.ErrorCode = ErrorCode.UNKNOWN;
                return(View("ErrorMessage", ErrorMessage));
            }

            if (cart.AddToCart(addedItem, DateTime.Parse(collection.Get("pickupdate")),
                               DateTime.Parse(collection.Get("dropoffdate"))))
            {
                Session["RentalCartItems"] = cart.GetCartItems().Count;
                // Go back to the main store page for more shopping
                return(RedirectToAction("Index"));
            }
            else
            {
                //ModelState.AddModelError("", "This item is already in your cart. Select a different item");
                string error = "This item is already in your cart. Select a different item";
                return(RedirectToAction("Details", "Item", new { id = id, errMsg = error }));
            }
        }