상속: DbContext
예제 #1
0
 public bool cancelOrder(int nr)
 {
     using (var db = new CustomerContext())
     {
         try
         {
             var order = db.Orders.Find(nr);
             if (order == null)
                 return false;
             var productOrders = (from p in db.ProductOrders
                                  where p.orderNr == nr
                                  select p).ToList();
             foreach(var po in productOrders)
             {
                 db.Products.Find(po.productID).stock += po.quantity;
                 db.ProductOrders.Remove(po);
             }
             db.Orders.Remove(order);
             db.SaveChanges();
             return true;
         }
         catch (Exception ex)
         {
             _logging.logToDatabase(ex);
         }
         return false;
     }
 }
예제 #2
0
 //Legger employee inn i datatbasen
 public bool add(EmployeeModel employeeModel)
 {
     using (var db = new CustomerContext())
     {
         try
         {
             if (!(db.Employees.Find(employeeModel.employeeID) == null))//Hvis employeeModel har personID som finnes fra før
                 return false;
             if (find(employeeModel.username) != null)
                 return false;
             var newEmployee = new Employees()//Opretter ny employee
             {
                 email = employeeModel.username + "@kaffeplaneten.no",
                 firstName = employeeModel.firstName,
                 lastName = employeeModel.lastName,
                 phone = employeeModel.phone,
                 employeeAdmin = employeeModel.employeeAdmin,
                 customerAdmin = employeeModel.customerAdmin,
                 productAdmin = employeeModel.productAdmin,
                 orderAdmin   = employeeModel.orderAdmin,
                 databaseAdmin = employeeModel.databaseAdmin
             };
             newEmployee = db.Employees.Add(newEmployee);
             db.SaveChanges();
             employeeModel.employeeID = newEmployee.personID;//Lagrer personID i modellen for senere bruk
             return true;
         }
         catch (Exception ex)
         {
             _logging.logToDatabase(ex);
         }
     }//end using
     return false;
 }
예제 #3
0
        //henter ut en UserModel med Users.email lik email
        public UserModel get(string email)
        {
            var userModel = new UserModel();
            using (var db = new CustomerContext())
            {
                try
                {
                    var user = (from u in db.Users
                                where u.username.Equals(email)
                                select u).FirstOrDefault();
                    if (user == null)//tester om brukeren finnes
                        return null;

                    userModel.ID = user.personID;
                    userModel.passwordHash = user.password;
                    userModel.username = user.username;
                    return userModel;
                }
                catch (Exception ex)
                {
                    _logging.logToDatabase(ex);
                }
                return null;
            }
        }
예제 #4
0
 //Henter ut produkt med id lik id
 public ProductModel find(int id)
 {
     using (var db = new CustomerContext())
     {
         try
         {
             var product = db.Products.Find(id);
             var productModel = new ProductModel();
             if (product == null)
                 return null;
             productModel.productID = product.productID;
             productModel.productName = product.productName;
             productModel.price = product.price;
             productModel.imageURL = product.imageURL;
             productModel.description = product.description;
             productModel.category = product.category;
             productModel.stock = product.stock;
             return productModel;
         }
         catch (Exception ex)
         {
             _logging.logToDatabase(ex);
         }
         return null;
     }
 }
예제 #5
0
        public bool Delete(int id)
        {
            using (var db = new CustomerContext())
            {
                try
                {
                    Products slett = (from p in db.Products where p.productID == id select p).Single();
                    db.Products.Remove(slett);
                    db.SaveChanges();
                    return true;

                }
                catch (Exception ex)
                {
                    _logging.logToDatabase(ex);
                }
            }
            return false;
        }
