Exemple #1
0
 static void Main(string[] args)
 {
     ShopService.SavePurchasesToXml("D:\\1.xml");
     //Purchase p2 = new Purchase()
     //{
     //    Date = DateTime.Now,
     //    UserId = 322,
     //    PurchaseProducts = new PurchaseProduct[] { new PurchaseProduct { ProductId = 9, Quantity = 12 } }
     //};
     //Purchase p1 = new Purchase { PurchaseId = 6 };
     //ShopService.UpdatePurchase(p1, p2);
     //Product tmp = new Product { Name = "broiler",Price=0,Category=1 };
     //Purchase p1 = new Purchase { Date = DateTime.Now, UserId = 22,
     //    PurchaseProducts = new PurchaseProduct[] { new PurchaseProduct { ProductId = 10 } , new PurchaseProduct {ProductId = 2,Quantity=2 } } };
     //ShopService.AddPurchase(p1);
     using (CodeFirstExistingDb.ShopModel dbc = new CodeFirstExistingDb.ShopModel())
     {
         //DbFirst.Product temp = new DbFirst.Product() { Category = DbFirst.ProductCategory.groceries, ProductId = 3 };
         //List<DbFirst.Product> a = dbc.Products.ToList<DbFirst.Product>();
         //if (a.Contains(temp)) Console.WriteLine("yes");
         //else Console.WriteLine("no");
         foreach (var item in dbc.Purchases)
         {
             Console.WriteLine("Username with id " + item.UserId + " bought on " + "{0:d}" + " this goods:", item.Date.Value);
             foreach (var product in item.PurchaseProducts)
             {
                 Console.WriteLine(product.Quantity.ToString() + " " + product.Product.Category
                                   + " pieces of " + product.Product.Name + ", total price:" + "{0:c}", product.Quantity * product.Product.Price);
             }
             Console.WriteLine();
         }
     }
     Console.ReadKey();
 }
Exemple #2
0
 public static void UpdateProduct(Product itemtoupdate, Product itemupdated)
 {
     using (ShopModel db = new ShopModel())
     {
         Product tmp = FindProduct(itemtoupdate.ProductId, db);
         tmp.Name  = itemupdated.Name;
         tmp.Price = itemupdated.Price;
         tmp.Image = itemtoupdate.Image;
         tmp.PurchaseProducts.Clear();
         foreach (var productPosition in itemupdated.PurchaseProducts)
         {
             tmp.PurchaseProducts.Add(productPosition);
         }
         db.SaveChanges();
     }
 }
Exemple #3
0
 public static void SavePurchasesToXml(string filename)
 {
     using (ShopModel db = new ShopModel())
     {
         XElement Purchases = new XElement("Purchases", from c in db.Purchases.ToList()
                                           select new XElement("Purchase",
                                                               new XElement("PurchaseID", c.PurchaseId),
                                                               new XElement("UserID", c.UserId),
                                                               new XElement("Products", from n in c.PurchaseProducts
                                                                            select new XElement("Product",
                                                                                                new XElement("ProductId", n.ProductId),
                                                                                                new XElement("Quantity", n.Quantity)))));
         XDocument tmp = new XDocument(Purchases);
         tmp.Save(filename);
     }
 }
Exemple #4
0
 public static void AddPurchase(Purchase purchase)
 {
     if (!ValidatePurchase(purchase))
     {
         throw new ArgumentException("invalid purchase fields");
     }
     using (ShopModel db = new ShopModel())
     {
         db.Purchases.Add(purchase);
         foreach (var item in purchase.PurchaseProducts)
         {
             AddProductPosition(item, db);
         }
         db.SaveChanges();
     }
 }
Exemple #5
0
 public static void UpdatePurchase(Purchase itemtoupdate, Purchase itemupdated)
 {
     using (ShopModel db = new ShopModel())
     {
         Purchase tmp = FindPurchase(itemtoupdate.PurchaseId, db);
         Console.WriteLine(tmp.UserId);
         tmp.UserId = itemupdated.UserId;
         tmp.Date   = itemupdated.Date;
         Console.WriteLine(tmp.UserId);
         tmp.PurchaseProducts.Clear();
         foreach (var productPosition in itemupdated.PurchaseProducts)
         {
             tmp.PurchaseProducts.Add(productPosition);
         }
         db.SaveChanges();
     }
 }
Exemple #6
0
 public static void RemoveProduct(int itemid)
 {
     using (ShopModel db = new ShopModel())
     {
         Product tmp = FindProduct(itemid, db);
         List <PurchaseProduct> pptoremove = new List <PurchaseProduct>();
         foreach (var item in tmp.PurchaseProducts)
         {
             pptoremove.Add(item);
         }
         foreach (var item in pptoremove)
         {
             db.PurchaseProducts.Remove(item);
         }
         db.Products.Remove(tmp);
         db.SaveChanges();
     }
 }
Exemple #7
0
 private static void AddProductPosition(PurchaseProduct p, ShopModel db)
 {
     if (p == null)
     {
         return;
     }
     if (p.PurchaseId != 0)
     {
         throw new ArgumentException("you can't update other purchases using this method");
     }
     if (FindProduct(p.ProductId, db) == null)
     {
         throw new ArgumentException("Unknown product in purchase");
     }
     if (p.Quantity == null)
     {
         p.Quantity = 1;
     }
     db.PurchaseProducts.Add(p);
 }
Exemple #8
0
 public static void UpdateProduct(int itemid, string newname = null, decimal?newprice = null, byte[] newimage = null, int?newcategory = null)
 {
     using (ShopModel db = new ShopModel())
     {
         Product tmp = FindProduct(itemid, db);
         if (newname != null)
         {
             tmp.Name = newname;
         }
         if (newprice != null)
         {
             tmp.Price = newprice;
         }
         if (newimage != null)
         {
             tmp.Image = newimage;
         }
         if (newcategory != null)
         {
             tmp.Category = newcategory;
         }
         db.SaveChanges();
     }
 }