-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookieCartParser.cs
124 lines (102 loc) · 4.22 KB
/
CookieCartParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Add_To_Cart_v1
{
/// <summary>
/// The functions of this class is used to manipulate the cart data stored in a cookie.
/// </summary>
public class CookieCartParser
{
Dictionary<string, string> dictCookie;
public CookieCartParser() { }
/// <summary>
/// Parses the cookie cart into a Dictionary, making it more accessible
/// </summary>
/// <param name="strCookie">cookie string taken from the HTTPCookie with the pattern => id=quantity,id=quantity,..</param>
/// <returns>Dictionary equivalent of strCookie</returns>
public Dictionary<string, string> ToDictionary(string strCookie)
{
dictCookie = new Dictionary<string, string>();
dictCookie = strCookie.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(part => part.Split('=')).ToDictionary(split => split[0], split => split[1]);
return dictCookie;
}
/// <summary>
/// Adds new item to the string and returns the newly created string cookie
/// </summary>
/// <param name="newItemID">The item to be added to the cookie</param>
/// <param name="strCookie">Cookie string taken from the HTTPCookie</param>
/// <returns>New cookie string after addition</returns>
public string Add(string newItemID, string strCookie)
{
dictCookie = ToDictionary(strCookie);
if (dictCookie.Keys.Contains(newItemID))
{
int item_quantity = int.Parse(dictCookie[newItemID]);
item_quantity++;
dictCookie[newItemID] = item_quantity.ToString();
}
else
{
dictCookie[newItemID] = "1";
}
return Stringify(dictCookie);
}
/// <summary>
/// Removes the item from the cookie
/// </summary>
/// <param name="itemIDToRemove">The item to be removed</param>
/// <param name="strCookie">Cookie string taken from the HTTPCookie</param>
/// <returns>New cookie string after removal</returns>
public string Remove(string itemIDToRemove, string strCookie)
{
dictCookie = ToDictionary(strCookie);
dictCookie.Remove(itemIDToRemove);
return Stringify(dictCookie);
}
/// <summary>
/// Updates the item data in the cookie string
/// </summary>
/// <param name="itemIDToUpdate">The item to be updated</param>
/// <param name="newData">New quantity of the item</param>
/// <param name="strCookie">Cookie string taken from the HTTPCookie</param>
/// <returns>New cookie string after update</returns>
public string Update(string itemIDToUpdate, string newData, string strCookie)
{
dictCookie = ToDictionary(strCookie);
if (dictCookie.Keys.Contains(itemIDToUpdate))
{
dictCookie[itemIDToUpdate] = newData;
}
return Stringify(dictCookie);
}
/// <summary>
/// Converts the Dictionary cookie into a string
/// </summary>
/// <param name="dictCookie">The dictionary containing the data from cookie</param>
/// <returns>String equivalent of the cookie</returns>
public string Stringify(Dictionary<string, string> dictCookie)
{
string strCookie = string.Join(",", dictCookie.Select(x => x.Key + "=" + x.Value).ToArray());
//strCookie += ",";
return strCookie;
}
/// <summary>
/// Gets the number of items in the cookie
/// </summary>
/// <param name="strCookie">Cookie string taken from the HTTPCookie</param>
/// <returns>Number of items</returns>
public int GetNumberOfItems(string strCookie)
{
int count = 0;
dictCookie = ToDictionary(strCookie);
foreach (KeyValuePair<string, string> item in dictCookie)
{
int item_count = int.Parse(item.Value);
count += item_count;
}
return count;
}
}
}