예제 #6
0
        //Legger en Users inn i databasen
        public bool add(UserModel userModel)
        {
            using (var db = new CustomerContext())
            {
                try
                {
                    var user = (from u in db.Users
                                where u.username.Equals(userModel.username)
                                select u).FirstOrDefault();
                    if (user != null)
                        return false;
                    user = new Users()
                    {
                        username = userModel.username,
                        password = userModel.passwordHash
                    };
                    user.person = (from c in db.Customers
                                   where c.email.Equals(userModel.username)
                                   select c).SingleOrDefault();
                    if (user.person == null)//tester om Users sin customer finnes
                    {
                        user.person = (from e in db.Employees
                                       where e.email.Equals(userModel.username)
                                       select e).SingleOrDefault();
                        if (user.person == null)//tester om Users sin admin finnes
                        {
                            return false;
                        }
                    }

                    db.Users.Add(user);
                        db.SaveChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    _logging.logToDatabase(ex);
                }
                return false;
            }//end using
        }
예제 #7
0
 public bool delete(int id)
 {
     using (var db = new CustomerContext())
     {
         try
         {
             Users delUser = (from u in db.Users where u.personID == id select u).Single();
             Employees delEmployee= (from e in db.Employees where e.personID == id select e).Single();
             db.Users.Remove(delUser);
             db.Employees.Remove(delEmployee);
             //Adress, Orders og ProductOrder slettes automatisk
             db.SaveChanges();
             return true;
         }
         catch (Exception ex)
         {
             _logging.logToDatabase(ex);
         }
     }
     return false;
 }
예제 #8
0
        public List<OrderModel> allOrders()
        {
            var orderList = new List<OrderModel>();
            using (var db = new CustomerContext())
            {
                try
                {
                    var orders = (from o in db.Orders select o).ToList();
                    var orderModel = new OrderModel();

                    foreach (var o in orders) // finner alle ordre
                        orderList.Add(createOrderModel(o));
                    return orderList;
                }
                catch (Exception ex)
                {
                    _logging.logToDatabase(ex);
                }
            }
            return null;
        }
예제 #9
0
 //Legger et produkt inn i databasen
 public bool add(ProductModel productModel)
 {
     using (var db = new CustomerContext())
     {
         try
         {
             var product = new Products();
             product.category = productModel.category;
             product.description = productModel.description;
             product.imageURL = productModel.imageURL;
             product.price = productModel.price;
             product.productName = productModel.productName;
             product.stock = productModel.stock;
             db.Products.Add(product);
             db.SaveChanges();
             return true;
         }
         catch (Exception ex)
         {
             _logging.logToDatabase(ex);
         }
         return false;
     }
 }
예제 #10
0
        /*Legger Orders og ProductOrders inn i databasen. CustomerID og pruductID-ene må være med i modellen*/
        public bool add(OrderModel orderModel)
        {
            using (var db = new CustomerContext())
            {
                try
                {
                    var customer = db.Customers.Find(orderModel.customerID);
                    if (customer == null)//tester om ordren tilhører en eksisterende kunde
                        return false;
                    var order = new Orders();
                    order.Customers = customer;
                    db.Orders.Add(order);
                    db.SaveChanges();
                    orderModel.orderNr = order.orderNr;//lagrer ordrenummeret i modellen for senere bruk
                    return addProductOrders(orderModel);//legger til produkt ordrene

                }
                catch (Exception ex)
                {
                    _logging.logToDatabase(ex);
                }
                return false;
            }
        }
예제 #11
0
 //Oppdaterer Users data med dataen i userModel
 public bool update(UserModel userModel)
 {
     using (var db = new CustomerContext())
     {
         try
         {
             var user = db.Users.Find(userModel.ID);
             if (user == null)//tester om brukeren finnes
                 return false;
             user.person = db.Persons.Find(userModel.ID);
             if(!userModel.username.Equals(user.username))
             {
                 var email = (from p in db.Users
                              where p.username.Equals(userModel.username)
                              select p).FirstOrDefault();
                 if (email != null)//tester om epostadressen finnes fra før
                     return false;
                 user.username = userModel.username;
             }
             user.password = null;
             user.password = userModel.passwordHash;
             db.SaveChanges();
             return true;
         }
         catch (Exception ex)
         {
             _logging.logToDatabase(ex);
         }
         return false;
     }
 }
