예제 #1
0
        //public ActionResult AddNew([Bind(Include = "OverallRating,Title,Content")] ReviewProduction1 model)
        public async Task <ActionResult> AddNewReview(ReviewProduction model)
        {
            if (UserManager.User != null)
            {
                if (ModelState.IsValid)
                {
                    // save to DB
                    DBModelContainer db = new DBModelContainer();

                    Review review = new Review();
                    review.UserId        = UserManager.User.Id;
                    review.ProductId     = model.ProductId;
                    review.ReviewDate    = DateTime.Now;
                    review.Title         = model.Title;
                    review.Content       = model.Content;
                    review.OverallRating = model.OverallRating;

                    db.Reviews.Add(review);
                    await db.SaveChangesAsync();

                    // Info.

                    /*
                     * return this.Json(new
                     * {
                     *  EnableSuccess = true,
                     *  SuccessTitle = "Success",
                     *  SuccessMsg = "Add new review sucessfully"
                     * });*/

                    List <ReviewProduction> reviewList = (from r in db.Reviews
                                                          join u in db.Users on r.UserId equals u.UserId
                                                          where r.ProductId == model.ProductId
                                                          select new ReviewProduction
                    {
                        ReviewId = r.ReviewId,
                        Title = r.Title,
                        Content = r.Content,
                        OverallRating = r.OverallRating,
                        UserName = u.UserName,
                        ReviewDate = r.ReviewDate,
                    }).ToList();
                    return(PartialView("_PartialPage_ReviewList", reviewList));
                }

                return(this.Json(new
                {
                    ResponseType = Config.SOMETHING_WRONG_WITH_POST_REQUEST,
                    Msg = "Something goes wrong, please try again later"
                }));
            }

            return(this.Json(new
            {
                ResponseType = Config.NEED_LOGIN,
                Msg = "Please login first"
            }));
        }
