Exemplo n.º 1
0
        public List<OrderModel> GetAllOrders()
        {
            using(var db = new TankshopDbContext())
            {
                var dbOrders = db.Orders.ToList();
                var orderModels = new List<OrderModel>();

                foreach(var dbOrder in dbOrders)
                {
                    try
                    {
                        var order = GetOrder(dbOrder.OrderId);
                        var count = order.Orderlines.Count;
                        if (count > 0)
                            orderModels.Add(order);
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }

                return orderModels;
            }
        }
        public bool DeleteCustomer(string email)
        {
            using (var db = new TankshopDbContext())
            {
                try
                {
                    var dbPerson = db.People.Find(email);
                    var dbCustomer = db.Customers.FirstOrDefault(c => c.Email == email);
                    var dbAdmin = db.Admins.FirstOrDefault(a => a.Email == email);
                    var dbCredentials = db.Credentials.Find(email);

                    if (dbPerson != null)
                        db.People.Remove(dbPerson);
                    if (dbCustomer != null)
                        db.Customers.Remove(dbCustomer);
                    if (dbAdmin != null)
                        db.Admins.Remove(dbAdmin);
                    if (dbCredentials != null) db.Credentials.Remove(dbCredentials);

                    db.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
Exemplo n.º 3
0
        public bool AddOldProduct(string Name, double Price, int Stock, string Description, int CategoryId, int AdminId)
        {
            var db = new TankshopDbContext();
            OldProduct oldProduct = new OldProduct();

            oldProduct.Name = Name;
            oldProduct.Price = Price;
            oldProduct.Stock = Stock;
            oldProduct.Description = Description;
            oldProduct.CategoryId = CategoryId;

            oldProduct.AdminId = AdminId;
            oldProduct.Changed = DateTime.Now;

            db.OldProducts.Add(oldProduct);

            try
            {
                db.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                LogHandler.WriteToLog(e);
            }

            return false;
        }
Exemplo n.º 4
0
        public OrderModel GetOrder(int orderId)
        {
            using (var db = new TankshopDbContext())
            {
                var dbOrder = db.Orders.Find(orderId);
                if (dbOrder == null)
                    return null;

                var order = new OrderModel()
                {
                    CustomerId = dbOrder.CustomerId,
                    OrderId = dbOrder.OrderId,
                    Orderlines = db.Orderlines.Where(l => l.OrderId == dbOrder.OrderId).Select(l => new OrderlineModel()
                    {
                        OrderlineId = l.OrderlineId,
                        OrderId = l.OrderId,
                        ProductId = l.ProductId,
                        Count = l.Count,
                        ProductName = l.Product.Name,
                        ProductPrice = l.Product.Price

                    }).ToList(),
                    Date = dbOrder.Date
                };

                return order;
            }
        }
Exemplo n.º 5
0
 public int FirstCategoryWithProducts()
 {
     using (var db = new TankshopDbContext())
     {
         var FirstCategoryWithProducts = db.Categories.Where(c => c.Products.Count > 0).FirstOrDefault();
         return FirstCategoryWithProducts == null ? 0 : FirstCategoryWithProducts.CategoryId;
     }
 }
        private bool disposedValue = false; // To detect redundant calls

        #endregion Fields

        #region Constructors

        public ServiceManager()
        {
            context = new TankshopDbContext();
            Categories = new CategoryService(new CategoryRepository(context));
            Products = new ProductService(new ProductRepository(context));
            Images = new ImageBLL();
            Accounts = new AccountBLL();
        }
        public bool AddCategory(string CategoryId)
        {
            try
            {
                var db = new TankshopDbContext();
                db.Categories.Add(new Category() {CategoryId = Convert.ToInt32(CategoryId) });
                db.SaveChanges();
                return true;
            }
            catch (Exception e) { }//LogHandler.WriteToLog(e); }

            return false;
        }
        public List<Customer> GetAllCustomers()
        {
            var customerList = new List<Customer>();

            try
            {
                using (var db = new TankshopDbContext())
                {
                    var dbCustomers = db.Customers.ToList();

                    foreach (var c in dbCustomers)
                    {
                        var p = db.People.Find(c.Email);
                        var customer = new Customer()
                        {
                            Email = p.Email,
                            Firstname = p.Firstname,
                            Lastname = p.Lastname,
                            Address = p.Address,
                            Postal = new Postal
                            {
                            Zipcode = p.Zipcode,
                                City = p.Postal.City
                            },
                            CustomerId = c.CustomerId,
                            Orders = c.Orders.Select(o => new Order()
                            {
                                CustomerId = o.CustomerId,
                                Date = o.Date,
                                OrderId = o.OrderId,
                                Orderlines = o.Orderlines.Select(l => new Orderline()
                                {
                                    Count = l.Count,
                                    OrderId = l.OrderId,
                                    OrderlineId = l.OrderlineId,
                                    ProductId = l.ProductId,
                                    ProductName = l.Product.Name,
                                    ProductPrice = l.Product.Price
                                }).ToList()
                            }).ToList()
                        };
                        customerList.Add(customer);
                    }
                    return customerList;
                }
            }
            catch (Exception)
            {
                return customerList;
            }
        }
Exemplo n.º 9
0
        public bool AddImage(int productId, string imageUrl)
        {
            try
            {
                var db = new TankshopDbContext();
                db.Images.Add(new Nettbutikk.Model.Image() { ProductId = productId, ImageUrl = imageUrl });
                db.SaveChanges();
                return true;
            }
            catch (Exception e) {
                LogHandler.WriteToLog(e);
            }

            return false;
        }
Exemplo n.º 10
0
        public bool AddCategory(string name)
        {
            try
            {
                var db = new TankshopDbContext();
                db.Categories.Add(new Nettbutikk.Model.Category() { Name = name });
                db.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                LogHandler.WriteToLog(e);
            }

            return false;
        }
Exemplo n.º 11
0
        public bool DeleteOrder(int orderId)
        {
            using(var db = new TankshopDbContext())
            {
                try
                {
                    var dbOrder = db.Orders.Find(orderId);
                    db.Orders.Remove(dbOrder);
                    db.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }

            }
        }
        public bool DeleteCategory(int CategoryId)
        {
            var db = new TankshopDbContext();

            Category category = (from i in db.Categories where i.CategoryId == CategoryId select i).FirstOrDefault();

            if (category == null)
            {
                return false;
            }

            try {
                db.Categories.Remove(category);
                db.SaveChanges();
                return true;
            }
            catch (Exception e) { }

            return false;
        }
        public bool UpdateCategory(int CategoryId, string CategoryName)
        {
            var db = new TankshopDbContext();

            Category category = (from i in db.Categories where i.CategoryId == CategoryId select i).FirstOrDefault();

            if (category == null)
            {
                return false;
            }

            category.Name = CategoryName;

            try {
                db.SaveChanges();
                return true;
            }
            catch (Exception e) { }//LogHandler.WriteToLog(e); }

            return false;
        }
Exemplo n.º 14
0
        public bool DeleteProduct(int ProductId)
        {
            var db = new TankshopDbContext();

            Nettbutikk.Model.Product product = (from p in db.Products where p.ProductId == ProductId select p).FirstOrDefault();

            if (product == null)
                return false;

            try
            {
                db.Products.Remove(product);
                db.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                LogHandler.WriteToLog(e);
            }

            return false;
        }
Exemplo n.º 15
0
        public bool DeleteImage(int imageId)
        {
            var db = new TankshopDbContext();

            Nettbutikk.Model.Image img = (from i in db.Images where i.ImageId == imageId select i).FirstOrDefault();

            if (img == null)
            {
                return false;
            }

            try {
                db.Images.Remove(img);
                db.SaveChanges();
                return true;
            }
            catch (Exception e) {
                LogHandler.WriteToLog(e);
            }

            return false;
        }
Exemplo n.º 16
0
        //OldImage
        public bool AddOldImage(int productId, string imageUrl, int adminId)
        {
            var db = new TankshopDbContext();
            OldImage oldImage = new OldImage();

            oldImage.ProductId = productId;
            oldImage.ImageUrl = imageUrl;
            oldImage.AdminId = adminId;
            oldImage.Changed = DateTime.Now;

            db.OldImages.Add(oldImage);

            try {
                db.SaveChanges();
                return true;
            }
            catch (Exception e) {
                LogHandler.WriteToLog(e);
            }

            return false;
        }
Exemplo n.º 17
0
        public bool DeleteCategory(int CategoryId)
        {
            var db = new TankshopDbContext();

            Nettbutikk.Model.Category category = (from c in db.Categories where c.CategoryId == CategoryId select c).FirstOrDefault();

            if (category == null)
                return false;

            try
            {
                db.Categories.Remove(category);
                db.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                LogHandler.WriteToLog(e);
            }

            return false;
        }
Exemplo n.º 18
0
        public bool AddOldCategory(string Name, int adminId)
        {
            var db = new TankshopDbContext();
            OldCategory oldCategory = new OldCategory();

            oldCategory.Name = Name;
            oldCategory.AdminId = adminId;
            oldCategory.Changed = DateTime.Now;

            db.OldCategories.Add(oldCategory);

            try
            {
                db.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                LogHandler.WriteToLog(e);
            }

            return false;
        }
Exemplo n.º 19
0
        public bool AddProduct(string Name, double Price, int Stock, string Description, int CategoryId)
        {
            var db = new TankshopDbContext();

            var newProduct = new Nettbutikk.Model.Product()
            {
                Name = Name,
                Price = Price,
                Stock = Stock,
                Description = Description,
                CategoryId = CategoryId
            };

            try {
                db.Products.Add(newProduct);
                db.SaveChanges();
                return true;
            }
            catch (Exception e) {
                LogHandler.WriteToLog(e);
            }

            return false;
        }
 public Customer GetCustomer(string email)
 {
     using (var db = new TankshopDbContext())
     {
         try
         {
             return db.Customers.Include("Postal,Orders").FirstOrDefault(c => c.Email == email);
         }
         catch (Exception)
         {
             return null;
         }
     }
 }
 private Person GetPerson(string email)
 {
     using (var db = new TankshopDbContext())
     {
         try
         {
             return db.People.Where(p => p.Email == email).FirstOrDefault(); ;
         }
         catch (Exception)
         {
             return null;
         }
     }
 }
Exemplo n.º 22
0
        public bool UpdatePerson(PersonModel personUpdate, string email)
        {
            // TODO: update admin/customer -id
            using (var db = new TankshopDbContext())
            {
                try
                {
                    var editPerson = db.People.Find(email);
                    var editPersonModel = GetPerson(email);

                    editPerson.Firstname = personUpdate.Firstname;
                    editPerson.Lastname = personUpdate.Lastname;
                    editPerson.Address = personUpdate.Address;

                    var personPostal = db.Postals.Find(personUpdate.Zipcode);
                    if (personPostal == null)
                    {
                        var oldPostal = db.Postals.Find(editPerson.Zipcode);
                        if (oldPostal != null)
                            oldPostal.People.Remove(editPerson);
                        db.SaveChanges();

                        personPostal = new Postal()
                        {
                            Zipcode = personUpdate.Zipcode,
                            City = personUpdate.City
                        };
                        personPostal.People.Add(editPerson);
                        db.SaveChanges();
                    }

                    editPerson.Zipcode = personUpdate.Zipcode;
                    editPerson.Postal = personPostal;

                    db.SaveChanges();

                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
 public Customer GetCustomer(int customerId)
 {
     using (var db = new TankshopDbContext())
     {
         try
         {
             return db.Customers.Include("Postal,Orders").FirstOrDefault(c => c.CustomerId == customerId);
         }
         catch (Exception)
         {
             return null;
         }
     }
 }
Exemplo n.º 24
0
        public bool isAdmin(string email)
        {
            using (var db = new TankshopDbContext())
            {
                var dbAdmin = db.Admins.FirstOrDefault(a => a.Email == email);

                return dbAdmin != null;
            }
        }
Exemplo n.º 25
0
 public bool SetRole(string email, Role role, bool isRole)
 {
     using (var db = new TankshopDbContext())
     {
         try
         {
             if (role == Role.Admin)
             {
                 var dbAdmin = db.Admins.Find(email);
                 if (isRole)
                 {
                     if (dbAdmin == null)
                     {
                         var newAdmin = new Admin()
                         {
                             Email = email
                         };
                         db.Admins.Add(newAdmin);
                     }
                 }
                 else
                 {
                     db.Admins.Remove(dbAdmin);
                 }
             }
             if (role == Role.Customer)
             {
                 var dbCustomer = db.Customers.Find(email);
                 if (isRole)
                 {
                     if (dbCustomer == null)
                     {
                         var newCustomer = new Nettbutikk.Model.Customer()
                         {
                             Email = email
                         };
                         db.Customers.Add(newCustomer);
                     }
                 }
                 else
                 {
                     db.Customers.Remove(dbCustomer);
                 }
             }
             //db.SaveChanges();
             return true;
         }
         catch (Exception)
         {
             return false;
         }
     }
 }
Exemplo n.º 26
0
        //public CustomerModel GetCustomer(int customerId)
        ////{
        ////    using (var db = new TankshopDbContext())
        ////    {
        ////        try
        ////        {
        ////            var dbCustomer = db.Customers.FirstOrDefault(c => c.CustomerId == customerId);
        ////            var dbPerson = GetPerson(dbCustomer.Email);
        ////            var orderRepo = new OrderRepo();
        ////            var customer = new CustomerModel()
        ////            {
        ////                CustomerId = customerId,
        ////                Email = dbPerson.Email,
        ////                Firstname = dbPerson.Firstname,
        ////                Lastname = dbPerson.Lastname,
        ////                Address = dbPerson.Address,
        ////                Zipcode = dbPerson.Zipcode,
        ////                City = dbPerson.City,
        ////                Orders = orderRepo.GetOrders(customerId)
        ////            };
        ////            return customer;
        ////        }
        ////        catch (Exception)
        ////        {
        ////            return null;
        ////        }
        ////    }
        ////}
        //public CustomerModel GetCustomer(string email)
        //{
        //    using (var db = new TankshopDbContext())
        //    {
        //        try
        //        {
        //            var dbPerson = GetPerson(email);
        //            var customerId = db.Customers.FirstOrDefault(c => c.Email == email).CustomerId;
        //            var orderRepo = new OrderRepo();
        //            var customer = new CustomerModel()
        //            {
        //                CustomerId = customerId,
        //                Email = dbPerson.Email,
        //                Firstname = dbPerson.Firstname,
        //                Lastname = dbPerson.Lastname,
        //                Address = dbPerson.Address,
        //                Zipcode = dbPerson.Zipcode,
        //                City = dbPerson.City,
        //                Orders = orderRepo.GetOrders(customerId)
        //            };
        //            return customer;
        //        }
        //        catch (Exception)
        //        {
        //            return null;
        //        }
        //    }
        //}
        public PersonModel GetPerson(string email)
        {
            using (var db = new TankshopDbContext())
            {
                try
                {

                    var person = db.People.Where(p => p.Email == email).Select(p => new PersonModel()
                    {
                        Email = p.Email,
                        Firstname = p.Firstname,
                        Lastname = p.Lastname,
                        Address = p.Address,
                        Zipcode = p.Zipcode,
                        City = p.Postal.City
                    }).Single();

                    return person;
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
Exemplo n.º 27
0
        public List<PersonModel> GetAllPeople()
        {
            using (var db = new TankshopDbContext())
            {
                var people = db.People.Select(p => new PersonModel()
                {
                    Email = p.Email,
                    Firstname = p.Firstname,
                    Lastname = p.Lastname,
                    Address = p.Address,
                    Zipcode = p.Zipcode,
                    City = p.Postal.City
                }).ToList();

                return people;
            }
        }
Exemplo n.º 28
0
        public AdminModel GetAdmin(string email)
        {
            using (var db = new TankshopDbContext())
            {
                var adminId = db.Admins.FirstOrDefault(a => a.Email == email).AdminId;
                var dbPerson = GetPerson(email);
                var admin = new AdminModel()
                {
                    AdminId = adminId,
                    Email = dbPerson.Email,
                    Firstname = dbPerson.Firstname,
                    Lastname = dbPerson.Lastname,
                    Address = dbPerson.Address,
                    Zipcode = dbPerson.Zipcode,
                    City = dbPerson.City
                };

                return admin;
            }
        }
Exemplo n.º 29
0
        public bool CreateCredentials(string email, string password)
        {
            using (var db = new TankshopDbContext())
            {
                try
                {
                    var existingCredentials = db.Credentials.Find(email);
                    if (existingCredentials != null)
                        return false;

                    var passwordHash = CreateHash(password);
                    var newCredentials = new Credential()
                    {
                        Email = email,
                        Password = passwordHash
                    };
                    db.Credentials.Add(newCredentials);

                    //db.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }

            }
        }
Exemplo n.º 30
0
        public bool ChangePassword(string email, string newPassword)
        {
            using (var db = new TankshopDbContext())
            {
                try
                {
                    var newPasswordHash = CreateHash(newPassword);
                    var existingUser = db.Credentials.Find(email);
                    if (existingUser == null)
                        return false;

                    existingUser.Password = newPasswordHash;
                    db.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }