public List<Item> selectAll()
 {
     using (var db = new CustomDBContext())
     {
         try
         {
             List<Item> items = db.Items.Select(item => new Item()
             {
                 ID = item.ID,
                 name = item.name,
                 country = item.country,
                 district = item.district,
                 year = item.year,
                 type = item.type,
                 price = item.price,
                 description = item.description,
                 stockCount = item.stockCount,
                 imgURL = item.imgURL
             }).ToList();
             return items;
         }
         catch (Exception e)
         {
             Debug.Write("Failed to read data. Error: " + e.StackTrace.ToString());
             return null;
         }
     }
 }
 public int insert(Order order)
 {
     using(var db = new CustomDBContext())
     {
         try
         {
             DBOrder dbOrder = db.Orders.Add(new DBOrder()
             {
                 ID = 0,
                 customerID = order.customer.ID,
                 lines = order.lines.Select(line => new DBOrderLine()
                 {
                     ID = 0,
                     itemID = line.item.ID,
                     quantity = line.quantity,
                     orderID = 0
                 }).ToList(),
                 orderTime = order.orderTime,
                 sentTime = order.sentTime
             });
             db.SaveChanges();
             return dbOrder.ID;
         }
         catch (Exception e)
         {
             Debug.Write("Failed to insert data. Error: " + e.StackTrace.ToString());
             return -1;
         }
     }
 }
 public bool insert(Item item)
 {
     using (var db = new CustomDBContext())
     {
         try
         {
             db.Items.Add(new DBItem()
             {
                 ID = item.ID,
                 name = item.name,
                 country = item.country,
                 district = item.district,
                 year = item.year,
                 type = item.type,
                 price = item.price,
                 description = item.description,
                 stockCount = item.stockCount,
                 imgURL = item.imgURL
             });
             db.SaveChanges();
             return true;
         }
         catch (Exception e)
         {
             Debug.Write("Failed to insert data. Error: " + e.StackTrace.ToString());
             return false;
         }
     }
 }
 public List<Order> selectAll(string customerEmail)
 {
     using (var db = new CustomDBContext())
     {
         try
         {
             List<Order> orders = db.Orders.Select(order => new Order()
             {
                 ID = order.ID,
                 customer = new Customer()
                 {
                     ID = order.customer.ID,
                     firstName = order.customer.firstName,
                     lastName = order.customer.lastName,
                     email = order.customer.email,
                     password = order.customer.password,
                     adress = order.customer.adress,
                     zipCode = order.customer.zipCode,
                     area = order.customer.area.area
                 },
                 lines = order.lines.Select(line => new OrderLine()
                 {
                     ID = line.ID,
                     item = new Item()
                     {
                         ID = line.item.ID,
                         name = line.item.name,
                         country = line.item.country,
                         district = line.item.district,
                         year = line.item.year,
                         type = line.item.type,
                         price = line.item.price,
                         description = line.item.description,
                         stockCount = line.item.stockCount,
                         imgURL = line.item.imgURL
                     },
                     quantity = line.quantity
                 }).ToList(),
                 orderTime = order.orderTime,
                 sentTime = order.sentTime
             }).Where(order => order.customer.email.Equals(customerEmail)).ToList();
             foreach(Order order in orders)
             {
                 order.calculateTotalPrice();
             }
             return orders;
         }
         catch(Exception e)
         {
             Debug.Write("Failed to read data. Error: " + e.StackTrace.ToString());
             return null;
         }
     }
 }
 public bool delete(int ID)
 {
     using (var db = new CustomDBContext())
     {
         try
         {
             DBItem item = db.Items.Find(ID);
             db.Items.Remove(item);
             db.SaveChanges();
             return true;
         }
         catch (Exception e)
         {
             Debug.Write("Failed to delete data. Error: " + e.StackTrace.ToString());
             return false;
         }
     }
 }
 public Order selectOne(int ID)
 {
     using (var db = new CustomDBContext())
     {
         try
         {
             DBOrder dbOrder = db.Orders.Find(ID);
             Order order = new Order()
             {
                 ID = dbOrder.ID,
                 customer = new Customer()
                 {
                     ID = dbOrder.customer.ID,
                     firstName = dbOrder.customer.firstName,
                     lastName = dbOrder.customer.lastName,
                     email = dbOrder.customer.email,
                     password = dbOrder.customer.password,
                     adress = dbOrder.customer.adress,
                     zipCode = dbOrder.customer.zipCode,
                     area = dbOrder.customer.area.area
                 },
                 lines = dbOrder.lines.Select(line => new OrderLine()
                 {
                     ID = line.ID,
                     item = new Item()
                     {
                         ID = line.item.ID,
                         name = line.item.name,
                         country = line.item.country,
                         district = line.item.district,
                         year = line.item.year,
                         type = line.item.type,
                         price = line.item.price,
                         description = line.item.description,
                         stockCount = line.item.stockCount,
                         imgURL = line.item.imgURL
                     },
                     quantity = line.quantity
                 }).ToList(),
                 orderTime = dbOrder.orderTime,
                 sentTime = dbOrder.sentTime
             };
             if (order == null)
                 return null;
             order.calculateTotalPrice();
             return order;
         }
         catch (Exception e)
         {
             Debug.Write("Failed to read data. Error: " + e.StackTrace.ToString());
             return null;
         }
     }
 }
 public Item selectOne(int ID)
 {
     using (var db = new CustomDBContext())
     {
         try
         {
             DBItem item = db.Items.Find(ID);
             return item == null ? null : new Item()
             {
                 ID = item.ID,
                 name = item.name,
                 country = item.country,
                 district = item.district,
                 year = item.year,
                 type = item.type,
                 price = item.price,
                 description = item.description,
                 stockCount = item.stockCount,
                 imgURL = item.imgURL
             };
         }
         catch (Exception e)
         {
             Debug.Write("Failed to read data. Error: " + e.StackTrace.ToString());
             return null;
         }
     }
 }
 public bool update(int ID, Item item)
 {
     using (var db = new CustomDBContext())
     {
         try
         {
             DBItem itemInDB = db.Items.Find(ID);
             if (itemInDB == null)
                 return false;
             itemInDB.name = item.name;
             itemInDB.country = item.country;
             itemInDB.district = item.district;
             itemInDB.year = item.year;
             itemInDB.type = item.type;
             itemInDB.price = item.price;
             itemInDB.description = item.description;
             itemInDB.stockCount = item.stockCount;
             itemInDB.imgURL = item.imgURL;
             db.SaveChanges();
             return true;
         }
         catch (Exception e)
         {
             Debug.Write("Failed to update data. Error: " + e.StackTrace.ToString());
             return false;
         }
     }
 }