public IActionResult Authenticate(string username, string password) { User user = db.Users.FirstOrDefault(x => x.Username == username && x.Password == password); if (user == null) { ViewData["errMsg"] = "No such user or incorrect password"; return(View("Login")); } Session session = new Session() { Id = Guid.NewGuid().ToString(), UserId = user.UserId, Timestamp = DateTimeOffset.Now.ToUnixTimeSeconds() }; db.Sessions.Add(session); db.SaveChanges(); // the sequence of steps below matters. AddNewItemsToCart(Environment.MachineName, session.UserId.ToString()); Response.Cookies.Append("sessionId", session.Id); Response.Cookies.Append("userId", session.UserId.ToString()); Response.Cookies.Append("Username", db.Users.FirstOrDefault(x => x.UserId == session.UserId).Username); return(RedirectToAction("ListProducts", "Product")); }
/* check if username and password are both in Users db*/ public IActionResult Authenticate(string username, string password) { /* get record in Users db */ User user = db.Users.FirstOrDefault(x => x.Username == username && x.Password == password); /* if no record from Users db was returned, show error msg * else (record from Users db was returned), user is authenticated*/ /* authenication failed*/ //--- show error msg in Login page if (user == null) { ViewData["errMsg"] = "no such user or incorrect password"; return(View("Login")); } /* authentication passed*/ //-- create new session record in Sessions db Session session = new Session() { Id = Guid.NewGuid().ToString(), UserId = user.UserId, Timestamp = DateTimeOffset.Now.ToUnixTimeSeconds() }; db.Sessions.Add(session); db.SaveChanges(); //-- create a new cookie in the user's browser Response.Cookies.Append("sessionId", session.Id); // check if a cookie was sent to identify guest who added to cart string fakeUserId = HttpContext.Request.Cookies["cartItems"]; // if user added to cart before logging in, // add any new items to their existing cart // and return them to the View Cart page if (fakeUserId != null) { AddNewItemsToCart(session, fakeUserId); HttpContext.Response.Cookies.Delete("cartItems"); //delete the cookie that was used to track the guest(fakeUserId) return(RedirectToAction("Cart", "Cart")); } // else (user did not to cart before logging in,) // return user to Gallery page Response.Cookies.Append("username", username); return(RedirectToAction("Index", "Gallery")); }
//receive JSON data from Add.js. (When an item is added to the cart from gallery) public JsonResult AddItemToCart([FromBody] Addinput product) { string sessionId; try { sessionId = HttpContext.Request.Cookies["sessionId"]; } catch (NullReferenceException) { sessionId = null; } string userId; if (sessionId != null) { userId = HttpContext.Request.Cookies["userId"]; } else { userId = Environment.MachineName; } CartItem item = db.Cart.FirstOrDefault(x => x.UserId == userId && x.pId == product.ProductId); if (item == null) { item = new CartItem(); item.UserId = userId; item.pId = product.ProductId; item.Quantity = 1; item.product = db.Products.FirstOrDefault(x => x.ProductId == int.Parse(product.ProductId)); db.Add(item); } else { item.Quantity += 1; db.Update(item); } db.SaveChanges(); List <CartItem> cart = db.Cart.Where(x => x.UserId == userId).ToList(); int total = 0; foreach (CartItem x in cart) { total += x.Quantity; } return(Json(new { status = "success", total = total })); }
public IActionResult Authenticate(string username, string NewPWD, string ConfirmedPWD) { User user = db.Users.FirstOrDefault(x => x.Username == username); if (user == null) { if (NewPWD == ConfirmedPWD) { User users = new User() { Username = username, Password = NewPWD, }; db.Add(users); db.SaveChanges(); return(RedirectToAction("Login", "Login")); } else { ViewData["errMsg"] = "Please enter a consistent password"; return(View("Index")); } } else { ViewData["errMsg"] = "user has existed"; return(View("Index")); } }
public string Cart([FromBody] ChangeInput change)//receive JSON object from Cart.js when the number in the cart is changed { Session session = db.Sessions.FirstOrDefault(x => x.Id == HttpContext.Request.Cookies["sessionId"]); int tempProd = int.Parse(change.ProductId); int tempValue = int.Parse(change.Value); if (session == null) //if the user is not login { string fakeUserId = HttpContext.Request.Cookies["cartItems"]; //get the guest fakeuserid CartItem cartitem = cartitems.map[fakeUserId]; //get the guest cartitem information based on the fakeuserid (same idea as the sessions in workshop) //inside cartitem class is a list of KeyValuePair of <int,int> --> can refer to the cartitem class file for more information //KeyValuePair is used to store the productid and quantity of each of the products. for (int i = 0; i < cartitem.item.Count(); i++) { if (cartitem.item[i].Key == tempProd) //update the quantity of the KeyValuePair with the productId passed in { cartitem.item.Remove(cartitem.item[i]); cartitem.item.Add(new KeyValuePair <int, int>(tempProd, tempValue)); } } } else //else if the user is login { int userid = session.UserId; List <Cart> carts = db.Carts.Where(x => x.UserId == userid).ToList(); foreach (Cart item in carts)//Update the cart of the user with the new value into the databse { if (item.ProductId == tempProd) { item.Quantity = tempValue; db.SaveChanges(); } } ; } object data = new { status = "success" }; return(JsonSerializer.Serialize(data)); }
public static void AddActivationCode(Cart cart, Team5_Db db) { for (int i = 0; i < cart.Quantity; i++) { OrderDetail order = new OrderDetail { ActivationCode = Guid.NewGuid().ToString().Substring(3, 15), UserId = cart.UserId, ProductId = cart.ProductId, PurchaseDate = DateTime.Today.Date }; db.Add(order); db.SaveChanges(); } return; }
[HttpPost] //when the cart is submitted for purchase public IActionResult Transaction() { Session session = db.Sessions.FirstOrDefault(x => x.Id == HttpContext.Request.Cookies["sessionId"]); if (session == null)//if not login, redirect to login page { return(RedirectToAction("Login", "Login")); } int userid = session.UserId; List <Cart> carts = db.Carts.Where(x => x.UserId == userid).ToList(); foreach (Cart item in carts) //for each item in the carts { ActivationC.AddActivationCode(item, db); //call a method to generate the activationcode and update to the orderdetail database (please refer to the ActivationC class) db.Carts.Remove(item); //remove the item from the Carts database } db.SaveChanges(); return(RedirectToAction("Index")); }
//receive JSON data from Add.js. (When an item is added to the cart from gallery) public JsonResult Addin([FromBody] Addinput addinput) { Session session = db.Sessions.FirstOrDefault(x => x.Id == HttpContext.Request.Cookies["sessionId"]); bool newItem = true; int productid = int.Parse(addinput.ProductId); int total = 0; //if the user is not login, their session will be null if (session == null) { //fakeUserId is a cookies that will be sent to the guest(not login) to track their activity string fakeUserId = HttpContext.Request.Cookies["cartItems"]; CartItem cartitem; //if they dont have this cookies(null), create a cookie of fakeUserId and send to them if (fakeUserId == null) { fakeUserId = Guid.NewGuid().ToString(); Response.Cookies.Append("cartItems", fakeUserId); cartitem = new CartItem(); } else //else if they have the fakeUserId cookie, find their record from the cartitems { cartitem = null; cartitems.map.TryGetValue(fakeUserId, out cartitem); //check if the fakeUserId is in our record or not if (cartitem == null) //if its not in our record { cartitem = new CartItem(); } } //inside cartitem class is a list of KeyValuePair of <int,int> --> can refer to the cartitem class file for more information //KeyValuePair is used to store the productid and quantity of each of the products. foreach (KeyValuePair <int, int> item in cartitem.item) { if (item.Key == productid) //to check if the product has been added before { newItem = false; } } if (cartitem.item.Count() == 0 || newItem == true) //if its a new product or the list is empty { cartitem.item.Add(new KeyValuePair <int, int>(productid, 1)); //Add a new KeyValuePair of this productid with quantity of 1 } else { for (int i = 0; i < cartitem.item.Count(); i++) { if (cartitem.item[i].Key == productid) //get the KeyValuePair of that particular product and increment the quantity by 1 { int quantity = cartitem.item[i].Value; cartitem.item.Remove(cartitem.item[i]); cartitem.item.Add(new KeyValuePair <int, int>(productid, quantity + 1)); break; } } } cartitems.map[fakeUserId] = cartitem; //update the cartitems (cartitems is a singleton object that store cartitem of guest)(Same idea as sessions in the workshop) //this part is to get the total quantity of products that the guest has. So that can be reflected on the cart image. for (int i = 0; i < cartitem.item.Count(); i++) { total += cartitem.item[i].Value; } } else //else if the user has login { List <Cart> carts = db.Carts.Where(x => x.UserId == session.UserId).ToList(); //get the carts information of the user foreach (Cart item in carts) //check if its a item or not { if (item.ProductId == productid) { newItem = false; } } if (carts.Count() == 0 || newItem == true) //if its a new item or the user has no items in his cart { Cart item = new Cart { UserId = session.UserId, ProductId = productid, Quantity = 1 }; db.Add(item); db.SaveChanges();//save the item to the user cart database } else //else if its not a new item { foreach (Cart item in carts)//get the cart row and increment the quantity by 1 { if (item.ProductId == productid) { item.Quantity += 1; db.SaveChanges(); } } } //this part is to get the total quantity of products that the user has. So that can be reflected on the cart image. carts = db.Carts.Where(x => x.UserId == session.UserId).ToList(); foreach (Cart item in carts) { total += item.Quantity; } } //return the total as JSON to the Add.js return(Json(new { status = "success", total = total })); }