예제 #2
0
        public async Task <ActionResult> AddToCart(CartItem item)
        {
            if (ModelState.IsValid)
            {
                Cart       cart   = null;
                CartDetail detail = null;

                if (UserManager.User != null)
                {
                    // get existing Cart
                    cart = (from c in db.Carts
                            where c.UserId == UserManager.User.Id
                            select c).FirstOrDefault();

                    // there is no existing Shopping Cart for this user -> create new Shopping Cart
                    if (cart == null)
                    {
                        // create new Shopping Cart
                        cart          = new Cart();
                        cart.DateOpen = System.DateTime.Now;
                        cart.UserId   = UserManager.User.Id;
                        db.Carts.Add(cart);

                        // save Cart Item
                        detail        = new CartDetail();
                        detail.Amount = item.Amount;
                        db.CartDetails.Add(detail);
                    }
                    else  // there is already an existing Shopping Cart for this user
                    {
                        // get Cart Item for this ProductDetailId
                        detail = (from cd in db.CartDetails
                                  where cd.CartId == cart.CartId && cd.ProductDetailsId == item.ProductDetailsId
                                  select cd).FirstOrDefault();
                        if (detail == null)
                        {
                            detail        = new CartDetail();
                            detail.Amount = item.Amount;
                            db.CartDetails.Add(detail);
                        }
                        else
                        {
                            detail.Amount = (short)(detail.Amount + item.Amount);
                        }
                    }

                    detail.ExtendedPrice    = item.Price;
                    detail.ProductDetailsId = item.ProductDetailsId;
                    detail.CartId           = cart.CartId;

                    await db.SaveChangesAsync();
                }
                else
                {
                    // get tmp Cart in cookie
                    string     dateOfOpen = string.Empty;
                    HttpCookie reqCookies = Request.Cookies["CartInfo"];
                    if (reqCookies != null)  // there is a temporary Cart in cookies
                    {
                        dateOfOpen = reqCookies["DateOfOpen"].ToString();

                        /* save Cart Item in cookie */
                        // read saved cart item for given ProductDetailId
                        HttpCookie reqCartItemCookies = Request.Cookies["CartItems[" + item.ProductDetailsId.ToString() + "]"]; // cartItem from request
                        HttpCookie respCartItemCookie;                                                                          // cartItem in response
                        if (reqCartItemCookies != null)
                        {
                            CartItem mItem = new JavaScriptSerializer().Deserialize <CartItem>(reqCartItemCookies.Value);
                            mItem.Amount = (short)(mItem.Amount + item.Amount);
                            string myObjectJson = new JavaScriptSerializer().Serialize(mItem);
                            respCartItemCookie = new HttpCookie("CartItems[" + item.ProductDetailsId.ToString() + "]", myObjectJson)
                            {
                                Expires = DateTime.Now.AddDays(1)
                            };
                        }
                        else
                        {
                            item.Amount = 1;
                            string myObjectJson = new JavaScriptSerializer().Serialize(item);
                            respCartItemCookie = new HttpCookie("CartItems[" + item.ProductDetailsId + "]", myObjectJson)
                            {
                                Expires = DateTime.Now.AddDays(1)
                            };
                        }
                        HttpContext.Response.Cookies.Add(respCartItemCookie);

                        /* add productDetailID in cookies */
                        HttpCookie reqIDListCookies = Request.Cookies["ProductDetailIDlist"];
                        if (reqIDListCookies != null)
                        {
                            string     yourListString = string.Empty;
                            string     dataAsString   = reqIDListCookies.Value;
                            List <int> listdata       = new List <int>();
                            if (!dataAsString.Equals(string.Empty))
                            {
                                listdata = dataAsString.Split(',').Select(x => Convert.ToInt32(x)).ToList();
                            }
                            if (!listdata.Contains(item.ProductDetailsId))
                            {
                                listdata.Add(item.ProductDetailsId);
                            }
                            // Stringify your list
                            yourListString = String.Join(",", listdata);
                            HttpCookie IDListCookies = new HttpCookie("ProductDetailIDlist", yourListString)
                            {
                                Expires = DateTime.Now.AddDays(1)
                            };
                            HttpContext.Response.Cookies.Add(IDListCookies);
                        }
                        else
                        {
                            List <int> listdata = new List <int>();
                            listdata.Add(item.ProductDetailsId);

                            // Stringify your list
                            var        yourListString = String.Join(",", listdata);
                            HttpCookie IDListCookies  = new HttpCookie("ProductDetailIDlist", yourListString)
                            {
                                Expires = DateTime.Now.AddDays(1)
                            };
                            HttpContext.Response.Cookies.Add(IDListCookies);
                        }
                    }
                    else  // There is not any tmp Shopping Cart for this user
                    {
                        // create new Shopping Cart in Cookie
                        HttpCookie cartCookie = new HttpCookie("CartInfo")
                        {
                            Expires = DateTime.Now.AddYears(1)
                        };
                        cartCookie["DateOfOpen"] = DateTime.Now.ToString();
                        HttpContext.Response.Cookies.Add(cartCookie);

                        // save Cart Item in cookie
                        item.Amount = 1;
                        string     myObjectJson   = new JavaScriptSerializer().Serialize(item);
                        HttpCookie cartItemCookie = new HttpCookie("CartItems[" + item.ProductDetailsId + "]", myObjectJson)
                        {
                            Expires = DateTime.Now.AddDays(1)
                        };
                        HttpContext.Response.Cookies.Add(cartItemCookie);

                        /* add productDetailID into cookies */
                        List <int> listdata = new List <int>();
                        listdata.Add(item.ProductDetailsId);
                        // Stringify your list
                        var        yourListString = String.Join(",", listdata);
                        HttpCookie IDListCookies  = new HttpCookie("ProductDetailIDlist", yourListString)
                        {
                            Expires = DateTime.Now.AddDays(1)
                        };
                        HttpContext.Response.Cookies.Add(IDListCookies);
                    }
                }

                ModelState.Clear();
                return(this.Json(new
                {
                    EnableSuccess = true,
                    SuccessTitle = "Successful!",
                    SuccessMsg = "Add cart successfully order!"
                }));
            }

            return(this.Json(new
            {
                EnableError = true,
                ErrorTitle = "Error",
                ErrorMsg = "Something goes wrong, please try again later"
            }));
        }
