/// <Summary>
/// Add Cookie
/// </Summary>
/// <Param
Public static int AddCookies (GoodsCookie gs)
{
String userID = null;
If (HttpContext. Current. Session ["hasLogin"]! = Null)
{
UserID = HttpContext. Current. Session ["hasLogin"]. ToString (); // log on
}
ShoppingMallContext db = new ShoppingMallContext ();
Var cookies = HttpContext. Current. Request. Cookies ["MyShoppingCart"]; // create a Cookie and name it
If (cookies = null)
{
String strCookie = gs. skuID + "-" + gs. count + ",";
Cookies = new HttpCookie ("MyShoppingCart") // object initialization
{
Expires = DateTime. Today. AddDays (1), // expiration time
Value = strCookie // Cookie Value assignment
};
HttpContext. Current. Response. Cookies. Add (cookies); // Add to Cookie
If (userID! = Null) // if the user logs on
{
SkuShoppingCart SC = new SkuShoppingCart (); // initialize the shopping cart class.
SC. GoodsSkuID = gs. skuID; // assign a value to the attributes of the class.
SC. ShoppingCartID = db. ShoppingCarts // find the corresponding shopping cart ID based on the user ID
. Where (f => f. UserID = userID)
. Select (f => f. ShoppingCartID)
. Single ();
SC. SkuCount = gs. Count; // Count
Db. SkuShoppingCarts. Add (SC); // Add to database
}
Return 1;
}
Else
{
If (String. IsNullOrWhiteSpace (cookies. Value) // judge whether the Value in Cookies is not empty
{
String strCookie = gs. skuID + "-" + gs. count + ",";
Cookies = new HttpCookie ("MyShoppingCart") // object initialization
{
Expires = DateTime. Today. AddDays (1 ),
Value = strCookie
};
HttpContext. Current. Response. Cookies. Add (cookies); // Add to Cookie
If (userID! = Null)
{
SkuShoppingCart SC = new SkuShoppingCart ();
SC. GoodsSkuID = gs. skuID; // assign a value to the attributes of the class.
SC. ShoppingCartID = db. ShoppingCarts // find the corresponding shopping cart ID based on the user ID
. Where (f => f. UserID = userID)
. Select (f => f. ShoppingCartID)
. Single ();
SC. SkuCount = gs. Count; // Count
Db. SkuShoppingCarts. Add (SC); // Add to database
}
Return 1;
}
Else // return
{
# Region ----- add to dictionary -----
Dictionary <int, int> dict = new Dictionary <int, int> (); // initialize the Dictionary
String goods = cookies. Value; // declare a string variable and assign a Value to Cookie
Goods = goods. Substring (0, goods. Length-1); // specify the string Length
String [] skuidArray = goods. Split (','); // disassemble the string and assign it to the skuidArray
Int count = 0; // declare an Int variable
Foreach (var I in skuidArray) // iteration Array
{
If (I = "" | I = null) // if no value exists
{
Continue; // continue
}
Else // if any
{
String [] strCont = I. Split ('-'); // disassemble the string
Int key = int. Parse (strCont [0]); // The first
Int value = int. Parse (strCont [1]); // The second
// Check whether the key exists in the dictionary.
If (dict. ContainsKey (key ))
{
Dict [key] + = value; // overlay if any
}
Else // No
{
Dict. Add (key, value); // Add to Dictionary
}
}
}
# Endregion
# Region ------- dictionary judgment -------
If (dict. ContainsKey (gs. skuID) // if the dictionary contains this ID
{
Dict [gs. skuID] + = gs. count;
Count + = skuidArray. Count (); // Add the number of them.
}
Else
{
Dict. Add (gs. skuID, gs. count );
Count = skuidArray. Count () + 1; // if not, add a new
}
String countStr = "";
Foreach (var v in dict) // re-iterate the dictionary to spell the string
{
CountStr + = v. Key + "-" + v. Value + ","; // Key-Value Pair
}
Cookies = new HttpCookie (countStr)
{
Expires = DateTime. Today. AddDays (1 ),
Value = countStr
}; // Added to the Cookie
HttpContext. Current. Request. Cookies. Add (cookies );
If (userID! = Null) // if the user logs on
{
SkuShoppingCart SC = new SkuShoppingCart (); // initialize the shopping cart class.
SC. GoodsSkuID = gs. skuID; // assign a value to the attributes of the class.
SC. ShoppingCartID = db. ShoppingCarts // find the corresponding shopping cart ID based on the user ID
. Where (f => f. UserID = userID)
. Select (f => f. ShoppingCartID)
. Single ();
SC. SkuCount = gs. Count; // Count
Db. SkuShoppingCarts. Add (SC); // Add to database
}
Return count; // quantity
# Endregion
}
}
}