예제 #12
0
 //henter ut en UserModel fra User med customerID lik id
 public UserModel get(int id)
 {
     var userModel = new UserModel();
     using (var db = new CustomerContext())
     {
         try
         {
             var user = db.Users.Find(id);
             if (user == null)
                 return null;
             userModel.ID = id;
             userModel.passwordHash = user.password;
             userModel.username = user.username;
             return userModel;
         }
         catch (Exception ex)
         {
             _logging.logToDatabase(ex);
         }
         return null;
     }//end using
 }
예제 #13
0
        //Oppdaterer customeren som har customerID lik customerModel.customerID
        public bool update(CustomerModel customerModel)
        {
            using (var db = new CustomerContext())
            {
                try
                {
                    var customer = (from c in db.Customers
                                    where c.personID == customerModel.customerID
                                    select c).FirstOrDefault();
                    if (customer == null)//tester om customeren finnes
                        return false;

                    //Persondataendring:
                    customer.email = customerModel.email;
                    customer.firstName = customerModel.firstName;
                    customer.lastName = customerModel.lastName;
                    customer.phone = customerModel.phone;

                    customer.users.username = customerModel.email;
                    db.SaveChanges();

                    //Adresseendring:
                    var adressModel = new AdressModel();
                    adressModel.personID = customerModel.customerID;
                    adressModel.deliveryAdress = true;
                    adressModel.payAdress = customerModel.sameAdresses;
                    adressModel.province = customerModel.province;
                    adressModel.streetName = customerModel.adress;
                    adressModel.zipCode = customerModel.zipCode;
                    addAdress(adressModel);
                    if (!customerModel.sameAdresses)
                    {
                        adressModel = new AdressModel();
                        adressModel.personID = customerModel.customerID;
                        adressModel.deliveryAdress = false;
                        adressModel.payAdress = true;
                        adressModel.province = customerModel.payProvince;
                        adressModel.streetName = customerModel.payAdress;
                        adressModel.zipCode = customerModel.payZipcode;
                        addAdress(adressModel);
                    }
                    return true;
                }//emd try
                catch (Exception ex)
                {
                    _logging.logToDatabase(ex);
                }
            }//end using
            return false;
        }
예제 #14
0
        //Legger customer inn i datatbasen
        public bool add(CustomerModel IncCustomer)
        {
            using (var db = new CustomerContext())
            {
                try
                {
                    if (!(db.Customers.Find(IncCustomer.customerID) == null))//Hvis IncCustomer har customerID som finnes fra før
                        return false;
                    if (find(IncCustomer.email) != null)//Tester om eposten er i bruk fra før
                        return false;
                    var newCustomer = new Customers()//Opretter ny customer
                    {
                        email = IncCustomer.email,
                        firstName = IncCustomer.firstName,
                        lastName = IncCustomer.lastName,
                        phone = IncCustomer.phone
                    };
                    newCustomer = db.Customers.Add(newCustomer);
                    db.SaveChanges();
                    IncCustomer.customerID = newCustomer.personID;//Lagrer customerID i modellen for senere bruk
                }
                catch (Exception ex)
                {
                    _logging.logToDatabase(ex);
                    return false;
                }
            }//end using

            //Legger til adresser
            //Sjekker om adressene er like
            if (IncCustomer.payAdress.Equals(IncCustomer.adress))
            {
                var adressModel = new AdressModel()
                {
                    personID = IncCustomer.customerID,
                    payAdress = true,
                    deliveryAdress = true,
                    zipCode = IncCustomer.zipCode,
                    streetName = IncCustomer.adress,
                    province = IncCustomer.province
                };
                addAdress(adressModel);
            }
            //Ulike adresser
            else
            {
                var newPaymentAdress = new AdressModel()
                {
                    personID = IncCustomer.customerID,
                    payAdress = true,
                    deliveryAdress = false,
                    zipCode = IncCustomer.payZipcode,
                    streetName = IncCustomer.payAdress,
                    province = IncCustomer.payProvince

                };
                addAdress(newPaymentAdress);

                var newAdress = new AdressModel()
                {
                    personID = IncCustomer.customerID,
                    payAdress = false,
                    deliveryAdress = true,
                    zipCode = IncCustomer.zipCode,
                    streetName = IncCustomer.adress,
                    province = IncCustomer.province

                };
                addAdress(newAdress);
            }
            return true;
        }