예제 #3
0
        //public ActionResult Login(String username, String password)
        public async Task <ActionResult> Login([Bind(Include = "Username,Password")] Logon logon)
        {
            if (ModelState.IsValid)
            {
                // Authenticate the user.
                if (UserManager.ValidateUser(logon, Response))
                {
                    /* move temporary shopping cart to DB */
                    string dateOfOpen = string.Empty;

                    // get tmp Cart in cookie
                    HttpCookie reqCartInfoCookies = HttpContext.Request.Cookies["CartInfo"];
                    if (reqCartInfoCookies != null)  // there is a temporary Cart in cookies
                    {
                        dateOfOpen = reqCartInfoCookies["DateOfOpen"].ToString();

                        Cart cart = null;
                        // get existing Cart
                        cart = (from c in db.Carts
                                where c.UserId == UserManager.User.Id
                                select c).FirstOrDefault();

                        // there is no existing Shopping Cart for this user -> create new Shopping Cart
                        if (cart == null)
                        {
                            cart          = new Cart();
                            cart.UserId   = UserManager.User.Id;
                            cart.DateOpen = DateTime.Parse(dateOfOpen);
                            db.Carts.Add(cart);
                        }

                        HttpCookie reqIDListCookies = Request.Cookies["ProductDetailIDlist"];
                        if (reqIDListCookies != null)
                        {
                            string dataAsString = reqIDListCookies.Value;
                            if (!dataAsString.Equals(string.Empty))
                            {
                                List <int> listdata = new List <int>();
                                //List<CartItem> listCartItem = new List<CartItem>();
                                listdata = dataAsString.Split(',').Select(x => Int32.Parse(x)).ToList();
                                for (int i = 0; i < listdata.Count(); i++)
                                {
                                    HttpCookie reqCartItemCookies = Request.Cookies["CartItems[" + listdata[i].ToString() + "]"];
                                    if (reqCartItemCookies != null)
                                    {
                                        CartItem cookiesItem = new JavaScriptSerializer().Deserialize <CartItem>(reqCartItemCookies.Value);

                                        // get Cart Item for this ProductDetailId in DB
                                        CartDetail detail;
                                        int        productDetailId = listdata[i];
                                        detail = (from cd in db.CartDetails
                                                  where cd.CartId == cart.CartId && cd.ProductDetailsId == productDetailId
                                                  select cd).FirstOrDefault();
                                        if (detail == null)
                                        {
                                            detail                  = new CartDetail();
                                            detail.Amount           = (short)cookiesItem.Amount;
                                            detail.ExtendedPrice    = cookiesItem.Price;
                                            detail.Type             = 0;
                                            detail.ProductDetailsId = listdata[i];
                                            detail.CartId           = cart.CartId;
                                            db.CartDetails.Add(detail);
                                        }
                                        else
                                        {
                                            detail.Amount += (short)cookiesItem.Amount;
                                        }

                                        await db.SaveChangesAsync();

                                        /* remove cart item of this ProductDetailId in cookies */
                                        var respCartItemscookies = new HttpCookie("CartItems[" + listdata[i].ToString() + "]");
                                        respCartItemscookies.Expires = DateTime.Now.AddDays(-1D);
                                        Response.Cookies.Add(respCartItemscookies);
                                    }
                                }

                                /* update productDetailID list in cookies */
                                HttpCookie respIDListCookies = new HttpCookie("ProductDetailIDlist", "")
                                {
                                    Expires = DateTime.Now.AddDays(1)
                                };
                                HttpContext.Response.Cookies.Add(respIDListCookies);
                            }
                        }
                    }

                    // Redirect to the secure area.
                    return(RedirectToAction("Index", "Category"));
                }
            }

            ViewBag.LoginFailure = 1;
            return(View());
        }