public static ObservableCollection<Purchase> GetPurchases(Profile profile) { _loadPurchasesUserCache = new List<User>(); _loadPurchasesArticleCache = new List<Article>(); ObservableCollection<Purchase> purchases = new ObservableCollection<Purchase>(); GetPurchaseArticleCache = new List<Article>(); GetPurchaseUserCache = new List<User>(); int currentPurchaseId = -1; Purchase p = null; Cart c = null; User u = null; Article a = null; try { SqlConnection connection = new SqlConnection(ConnectionString); SqlCommand cmd = new SqlCommand("SELECT PurchaseID, UserID, ArticleID, ArticleCount, [Date] FROM vPurchaseProfile WHERE ProfileID = " + profile.ProfileID + " ORDER BY [Date] DESC", connection); connection.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { int pid = Convert.ToInt32(reader["PurchaseID"]); int artId = Convert.ToInt32(reader["ArticleID"]); // article cache a = (from art in _loadPurchasesArticleCache where art.ArticleID == artId select art as Article).FirstOrDefault(); if (a == null) { a = GetArticle(artId); _loadPurchasesArticleCache.Add(a); } if (currentPurchaseId == -1 || pid != currentPurchaseId) // create new purchase object { int userId = Convert.ToInt32(reader["UserID"]); if (p != null) purchases.Add(p); // user cache u = (from usr in _loadPurchasesUserCache where usr.UserID == userId select usr as User).FirstOrDefault(); if (u == null) { u = GetUser(userId); _loadPurchasesUserCache.Add(u); } currentPurchaseId = pid; c = new Cart(); p = new Purchase(pid, c, u, Convert.ToDateTime(reader["Date"])); c.Add(a, Convert.ToInt32(reader["ArticleCount"])); } else // add article to purchase object { c.Add(a, Convert.ToInt32(reader["ArticleCount"])); } } if (!purchases.Contains(p) && p != null) purchases.Add(p); connection.Close(); } catch (Exception ex) { string errmsg = "Fehler beim Abrufen der Verkäufe.\n\n"; errmsg += "DatabaseHandler.GetPurchases(profile): " + ex.ToString(); throw new Exception(errmsg); } return purchases; }
public static void RemovePurchase(Purchase purchase) { try { string query = "DELETE Purchase WHERE PurchaseID = " + purchase.PurchaseID; SqlConnection connection = new SqlConnection(ConnectionString); SqlCommand cmd = new SqlCommand(query, connection); connection.Open(); cmd.ExecuteNonQuery(); connection.Close(); } catch (Exception ex) { string errmsg = "Fehler beim Löschen des Verkaufs.\n\n"; errmsg += "DatabaseHandler.RemovePurchase(purchase): " + ex.ToString(); throw new Exception(errmsg); } }
public bool ArticleHitTest(Purchase p) { if (this.users != null) { bool containsUser = false; foreach (User u in this.users) { if (u.UserID == p.User.UserID) { containsUser = true; break; } } if (!containsUser) return false; } if (p.Count < minCount || p.Count > maxCount) return false; if (p.Amount < minAmount || p.Amount > maxAmount) return false; if (p.Date < minDateTime || p.Date > maxDateTime) return false; return true; }