예제 #1
0
        public bool update(int id, Customer updateUser)
        {
            var db = new DatabaseContext();
            try
            {
                Customers cust = db.Customers.FirstOrDefault(u => u.Id == id);
           
                cust.Firstname = updateUser.firstname;
                cust.Lastname = updateUser.lastname;
                cust.Address = updateUser.address;
                cust.PostalareasId = Convert.ToInt16(updateUser.postalcode);
                cust.Phonenumber = updateUser.phonenumber;
                cust.Email = updateUser.email;
            
                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();
                return true;
            }
            catch (Exception fail)
            {
                return false;
               
            }
        }
예제 #2
0
        public List<Product> getMostSold()
        {
            var db = new DatabaseContext();
            List<int> topOrders = db.OrderLines
                                    .GroupBy(p => p.ProductsId)
                                    .OrderByDescending(gp => gp.Count())
                                    .Take(4)
                                    .Select(g => g.Key).ToList();
            List<Products> products = new List<Products>(); 
            
            foreach (var i in topOrders)
            {
                products.Add(db.Products.Where(p => p.Id == i).First());
            }
            List<Product> prod = new List<Product>();
            foreach (var p in products)
            { 
                 var product = new Product()
                {
                    itemnumber = p.Id,
                    name = p.Name,
                    description = p.Description,
                    price = p.Price,
                    volum = p.Volum,
                    producer = p.Producers.Name,
                    subCategory = p.SubCategories.Name,
                    subCategoryid = p.SubCategories.Id,
                    country = p.Countries.Name
                };
                prod.Add(product);
            }

            
            return prod;
        }
예제 #3
0
        public int checkout(ShoppingCart cart)
        {
            var db = new DatabaseContext();

            var newOrder = new Orders()
            {
                OrderDate = DateTime.Now, 
                CustomersId = cart.userID
            };
            db.Orders.Add(newOrder);
            db.SaveChanges();
            

            foreach (var item in cart.shoppingCartItems)
            {
                var newItem = new OrderLines()
                {
                    ProductsId = item.product.itemnumber,
                    OrdersId = newOrder.Id,
                    Quantity = item.quantity
                };
                db.OrderLines.Add(newItem);
            }
            db.SaveChanges();

            return newOrder.Id;
        }
예제 #4
0
 public List<OrderLine> getOrder(int id)
 {
     Debug.WriteLine("OrdreID: " + id);
     var db = new DatabaseContext();
     var lines = db.OrderLines.Include(p => p.Products).Where(o => o.OrdersId == id).ToList();
     Debug.WriteLine("Antall ordrelinjer fra db");
     Debug.WriteLine(lines.Count);
     List<OrderLine> list = new List<OrderLine>();
     foreach (var item in lines)
     {
         var itemProduct = new Product()
         {
             itemnumber = item.Products.Id,
             name = item.Products.Name,
             price = item.Products.Price,
             description = item.Products.Description
         };
         list.Add(new OrderLine()
         {
             product = itemProduct,
             quantity = item.Quantity
         });
     }
     return list;
 }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            Database.SetInitializer<DatabaseContext>(new DbInitializer());

            using (var db = new DatabaseContext())
            {
                {
                    db.Database.Initialize(true);
                }
            }
        }
예제 #6
0
 public Customer findCustomer(String email)
 {
     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.address = userFound.Address;
     c.postalcode = userFound.PostalareasId.ToString();
     c.postalarea = db.Postalareas.Find(userFound.PostalareasId).Postalarea;
     c.hashpassword = userFound.Password;
      return c;
 }
예제 #7
0
 public List<Order> getOrders(int id)
 {
     var db = new DatabaseContext();
     var order = db.Orders.Where(o => o.CustomersId == id).ToList();
     Debug.WriteLine("Antall i denne lista daaa?");
     Debug.WriteLine(order.Count);
     List<Order> list = new List<Order>();
     foreach (var item in order){
         list.Add(new Order()
         {
             id = item.Id,
             orderdate = item.OrderDate,
         });
     }
     return list; 
 }
