public List<Product> getAll()
        {
            try
            {
                var db = new DatabaseContext();
                List<Product> products = db.Products.Select(item => new Product()
                {
                    itemnumber = item.Id,
                    name = item.Name,
                    description = item.Description,
                    price = item.Price,
                    volum = item.Volum,
                    producer = item.Producers.Name,
                    country = item.Countries.Name,

                }).ToList();


                return products;
            }
            catch(Exception e)
            {
                writeToFile(e);
                return null;
            }
        }
        public List<Category> getAll(int? id)
        {
            try
            {
                var db = new DatabaseContext();
                List<Category> category;
                if (id == null)
                {
                    category = db.Categories.Select(item => new Category()
                    {
                        ID = item.Id,
                        name = item.Name
                    }).ToList();
                }
                else
                {
                    category = db.Categories.Select(item => new Category()
                {
                    ID = item.Id,
                    name = item.Name
                }).Where(item => item.ID == id).ToList();
                }
                return category;

            }
            catch(Exception e)
            {
                writeToFile(e);
                return null; 
            }
        }
 public List<SubCategory> getAllSub(int? id)
 {
     try
     {
         var db = new DatabaseContext();
         List<SubCategories> subcat;
         if (id != null)
         {
             subcat = db.SubCategories.Include("Categories").Where(p => p.Id == id).ToList();
         }
         else
             subcat = db.SubCategories.Include("Categories").ToList();
         var list = new List<SubCategory>();
         foreach (var item in subcat)
         {
             list.Add(new SubCategory()
             {
                 ID = item.Id,
                 name = item.Name,
                 catName = item.Categories.Name
             });
         }
         return list;
     }
     catch(Exception e)
     {
         writeToFile(e);
         return null; 
     }
 }
        public List<Customer> getAll()
        {
            try
            {
                var db = new DatabaseContext();
                List<Customer> customers = db.Customers.Select(item => new Customer()
                {
                    id = item.Id,
                    firstname = item.Firstname,
                    lastname = item.Lastname,
                    address = item.Address,
                    email = item.Email,
                    postalcode = item.PostalareasId.ToString(),
                    postalarea = item.Postalareas.Postalarea,
                    phonenumber = item.Phonenumber,
                    admin = item.Admin
                }).ToList();

                foreach (var i in customers)
                {
                    i.postalcode = normalizePostalcode(int.Parse(i.postalcode));
                }

                return customers;
            }
            catch(Exception e)
            {
                writeToFile(e);
                return null; 
            }
        }
        public List<Customer> getResult(string searchString)
        {
            try
            {
                var db = new DatabaseContext();

                List<Customer> users = db.Customers.Select(p => new Customer()
                {
                    id = p.Id,
                    firstname = p.Firstname,
                    lastname = p.Lastname,
                    address = p.Address,
                    email = p.Email,
                    postalcode = p.PostalareasId.ToString(),
                    postalarea = p.Postalareas.Postalarea,
                    phonenumber = p.Phonenumber

                }).Where(p => p.firstname.ToUpper().Contains(searchString.ToUpper())
                                || p.lastname.ToUpper().Contains(searchString.ToUpper())).ToList();
                foreach (var u in users)
                {
                    u.postalcode = normalizePostalcode(int.Parse(u.postalcode));
                }
                return users;
            }
            catch(Exception e)
            {
                writeToFile(e);
                return null;
            }
        }
        public List<Order> getAllOrders(int? id)
        {

            try
            {
              
                var db = new DatabaseContext();
                List<Order> lines;
                if (id != null)
                    lines = db.Orders.Select(item => new Order()
                    {
                        id = item.Id,
                        orderdate = item.OrderDate,
                        customerid = item.CustomersId
                    }).Where(item => item.id == id).ToList();
                else
                    lines = db.Orders.Select(item => new Order()
                    {
                        id = item.Id,
                        orderdate = item.OrderDate,
                        customerid = item.CustomersId
                    }).ToList();
               
                return lines;
            }
            catch(Exception e)
            {
                writeToFile(e);
                return null; 
            }
        }
        public Customer getCustomer(int id)
        {
            try
            {
                var db = new DatabaseContext();
                Customers cust = (Customers)db.Customers.FirstOrDefault(c => c.Id == id);
                Customer customer = new Customer();

                customer.id = cust.Id;
                customer.firstname = cust.Firstname;
                customer.lastname = cust.Lastname;
                customer.phonenumber = cust.Phonenumber;
                customer.email = cust.Email;
                customer.address = cust.Address;
                customer.hashpassword = cust.Password;
                customer.admin = cust.Admin;
                customer.postalarea = getPostalarea(cust.PostalareasId);
                customer.postalcode = normalizePostalcode(cust.PostalareasId);

                return customer;
            }
            catch(Exception e)
            {
                writeToFile(e);
                return null;
            }
        }
        public List<Category> getAllCategories()
        {
            try
            {
                var db = new DatabaseContext();
                List<Category> categories = db.Categories.Select(item => new Category()
                {
                    ID = item.Id,
                    name = item.Name
                }).ToList();

                return categories;
            }
            catch (Exception e)
            {
                writeToFile(e);
                return null;
            }
        }
        public List<Producer> getAllProducers(int? id)
        {
            try
            {
                var db = new DatabaseContext();
                List<Producer> producers = db.Producers.Select(item => new Producer()
                {
                    id = item.Id,
                    name = item.Name
                }).ToList();

                return producers;
            }
            catch(Exception e)
            {
                writeToFile(e);
                return null; 
            }
        }
        public List<SubCategory> getAllSubCategories()
        {
            try
            {
                var db = new DatabaseContext();
                List<SubCategory> subCategories = db.SubCategories.Select(item => new SubCategory()
                {
                    ID = item.Id,
                    name = item.Name,
                    catName = item.Categories.Name,
                    catId = item.CategoriesId
                }).ToList();

                return subCategories;
            }
            catch(Exception e)
            {
                writeToFile(e);
                return null;
            }
        }