예제 #15
0
 //Oppdaterer lagerstatur på produkt. Bruker productModel.stock som ny verdi
 public bool updateQuantity(ProductModel productModel)
 {
     using (var db = new CustomerContext())
     {
         try
         {
             var product = (from p in db.Products
                            where p.productID == productModel.productID
                            select p).FirstOrDefault();
             if (product == null)
                 return false;
             if (product.stock < productModel.stock)
                 return false;
             product.stock = productModel.stock;
             db.SaveChanges();
             return true;
         }
         catch (Exception ex)
         {
             _logging.logToDatabase(ex);
         }
         return false;
     }
 }
예제 #16
0
 public List<OrderModel> findOrders(CustomerModel customerModel)
 {
     var orderModelList = new List<OrderModel>();
     using(var db = new CustomerContext())
     {
         try
         {
             var orders = (from o in db.Orders
                           where o.personID == customerModel.customerID ||
                           o.Customers.email.Equals(customerModel.email) ||
                           o.Customers.phone.Equals(customerModel.phone)
                           select o).OrderBy(o => o.orderNr).ToList();
             foreach (var o in orders)
                 orderModelList.Add(createOrderModel(o));
             return orderModelList;
         }
         catch (Exception ex)
         {
             _logging.logToDatabase(ex);
         }
         return null;
     }
 }
예제 #17
0
 //Henter ut navnet på poststedet med postkode lik zipCode
 public string getProvince(string zipCode)
 {
     using (var db = new CustomerContext())
     {
         try
         {
             var province = db.Provinces.Find(zipCode);
             if (province == null)
                 return null;
             return province.province;
         }
         catch (Exception ex)
         {
             _logging.logToDatabase(ex);
         }
     }//end using
     return null;
 }
예제 #18
0
 //Henter ut en OrderModel fra en ordre med ordreNr lik nr
 public OrderModel find(int nr)
 {
     var orderModel = new OrderModel();
     using (var db = new CustomerContext())
     {
         try
         {
             var order = (from o in db.Orders
                          where o.orderNr == nr
                          select o).FirstOrDefault();
             return createOrderModel(order);
         }
         catch (Exception ex)
         {
             _logging.logToDatabase(ex);
         }
     }
     return null;
 }
예제 #19
0
        private OrderModel createOrderModel(Orders order)
        {
            var orderModel = new OrderModel();
            using (var db = new CustomerContext())
            {
                if (order == null)//tester om orderen finnes
                    return null;
                try
                {
                    orderModel.orderNr = order.orderNr;
                    orderModel.customerID = order.personID;

                    var productOrders = (from p in db.ProductOrders
                                         where p.orderNr == order.orderNr
                                         select p).ToList();
                    orderModel.total = 0;
                    var productDAL = new ProductDAL();
                    foreach (var o in productOrders)//legger produktene til i order modellen
                    {
                        var productModel = productDAL.find(o.products.productID);
                        productModel.quantity = o.quantity;
                        orderModel.products.Add(productModel);
                        orderModel.total += o.price;
                    }
                    return orderModel;
                }
                catch (Exception ex)
                {
                    _logging.logToDatabase(ex);
                }
            }
            return null;
        }
예제 #20
0
 /*Legger ProductOrders inn i databasen. OrderNr og pruductID-ene må være med i modellen*/
 private bool addProductOrders(OrderModel orderModel)
 {
     using (var db = new CustomerContext())
         {
         try
         {
             var order = db.Orders.Find(orderModel.orderNr);
             if (order == null)//tester om ordren eksisterer
                 return false;
             var productDAL = new ProductDAL();
             foreach (var p in orderModel.products)//Opretter ProductOrders fra modellen
             {
                 p.stock -= p.quantity;
                 productDAL.updateQuantity(p);
                 var productOrder = new ProductOrders();
                 productOrder.orders = order;
                 productOrder.price = p.price;
                 productOrder.products = db.Products.Find(p.productID);
                 productOrder.quantity = p.quantity;
                 productOrder.price = p.price * p.quantity;
                 db.ProductOrders.Add(productOrder);
             }
             db.SaveChanges();
             return true;
             }
         catch (Exception ex)
         {
             _logging.logToDatabase(ex);
         }
         return false;
     }
 }
