Пример #1
0
        public bool SaveCart(List<CartHistory> dataList)
        {
            var check = false;
            //JavaScriptSerializer ser = new JavaScriptSerializer();
            //List<CartHistory> dataList = ser.Deserialize<List<CartHistory>>(totaldata);
            try
            {
                for (int i = 0; i < dataList.Count; i++)
                {
                    var username = dataList[i].Username;
                    var pid = dataList[i].ProductId;
                    var now = DateTime.Now.Date;
                    var dupHistory = context.Histories.Where(his => his.Username == username &&
                        his.BuyTime == now).FirstOrDefault();

                    if (dupHistory == null)
                    {
                        var newHitory = new History();
                        newHitory.Username = username;
                        newHitory.BuyTime = now;

                        context.Histories.Add(newHitory);
                        var newHistoryDetail = new HistoryDetail();
                        newHistoryDetail.History = newHitory;
                        newHistoryDetail.ProductId = pid;
                        newHistoryDetail.MinPrice = dataList[i].MinPrice;
                        newHistoryDetail.MaxPrice = dataList[i].MaxPrice;
                        context.HistoryDetails.Add(newHistoryDetail);

                    }
                    else
                    {
                        var historyId = (from h in context.Histories
                                         where h.BuyTime == now && h.Username == username
                                         select h.Id).First();

                        var dupProductId = context.HistoryDetails.Where(p => p.ProductId == pid && p.HistoryId == historyId).FirstOrDefault();
                        if (dupProductId == null)
                        {
                            var newHistoryDetail = new HistoryDetail();
                            newHistoryDetail.History = dupHistory;
                            newHistoryDetail.ProductId = pid;
                            newHistoryDetail.MinPrice = dataList[i].MinPrice;
                            newHistoryDetail.MaxPrice = dataList[i].MaxPrice;
                            context.HistoryDetails.Add(newHistoryDetail);

                        }
                    }
                    context.SaveChanges();

                }

                check = true;
                return check;
            }
            catch
            {
                return check;
            }
        }
Пример #2
0
        public JsonResult SaveCart(String totaldata)
        {
            var check = false;
            JavaScriptSerializer ser = new JavaScriptSerializer();
            List<CartHistory> dataList = ser.Deserialize<List<CartHistory>>(totaldata);
            try
            {
                for (int i = 0; i < dataList.Count; i++)
                {
                    var username = dataList[i].Username;
                    var pid = dataList[i].ProductId;
                    var now = DateTime.Now.Date;
                    var dupHistory = db.Histories.Where(his => his.Username == username &&
                        his.BuyTime == now).FirstOrDefault();

                    if (dupHistory == null)
                    {
                        var newHitory = new History();
                        newHitory.Username = username;
                        newHitory.BuyTime = now;

                        db.Histories.Add(newHitory);
                        var newHistoryDetail = new HistoryDetail();
                        newHistoryDetail.History = newHitory;
                        newHistoryDetail.ProductId = pid;
                        newHistoryDetail.MinPrice = dataList[i].MinPrice;
                        newHistoryDetail.MaxPrice = dataList[i].MaxPrice;
                        db.HistoryDetails.Add(newHistoryDetail);

                    }
                    else
                    {
                        var historyId = (from h in db.Histories
                                         where h.BuyTime == now && h.Username == username
                                         select h.Id).First();
                        var checkCount = (from c in db.HistoryDetails
                                          where c.HistoryId == historyId
                                          select c).Count();
                        if (checkCount >= 10)
                        {
                            return Json("full", JsonRequestBehavior.AllowGet);
                        }
                        var dupProductId = db.HistoryDetails.Where(p => p.ProductId == pid && p.HistoryId == historyId).FirstOrDefault();
                        if (dupProductId == null)
                        {
                            var newHistoryDetail = new HistoryDetail();
                            newHistoryDetail.History = dupHistory;
                            newHistoryDetail.ProductId = pid;
                            newHistoryDetail.MinPrice = dataList[i].MinPrice;
                            newHistoryDetail.MaxPrice = dataList[i].MaxPrice;
                            db.HistoryDetails.Add(newHistoryDetail);

                        }
                    }
                    db.SaveChanges();

                }

                check = true;
                return Json(check, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(check, JsonRequestBehavior.AllowGet);
            }
            //return RedirectToAction("SearchProduct");
        }
Пример #3
0
        public ActionResult SaveCart()
        {
            if (GetCart().Lines.Count() == 0)
            {
                ModelState.AddModelError("", "Không có sản phẩm nào trong giỏ hàng.");
            }

            if (ModelState.IsValid)
            {
                var now = DateTime.Now.Date;
                foreach (var line in GetCart().Lines)
                {
                    var pid = line.Product.ProductId;

                    var dupHistory = db.Histories.FirstOrDefault(his => his.Username == User.Identity.Name &&
                                                                        his.BuyTime == now);

                    if (dupHistory == null)
                    {
                        var newHitory = new History();
                        newHitory.Username = User.Identity.Name;
                        newHitory.BuyTime = now;

                        db.Histories.Add(newHitory);
                        var newHistoryDetail = new HistoryDetail();
                        newHistoryDetail.History = newHitory;
                        newHistoryDetail.ProductId = pid;
                        newHistoryDetail.MinPrice = line.Product.MinPrice;
                        newHistoryDetail.MaxPrice = line.Product.MaxPrice;
                        db.HistoryDetails.Add(newHistoryDetail);

                    }
                    else
                    {
                        var historyId = (from h in db.Histories
                                         where h.BuyTime == now && h.Username == User.Identity.Name
                                         select h.Id).First();
                        //var checkCount = (from c in db.HistoryDetails
                        //                  where c.HistoryId == historyId
                        //                  select c).Count();
                        //if (checkCount >= 10)
                        //{
                        //    break;
                        //}

                        var dupProductId = db.HistoryDetails.FirstOrDefault(p => p.ProductId == pid && p.HistoryId == historyId);
                        if (dupProductId == null)
                        {
                            var newHistoryDetail = new HistoryDetail();
                            newHistoryDetail.History = dupHistory;
                            newHistoryDetail.ProductId = pid;
                            newHistoryDetail.MinPrice = line.Product.MinPrice;
                            newHistoryDetail.MaxPrice = line.Product.MaxPrice;
                            db.HistoryDetails.Add(newHistoryDetail);
                        }
                    }
                    db.SaveChanges();

                }
                GetCart().Clear();
            }

            return RedirectToAction("BuyingHistory", "History");
        }