public bool delete(int id, int adminid)
        {
            var db = new DatabaseContext();
            try
            {
                Customers cust = db.Customers.FirstOrDefault(u => u.Id == id);
                db.Customers.Remove(cust);
                db.SaveChanges(adminid);
                return true;
            }
            catch (Exception e)
            {
                writeToFile(e);
                return false;
            }

        }
        public bool update(int id, Customer updateUser, int adminid)
        {
            var db = new DatabaseContext();
            try
            {
                Customers cust = db.Customers.FirstOrDefault(u => u.Id == id);
                cust.Firstname = updateUser.firstname;
                cust.Lastname = updateUser.lastname;
                cust.Email = updateUser.email;
                cust.Phonenumber = updateUser.phonenumber;
                cust.Address = updateUser.address;
                cust.PostalareasId = Convert.ToInt16(updateUser.postalcode);

                var existPostalcode = db.Postalareas.Find(Convert.ToInt16(updateUser.postalcode));

                if (existPostalcode == null)
                {
                    var newPostalarea = new Postalareas()
                    {
                        PostalareasId = Convert.ToInt16(updateUser.postalcode),
                        Postalarea = updateUser.postalarea
                    };
                    cust.Postalareas = newPostalarea;
                }
                db.SaveChanges(adminid);
                return true;
            }
            catch (Exception e)
            {
                writeToFile(e);
                return false;
            }
        }
        public bool updateProducer(int id, Producer p, int adminid)
        {
            var db = new DatabaseContext();

            try
            {
                Producers prod = db.Producers.FirstOrDefault(pr => pr.Id  == id);
                prod.Id = id;
                prod.Name = p.name;
                db.SaveChanges(adminid);
                return true;
            }
            catch (Exception e)
            {
                writeToFile(e);
                return false;
            }
        }
        public bool Add(Category category, int adminId)
        {
            var newCategory = new Categories()
            {
                Name = category.name
            };

            try
            {
                var db = new DatabaseContext();
                db.Categories.Add(newCategory);
                db.SaveChanges(adminId);
                return true;
            }
            catch (Exception failed)
            {
                writeToFile(failed);
                return false;
            }
            
        }
 public List<SubCategory> deleteCategory(int id, int adminid)
 {
     var db = new DatabaseContext();
     Categories c = db.Categories.FirstOrDefault(ca => ca.Id == id);
     db.Categories.Remove(c);
     try
     {
         db.SaveChanges(adminid);
     }
     catch (DbUpdateException ue)
     {
         writeToFile(ue);
         try
         {
             return db.SubCategories.Where(sc => sc.CategoriesId == id).Select(p => new SubCategory()
             {
                 name = p.Name
             }).ToList();
         }
         catch(Exception e)
         {
             writeToFile(e);
         }
     }
     catch( Exception e)
     {
         writeToFile(e);
     }
     return null; 
 }
 public List<Product> deleteProducer(int id, int adminid)
 {
     var db = new DatabaseContext();
     Producers c = db.Producers.FirstOrDefault(ca => ca.Id == id);
     db.Producers.Remove(c);
     try
     {
         db.SaveChanges(adminid);
     }
     catch (DbUpdateException ue)
     {
         writeToFile(ue);
         try
         {
             return db.Products.Where(sc => sc.ProducersId == id).Select(p => new Product()
             {
                 name = p.Name
             }).ToList();
         }
         catch (Exception e)
         {
             writeToFile(e);
         }
     }
     catch (Exception e)
     {
         writeToFile(e);
     }
     return null;
 
 
 }
        public bool update(int id, SubCategory sc)
        {
            var db = new DatabaseContext();
            SubCategories existing = db.SubCategories.FirstOrDefault(u => u.Id == sc.ID);
            try
            {
                existing.Name = sc.name;
                existing.CategoriesId = sc.catId;

                db.SaveChanges(id);
            }
            catch (Exception e)
            {
                writeToFile(e);
                return false;
            }
            return true;
        }
        public bool updateCategory(int id, Category c, int adminid)
        {
            var db = new DatabaseContext();

            try
            {
                Categories cat = db.Categories.FirstOrDefault(ca => ca.Id == id);
                cat.Name = c.name;

                db.SaveChanges(adminid);
                return true;
            }
            catch(Exception e)
            {
                writeToFile(e);
                return false;
            }

        }
 public bool AddSub(int adminId, SubCategory sc)
 {
     var db = new DatabaseContext();
     db.SubCategories.Add(new SubCategories()
     {
         Name = sc.name,
         CategoriesId = sc.catId
     });
     try
     {
         db.SaveChanges(adminId);
     }
     catch(Exception e)
     {
         writeToFile(e);
         return false; 
     }
     return true;
 }
        public bool AddProducer(Producer producer, int id)
        {
            var newProducer = new Producers()
            {
                Name = producer.name
            };

            try
            {
                var db = new DatabaseContext();
                db.Producers.Add(newProducer);
                db.SaveChanges(id);
                return true;
            }
            catch (Exception fail)
            {
                writeToFile(fail);
                return false;
            }
        }
 public bool deleteProduct(int id, int adminid)
 {
     var db = new DatabaseContext();
     Products c = db.Products.FirstOrDefault(ca => ca.Id == id);
     db.Products.Remove(c);
     try
     {
         db.SaveChanges(adminid);
     }
     catch (DbUpdateException ue)
     {
         writeToFile(ue);
         return false;
     }
     catch (Exception e)
     {
         writeToFile(e);
         return false;
     }
     return true;
 }
        public Product addProduct(int id, Product p)
        {
            try
            {
                var db = new DatabaseContext();
                var newp = new Products()
                    {
                        Name = p.name,
                        Description = p.description,
                        LongDescription = p.longDescription,
                        CountriesId = p.countryid,
                        SubCategoriesId = p.subCategoryid,
                        Price = p.price,
                        Volum = p.volum,
                        ProducersId = p.producerid
                    };
                db.Products.Add(newp);
                db.SaveChanges(id);
                p.itemnumber = newp.Id;

                return p;
            }
            catch(Exception e)
            {
                writeToFile(e);
                return null;
            }
        }
        public bool updateProduct(int id,Product update)
        {
           
            var db = new DatabaseContext();
            
            try
            {
                Products existing = db.Products.FirstOrDefault(u => u.Id == update.itemnumber);
                existing.Name = update.name;
                existing.Price = update.price;
                existing.Volum = update.volum;
                existing.SubCategoriesId = update.subCategoryid;
                existing.ProducersId = update.producerid; 
                existing.CountriesId = update.countryid;
                existing.Description = update.description;
                existing.LongDescription = update.longDescription;
                existing.ProducersId = update.producerid;
                db.SaveChanges(id);
                return true;
            }
            catch(Exception e)
            {
                writeToFile(e);
                return false;
            }
 

        }