Example #1
0
        public ActionResult Basket(FormCollection collection)
        {
            RentDb     dbRent    = new RentDb();
            ProductsDb dbProduct = new ProductsDb();
            bool       rent      = false;
            bool       isRemove  = false;
            bool       resultat  = false;

            var claimIdentity = User.Identity as ClaimsIdentity;
            int idUser        = Convert.ToInt32(claimIdentity.FindFirst(ClaimTypes.NameIdentifier).Value);
            var productList   = claimIdentity.FindAll(ClaimTypes.UserData);

            if (collection["getRentDate"] != null)
            {
                foreach (var product in productList)
                {
                    if (product.Value != "")
                    {
                        // Down the product's stock in DB
                        isRemove = dbProduct.RemoveStock(Convert.ToInt32(product.Value));
                        resultat = isRemove;
                        if (isRemove == false)
                        {
                            break;
                        }
                    }
                }
            }

            // Create the rent in DB with the date when the customer will get the products in the shop
            if (resultat)
            {
                rent = dbRent.AddRent(Convert.ToDateTime(collection["getRentDate"]), idUser, productList);
                RemoveAllProductBasket();// Remove all products in basket
                return(RedirectToAction("Rents", "Rents", new { id = idUser }));
            }

            return(RedirectToAction("Basket"));
        }