Exemplo n.º 1
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;
        }
        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;
                }
            }
        }
        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;
        }
Exemplo n.º 4
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.º 5
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.º 6
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.º 9
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.º 10
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.º 11
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.º 12
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.º 13
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.º 14
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;
        }
Exemplo n.º 15
0
        public bool UpdateProduct(int ProductId, string Name, double Price, int Stock, string Description, int CategoryId)
        {
            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;

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

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

            return false;
        }
Exemplo n.º 16
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;
                }
            }
        }
Exemplo n.º 17
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;
                }
            }
        }
Exemplo n.º 18
0
        public bool AddPerson(PersonModel person, Role role, string password)
        {
            var email = person.Email;
            var newPerson = new Person()
            {
                Email = email,
                Firstname = person.Firstname,
                Lastname = person.Lastname,
                Address = person.Address,
                Zipcode = person.Zipcode,

            };
            using (var db = new TankshopDbContext())
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        var personPostal = db.Postals.Find(person.Zipcode);
                        if (personPostal == null)
                        {
                            personPostal = new Postal()
                            {
                                Zipcode = person.Zipcode,
                                City = person.City
                            };
                        }
                        personPostal.People.Add(newPerson);
                        newPerson.Postal = personPostal;

                        // Create email / password - combination
                        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);

                        // Set Customer / AdminId
                        int AdminId = 0, CustomerId = 0;
                        if (role == Role.Admin)
                        {
                            var dbAdmin = db.Admins.FirstOrDefault(a => a.Email == email);
                            if (dbAdmin == null)
                            {
                                dbAdmin = new Admin()
                                {
                                    Email = email
                                };
                                db.Admins.Add(dbAdmin);
                            }
                            AdminId = dbAdmin.AdminId;
                        }
                        if (role == Role.Customer)
                        {
                            var dbCustomer = db.Customers.FirstOrDefault(c => c.Email == email);
                            if (dbCustomer == null)
                            {
                                dbCustomer = new Nettbutikk.Model.Customer()
                                {
                                    Email = email
                                };
                                db.Customers.Add(dbCustomer);
                            }
                            CustomerId = dbCustomer.CustomerId;

                        }

                        db.People.Add(newPerson);

                        db.SaveChanges();
                        transaction.Commit();

                        return true;

                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        return false;
                    }
                }
            }
        }
Exemplo n.º 19
0
        public int PlaceOrder(OrderModel order)
        {
            using (var db = new TankshopDbContext())
            {
                try
                {
                    var newOrder = new Nettbutikk.Model.Order()
                    {
                        CustomerId = order.CustomerId,
                        Date = order.Date
                    };

                    foreach (var item in order.Orderlines)
                    {
                        var product = db.Products.Find(item.ProductId);
                        var orderline = new Orderline()
                        {
                            Product = product,
                            Count = item.Count,
                            ProductId = item.ProductId
                        };

                        newOrder.Orderlines.Add(orderline);
                    }

                    db.Orders.Add(newOrder);
                    db.SaveChanges();
                    return newOrder.OrderId;
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }
Exemplo n.º 20
0
        public bool UpdateOrderline(OrderlineModel orderline)
        {
            using (var db = new TankshopDbContext())
            {
                try
                {
                    var dbOrderline = db.Orderlines.Find(orderline.OrderlineId);

                    if (orderline.Count == 0)
                    {
                        db.Orderlines.Remove(dbOrderline);
                    }
                    else
                    {
                        dbOrderline.ProductId = orderline.ProductId;
                        dbOrderline.Count = orderline.Count;
                    }

                    db.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
Exemplo n.º 21
0
        public bool UpdateOrderline(OrderlineModel orderline, int adminId)
        {
            using (var db = new TankshopDbContext())
            {
                try
                {
                    var dbOrderline = db.Orderlines.Find(orderline.OrderlineId);
                    var oldOrderline = new OldOrderline()
                    {
                        OrderlineId = dbOrderline.OrderlineId,
                        OrderId = dbOrderline.OrderId,
                        ProductId_From = dbOrderline.ProductId,
                        ProductId_To= orderline.ProductId,
                        Count_From = dbOrderline.Count,
                        Count_To = orderline.Count,
                        AdminId = adminId,
                        Changed = DateTime.Now,
                    };

                    if (orderline.Count == 0)
                    {
                        db.Orderlines.Remove(dbOrderline);
                    }
                    else
                    {
                        dbOrderline.ProductId = orderline.ProductId;
                        dbOrderline.Count = orderline.Count;
                    }

                    db.OldOrderLines.Add(oldOrderline);

                    db.SaveChanges();
                    return true;
                }
                catch (Exception e)
                {
                    LogHandler.WriteToLog(e);
                    return false;
                }
            }
        }
Exemplo n.º 22
0
        public bool UpdateImage(int imageId, int productId, string imageUrl)
        {
            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;
            }

            img.ProductId = productId;
            img.ImageUrl = imageUrl;

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

            return false;
        }