예제 #8
0
        public bool validate(String email, byte[] hashedPassword)
        {
            var db = new DatabaseContext();

            try
            {
                Customers validated = db.Customers.FirstOrDefault(u => u.Password == hashedPassword && u.Email == email);
                if (validated == null)
                    return false;
                else
                    return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
예제 #9
0
        public List<Product> getAll(int? id, String sc, int? sort)
        {
           
            var db = new DatabaseContext();
            List<Product> allProducts = new List<Product>();
            var products = db.Products.Include(p => p.SubCategories.Categories).Where(p => p.SubCategoriesId == id).OrderBy(p => p.Name).ToList();
            switch (sort)
            {
                case 1:
                    products = db.Products.Include(p => p.SubCategories.Categories).Where(p => p.SubCategoriesId == id).OrderBy(p => p.Name).ToList();
                    break;
                case 2:
                    products = db.Products.Include(p => p.SubCategories.Categories).Where(p => p.SubCategoriesId == id).OrderBy(p => p.Price).ToList();
                    break;
                case 3:
                    products = db.Products.Include(p => p.SubCategories.Categories).Where(p => p.SubCategoriesId == id).OrderByDescending(p => p.Price).ToList();
                    break;
            }
           
            foreach (var p in products)
            {

                var product = new Product()
                {
                    itemnumber = p.Id,
                    name = p.Name,
                    description = p.Description,
                    price = p.Price,
                    volum = p.Volum,
                    producer = p.Producers.Name,
                    pricePerLitre = pricePerLitre(p.Price, p.Volum),
                    category = p.SubCategories.Categories.Name,
                    categoryid = p.SubCategories.Categories.Id,
                    subCategory = p.SubCategories.Name,
                    subCategoryid = p.SubCategories.Id,
                    country = p.Countries.Name

                };
                allProducts.Add(product);
            }

            return allProducts;
        }
예제 #10
0
        public bool add(Customer inCustomer, byte[] hashedPassword)
        {
            var newCustomer = new Customers()
            {
                Firstname = inCustomer.firstname,
                Lastname = inCustomer.lastname,
                Address = inCustomer.address,
                PostalareasId = Convert.ToInt16(inCustomer.postalcode),
                Password = hashedPassword,
                Phonenumber = inCustomer.phonenumber,
                Email = inCustomer.email
            };

            var db = new DatabaseContext();
            try
            {
                var existPostalcode = db.Postalareas.Find(Convert.ToInt16(inCustomer.postalcode));

                if(existPostalcode == null )
                {
                    var newPostalarea = new Postalareas()
                    {
                        PostalareasId = Convert.ToInt16(inCustomer.postalcode),
                        Postalarea = inCustomer.postalarea
                    };
                    newCustomer.Postalareas = newPostalarea;
                }
                db.Customers.Add(newCustomer);
                db.SaveChanges();
                return true;
            }
            catch (Exception fail)
            {
                return false;
            }
        }
예제 #11
0
        public bool checkEmail(string email, int? id)
        {
            var db = new DatabaseContext();
            Customers cust = db.Customers.FirstOrDefault(u => u.Email.Equals(email));

            if ((cust == null) || (cust != null && cust.Id == id))
                return true;
            else return false;  
        }
예제 #12
0
        public bool updatePw(int id, byte[] newPassword)
        {
            var db = new DatabaseContext();
            try
            {
                Customers cust = db.Customers.FirstOrDefault(u => u.Id == id);
                cust.Password = newPassword;
                db.SaveChanges();
                return true;
            }
            catch (Exception fail)
            {
                return false;
            }
 
        }
예제 #13
0
 public List<string> getAutoComplete(string term)
 {
     DatabaseContext db = new DatabaseContext();
     List<string> foundProducts;
     foundProducts = db.Products.Where(x => x.Name.StartsWith(term))
                                        .Select(y => y.Name).ToList();
     return foundProducts;
 }
예제 #14
0
 public List<Product> getResult(string searchString)
 {
     var db = new DatabaseContext();
     List<Product> foundProducts = new List<Product>();
     var products = db.Products.Include(p => p.SubCategories.Categories).Where(p => p.Name.ToUpper().Contains(searchString.ToUpper())
                     || p.Description.ToUpper().Contains(searchString.ToUpper())).ToList();
     foreach (var p in products)
     {
         var product = new Product()
         {
             itemnumber = p.Id,
             name = p.Name,
             description = p.Description,
             price = p.Price,
             volum = p.Volum,
             producer = p.Producers.Name,
             pricePerLitre = pricePerLitre(p.Price, p.Volum),
             category = p.SubCategories.Categories.Name,
             categoryid = p.SubCategories.Categories.Id,
             subCategory = p.SubCategories.Name,
             subCategoryid = p.SubCategories.Id,
             country = p.Countries.Name
         };
         foundProducts.Add(product);
     }
     return foundProducts;
 }
예제 #15
0
 public Product get(int id)
 {
     var db = new DatabaseContext();
     Products products = db.Products.Include(p => p.SubCategories.Categories).Where(p=> p.Id == id).FirstOrDefault<Products>();
     return new Product()
     {
         itemnumber = products.Id,
         name = products.Name,
         description = products.Description,
         longDescription = products.LongDescription,
         price = products.Price,
         volum = products.Volum,
         pricePerLitre = pricePerLitre(products.Price, products.Volum),
         producer = products.Producers.Name,
         category = products.SubCategories.Categories.Name,
         categoryid = products.SubCategories.Categories.Id,
         subCategory = products.SubCategories.Name,
         subCategoryid = products.SubCategories.Id,
         country = products.Countries.Name
     };
 }