Esempio n. 11
0
        public List<OrderLine> getAllOrderLinesOfOrder(int id)
        {
            try
            {
                var db = new DatabaseContext();
                List<OrderLine> lines = db.OrderLines.Select(item => new OrderLine()
                {
                    id = item.Id,
                    orderid = item.OrdersId,
                    productid = item.ProductsId,
                    quantity = item.Quantity,
                }).Where(item => item.orderid == id).ToList();

                return lines;
            }
            catch(Exception e)
            {
                writeToFile(e);
                return null;
            }
        }
 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 bool validate(String email, byte[] hashedPassword)
        {
            try
            {
                var db = new DatabaseContext();
                Customers userFound = db.Customers.FirstOrDefault(u => u.Email == email);

                if (userFound != null)
                {
                    if (email.Equals(userFound.Email) && Enumerable.SequenceEqual(hashedPassword, userFound.Password))
                    {
                        if (userFound.Admin == true)
                        {
                            return true;
                        }
                    }
                }
            }
            catch(Exception e)
            {
                writeToFile(e);
            }
            return false; 
        }
 public String getPostalarea(int id)
 {
     try
     {
         var db = new DatabaseContext();
         var postalarea = (Postalareas)db.Postalareas.FirstOrDefault(p => p.PostalareasId == id);
         return postalarea.Postalarea;
     }
     catch(Exception e)
     {
         writeToFile(e);
         return null;
     }
 }
 public Customer findUser(String email)
 {
     try
     {
         var db = new DatabaseContext();
         Customers userFound = db.Customers.FirstOrDefault(u => u.Email == email);
         Customer c = new Customer();
         c.id = userFound.Id;
         c.firstname = userFound.Firstname;
         c.lastname = userFound.Lastname;
         c.email = userFound.Email;
         c.phonenumber = userFound.Phonenumber;
         c.postalcode = normalizePostalcode(userFound.PostalareasId);
         c.postalarea = db.Postalareas.Find(userFound.PostalareasId).Postalarea;
         c.address = userFound.Address;
         c.hashpassword = userFound.Password;
         c.admin = true;
         return c;
     }
     catch(Exception e)
     {
         writeToFile(e);
         return null;
     }
 }
        public SubCategory SubCatDetails(int id)
        {
            try
            {
                var db = new DatabaseContext();
                SubCategories subcat = (SubCategories)db.SubCategories.FirstOrDefault(c => c.Id == id);
                SubCategory subcategory = new SubCategory()
                {
                    ID = subcat.Id,
                    name = subcat.Name,
                    catId = subcat.CategoriesId
                };

                return subcategory;
            }
            catch(Exception e)
            {
                writeToFile(e);
                return null; 
            }
        }
        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 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 List<Producer> getProducers()
        {
            try
            {
                var db = new DatabaseContext();
                List<Producer> list = db.Producers.Select(p => new Producer()
                {
                    id = p.Id,
                    name = p.Name
                }).ToList();

                return list;
            }
            catch(Exception e)
            {
                writeToFile(e);
                return null; 
            }
        }
 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 List<Country> getCountries()
        {
            try
            {
                var db = new DatabaseContext();
                List<Country> list = db.Countries.Select(item => new Country()
                    {
                        id = item.Id,
                        name = item.Name
                    }).ToList();

                return list;
            }
            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 Producer producerDetails(int id)
        {
            try
            {
                var db = new DatabaseContext();
                Producers produser = (Producers)db.Producers.FirstOrDefault(c => c.Id == id);
                Producer prod = new Producer()
                {
                    id = produser.Id,
                    name = produser.Name
                };

                return prod;
            }
            catch (Exception e)
            {
                writeToFile(e);
                return null;
            }
        }
        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 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 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 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 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 List<Product> getResult(string searchString)
 {
     try
     {
         var db = new DatabaseContext();
         List<Product> products = db.Products.Select(p => new Product()
         {
             itemnumber = p.Id,
             name = p.Name,
             description = p.Description,
             price = p.Price,
             volum = p.Volum,
             producer = p.Producers.Name,
             category = p.SubCategories.Categories.Name,
             categoryid = p.SubCategories.Categories.Id,
             subCategory = p.SubCategories.Name,
             subCategoryid = p.SubCategories.Id,
             country = p.Countries.Name
         }).Where(p => p.name.ToUpper().Contains(searchString.ToUpper())
                         || p.description.ToUpper().Contains(searchString.ToUpper())).ToList();
         return products;
     }
     catch(Exception e)
     {
         writeToFile(e);
         return null;
     }
 }