public void updateShoppingBasket() { try { //SqlConnection connection = Connector.getInstance().getSQLConnection(); lock (connection) { connection.Open(); foreach (KeyValuePair <string, SubscribedUser> pair in users) { string username = pair.Key; SubscribedUser su = pair.Value; string sql = "SELECT * FROM BasketCart WHERE username=@username;"; var c2 = connection.Query <BasketCartEntry>(sql, new { username = username }); ShoppingBasket sb = su.getShoppingBasket(); if (Enumerable.Count(c2) > 0) { for (int i = 0; i < Enumerable.Count(c2); i++) { BasketCartEntry bc = c2.ElementAt(i); int storeID = bc.getStoreID(); sql = "SELECT * FROM CartProduct WHERE storeID=@storeID AND username=@username;"; var c3 = connection.Query <CartProductEntry>(sql, new { storeID, username }); for (int j = 0; j < Enumerable.Count(c3); j++) { CartProductEntry cp = c3.ElementAt(j); int productID = cp.getProductID(); int amount = cp.getAmount(); Product p = DBProduct.getInstance().getProductByID(productID); sb.addToCartNoDBUpdate(p, amount, storeID); } } } List <StoreRole> storeRoles = su.getStoreRoles(); foreach (StoreRole sr in DBStore.getInstance().getAllStoreRoles(username)) { if (sr.getUser().getUsername() == username) { storeRoles.Add(sr); } } } connection.Close(); } } catch (Exception e) { connection.Close(); } }
public SubscribedUser getSubscribedUser(string username) { if (users.ContainsKey(username)) { return(users[username]); } try { //SqlConnection connection = Connector.getInstance().getSQLConnection(); lock (connection) { connection.Open(); var c1 = connection.Query <RegisterEntry>("SELECT username, password FROM [dbo].[Register] WHERE username=@username ", new { username = username }); if (Enumerable.Count(c1) == 1) { RegisterEntry re = c1.ElementAt(0); string password = re.getPassword(); string sql = "SELECT * FROM BasketCart WHERE username=@username;"; var c2 = connection.Query <BasketCartEntry>(sql, new { username = username }); ShoppingBasket sb = new ShoppingBasket(username); SubscribedUser su = new SubscribedUser(username, password, sb); List <StoreRole> storeRoles = su.getStoreRoles(); users.Add(username, su); DBStore.getInstance().getAllStoreRoles(username); if (Enumerable.Count(c2) > 0) { for (int i = 0; i < Enumerable.Count(c2); i++) { BasketCartEntry bc = c2.ElementAt(i); int storeID = bc.getStoreID(); sql = "SELECT * FROM CartProduct WHERE storeID=@storeID AND username=@username;"; var c3 = connection.Query <CartProductEntry>(sql, new { storeID, username }); for (int j = 0; j < Enumerable.Count(c3); j++) { CartProductEntry cp = c3.ElementAt(j); int productID = cp.getProductID(); int amount = cp.getAmount(); Product p = DBProduct.getInstance().getProductByID(productID); sb.addToCartNoDBUpdate(p, amount, storeID); } } } //foreach (StoreRole sr in DBStore.getInstance().getAllStoreRoles(username)) //{ // if(sr.getUser().getUsername()==username) // { // storeRoles.Add(sr); // } //} //users.Add(username, su); connection.Close(); return(su); } else { connection.Close(); return(null); } } } catch (Exception e) { connection.Close(); return(null); } }