Beispiel #1
0
        public List<CartItem> getUserCart(string UserID)
        {
            bool status = false;
            HttpContext.Current.Session["status"] = "DefaultMessage";
            string conStr = ConfigurationManager.ConnectionStrings["FashionableMeDB"].ConnectionString;
            SqlConnection conn = new SqlConnection(conStr);
            List<CartItem> cartItems = new List<CartItem>();
            try
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand("SELECT * FROM SavedCart WHERE UserID=@UserID", conn);
                cmd.Parameters.AddWithValue("UserID", UserID.Trim());
                var reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        CartItem cartItem = new CartItem();

                        string apparelID = reader.GetString(reader.GetOrdinal("ApparelID"));
                        string apparelSize = reader.GetString(reader.GetOrdinal("ApparelSize"));
                        string offerID = reader.GetString(reader.GetOrdinal("OfferID"));
                        int quantity = reader.GetInt32(reader.GetOrdinal("Quantity"));
                        cartItem.OfferID = offerID;
                        cartItem.Apparel.ApparelSize = apparelSize;
                        cartItem.Apparel.ApparelID = Convert.ToInt32(apparelID);

                        CustomerDal custDal = new CustomerDal();
                        Quantity quantityDetails = custDal.GetQuantityDetailForApparel(Convert.ToInt32(apparelID), apparelSize);

                        int availableQuantity = (quantityDetails.ApparelQuantity >= quantity) ? quantity : quantityDetails.ApparelQuantity;
                        if(availableQuantity!=quantity)
                            HttpContext.Current.Session["cartUpdated"] = "true";

                        if(availableQuantity >0 )
                        {
                            cartItem.Quantity = availableQuantity;
                            Apparel apparel = new Apparel();
                            apparel = custDal.GetApparelByID(Convert.ToInt32(apparelID))[0];

                            cartItem.Apparel.ApparelName = apparel.ApparelName;
                            cartItem.Apparel.ApparelCost = quantityDetails.ApparelCost;
                            cartItem.Apparel.ApparelDiscount = quantityDetails.ApparelDiscount;
                            if(!(offerID.Trim().Equals("NOOFF")))
                            {
                                AdminDal adminDal = new AdminDal();
                                Offer offer = new Offer();
                                offer = adminDal.GetOfferDateAndDiscountByID(offerID);
                                if(System.DateTime.Equals(offer.OfferDate, DateTime.Now.Date))
                                    cartItem.Apparel.ApparelDiscount += offer.Discount;
                            }
                            cartItems.Add(cartItem);
                        }

                    }

                }
                conn.Close();
                conn.Open();
                SqlCommand cmd2 = new SqlCommand("DELETE FROM SavedCart WHERE UserID=@UserID", conn);
                cmd2.Parameters.AddWithValue("UserID", UserID.Trim());
                var reader2 = cmd2.ExecuteNonQuery();

            }
            catch (Exception exc)
            {
                HttpContext.Current.Session["ErrorMessage"] = exc.Message;
            }
            conn.Close();
            return cartItems;
        }