예제 #21
0
 //Bekrefter brukernavn og passord for user
 public bool verifyUser(UserModel userModel)
 {
     using (var db = new CustomerContext())
     {
         try
         {
             var user = (from u in db.Users
                         where u.password == userModel.passwordHash && u.username == userModel.username
                         select u).SingleOrDefault();
             if (user == null)
                 return false;
             return true;
         }
         catch (Exception ex)
         {
             _logging.logToDatabase(ex);
         }
         return false;
     }//end using
 }
예제 #22
0
        //Henter ut navn på bruker med brukernavn lik email
        public CustomerModel find(string email)
        {
            var customerModel = new CustomerModel();
            using (var db = new CustomerContext())
            {
                try
                {
                    var temp = (from c in db.Customers
                                where c.email == email
                                select c).SingleOrDefault();
                    if (temp == null) //I prinsippet ikke nødvendig da denne metoden blir trigget da en kunde eksisterer
                        return null;
                    return find(temp.personID);

                }
                catch (Exception ex)
                {
                    _logging.logToDatabase(ex);
                }
                return null;
            }
        }
예제 #23
0
        public List<EmployeeModel> getAllEmployees()
        {
            using (var db = new CustomerContext())
            {
                var EmployeeList = new List<EmployeeModel>();
                try
                {

                    var employees = (from e in db.Employees select e).ToList();
                    if (employees != null)
                        foreach (var e in employees)
                        {
                            var empModel = new EmployeeModel();
                            empModel.employeeID = e.personID;
                            empModel.firstName = e.firstName;
                            empModel.lastName = e.lastName;
                            empModel.phone = e.phone;
                            EmployeeList.Add(empModel);
                        }
                    return EmployeeList;
                }
                catch (Exception ex)
                {
                    _logging.logToDatabase(ex);
                }
                return null;
            }
        }
예제 #24
0
 //Henter ut en liste med alle ordre for kunde med customerID lik id
 public List<OrderModel> findOrders(int id)
 {
     var orderModelList = new List<OrderModel>();
     using (var db = new CustomerContext())
     {
         try
         {
             var orders = (from o in db.Orders
                          where o.personID == id
                          select o).ToList();
             foreach (var o in orders)//legger order modellene inn i listen
                 orderModelList.Add(find(o.orderNr));
             return orderModelList;
         }
         catch (Exception ex)
         {
             _logging.logToDatabase(ex);
         }
     }//end using
     return null;
 }
예제 #25
0
        //Henter ut en EmployeeModel for employee med personID lik id
        public EmployeeModel find(int id)
        {
            var employeeModel = new EmployeeModel();
            using (var db = new CustomerContext())
            {
                try
                {
                    var temp = (from c in db.Employees
                                where c.personID == id
                                select c).FirstOrDefault();

                    if (temp == null)//Tester om employeen finnes
                        return null;
                    employeeModel.employeeID = temp.personID;
                    employeeModel.firstName = temp.firstName;
                    employeeModel.lastName = temp.lastName;
                    employeeModel.username = temp.email;
                    employeeModel.phone = temp.phone;
                    employeeModel.customerAdmin = temp.customerAdmin;
                    employeeModel.orderAdmin = temp.orderAdmin;
                    employeeModel.employeeAdmin = temp.employeeAdmin;
                    employeeModel.productAdmin = temp.productAdmin;
                    employeeModel.databaseAdmin = temp.databaseAdmin;

                    return employeeModel;
                }//end try
                catch (Exception ex)
                {
                    _logging.logToDatabase(ex);
                }
            }//end using
            return null;
        }
