Example #1
0
        public ActionResult Buy(BuyModel model)
        {
            // If haven't login => login and come back
            if (Session["UserID"] == null)
            {
                return(RedirectToAction("Login", "Users", new { @returnUrl = Request.Url.OriginalString }));
            }

            if (ModelState.IsValid)
            {
                string userid   = Session["UserID"].ToString();
                string hashPass = DataProvider.getHashPass(model.Password);
                User   user     = DataProvider.Instance.DataBase.Users.Where(x => x.Id == userid && x.Password == hashPass).SingleOrDefault();
                if (user != null)
                {
                    // Add new output
                    var output = DataProvider.Instance.DataBase.Outputs.Where(x => x.IdCustomer == userid).SingleOrDefault();
                    if (output == null)
                    {
                        output = new Output()
                        {
                            Id = Guid.NewGuid().ToString(), IdCustomer = userid
                        };
                        DataProvider.Instance.DataBase.Outputs.Add(output);
                        DataProvider.Instance.DataBase.SaveChanges();
                    }
                    var        product    = DataProvider.Instance.DataBase.Products.Where(x => x.Id == model.ProductID).SingleOrDefault();
                    OutputInfo outputinfo = new OutputInfo()
                    {
                        Id          = Guid.NewGuid().ToString(),
                        IdProduct   = model.ProductID,
                        IdOutput    = output.Id,
                        OutputPrice = product.Price.Value * (100 - product.Discount.Value) / 100,
                        Count       = model.Count,
                        DateOutput  = DateTime.Now,
                        IsCheckOut  = 0,
                    };
                    DataProvider.Instance.DataBase.OutputInfoes.Add(outputinfo);
                    // remove from basket
                    if (model.FromBasket)
                    {
                        var basket = DataProvider.Instance.DataBase.Baskets.Where(x => x.IdCustomer == userid).SingleOrDefault();
                        if (basket != null)
                        {
                            var bi = DataProvider.Instance.DataBase.BasketInfoes.Where(x => x.IdProduct == model.ProductID && x.IdBasket == basket.Id).SingleOrDefault();
                            if (bi != null)
                            {
                                DataProvider.Instance.DataBase.BasketInfoes.Remove(bi);
                                if (basket.BasketInfoes.Count == 0)
                                {
                                    DataProvider.Instance.DataBase.Baskets.Remove(basket);
                                }
                            }
                        }
                    }
                    DataProvider.Instance.DataBase.SaveChanges();

                    ViewBag.Bought = true;
                    return(View(model));
                }
                else
                {
                    ModelState.AddModelError("", "Your password is incorrect!");
                }
            }

            ViewBag.Bought = null;
            return(View(model));
        }