예제 #26
0
        //Henter ut en CustomerModel for customer med customerID lik id
        public CustomerModel find(int id)
        {
            var customerModel = new CustomerModel();
            using (var db = new CustomerContext())
            {
                try
                {
                    var temp = (from c in db.Customers
                                where c.personID == id
                                select c).FirstOrDefault();

                    if (temp == null)//Tester om customeren finnes
                        return null;
                    customerModel.customerID = temp.personID;
                    customerModel.firstName = temp.firstName;
                    customerModel.lastName = temp.lastName;
                    customerModel.email = temp.email;
                    customerModel.phone = temp.phone;

                    List<Adresses> adresses = (from a in db.Adresses
                                               where a.personID == customerModel.customerID
                                               select a).ToList();

                    foreach (var a in adresses)//Legger adressene inn i CustomerModelen
                    {
                        if (a.deliveryAdress)
                        {
                            customerModel.adress = a.streetName;
                            customerModel.province = a.province.province;
                            customerModel.zipCode = a.zipCode;
                        }
                        if (a.payAdress)
                        {
                            customerModel.payAdress = a.streetName;
                            customerModel.payProvince = a.province.province;
                            customerModel.payZipcode = a.zipCode;
                        }
                    }
                    return customerModel;
                }//end try
                catch (Exception ex)
                {
                    _logging.logToDatabase(ex);
                }
            }//end using
            return null;
        }
예제 #27
0
        //Henter alle produkter fra databasen og oppretter liste av modelobjekter
        public List<ProductModel> getAllProducts()
        {
            using (var db = new CustomerContext())
            {
                List<ProductModel> ProductList = new List<ProductModel>();
                try
                {

                    ProductList = new List<ProductModel>();

                    var produkter = (from p in db.Products select p).ToList();
                    if(produkter!=null)
                    foreach (var p in produkter)
                    {
                        var newProductModel = new ProductModel();
                        newProductModel.productID = p.productID;
                        newProductModel.category = p.category;
                        newProductModel.productName = p.productName;
                        newProductModel.price = p.price;
                        newProductModel.imageURL = p.imageURL;
                        newProductModel.description = p.description;
                        newProductModel.stock = p.stock;
                        ProductList.Add(newProductModel);
                    }
                    return ProductList;
                }
                catch (Exception ex)
                {
                    _logging.logToDatabase(ex);
                }
                return null;
            }
        }
예제 #28
0
        public bool update(ProductModel _productModel)
        {
            using (var db = new CustomerContext())
            {
                try
                {
                    var product = db.Products.Find(_productModel.productID);
                    product.productName = _productModel.productName;
                    product.price = _productModel.price;
                    product.imageURL = _productModel.imageURL;
                    product.stock = _productModel.stock;
                    product.category = _productModel.category;
                    product.description = _productModel.description;
                    db.SaveChanges();
                    return true;

                }
                catch (Exception ex)
                {
                    _logging.logToDatabase(ex);
                }
            }
            return false;
        }
예제 #29
0
        //Henter ut navn på bruker med brukernavn lik email
        public EmployeeModel find(string email)
        {
            var employeeModel = new EmployeeModel();
            using (var db = new CustomerContext())
            {
                try
                {
                    var temp = (from c in db.Employees
                                where c.email == email
                                select c).SingleOrDefault();
                    if (temp == null)
                        return null;
                    return find(temp.personID);

                }
                catch (Exception ex)
                {
                    _logging.logToDatabase(ex);
                }
                return null;
            }
        }
예제 #30
0
        public List<CustomerModel> allCustomers()
        {
            using (var db = new CustomerContext())
            {
                List<CustomerModel> customerList = new List<CustomerModel>();
                try
                {
                    foreach(var c in db.Customers)
                    {

                        customerList.Add(find(c.personID));
                    }

                    return customerList;

                }
                catch (Exception ex)
                {
                    _logging.logToDatabase(ex);
                }
                return null;
            }
        }