예제 #1
0
        public string CreateOrder(List <Product> productList, String cred)
        {
            using (var db = new SouthwindContext())
            {
                var order = new Order()
                {
                    CustomerId = cred, OrderDate = DateTime.Now, ShippedDate = DateTime.Now.AddDays(1), ShipCountry = "UK"
                };
                // db.Orders.Add(order);
                //db.SaveChanges();

                // int orderid = db.Orders.Last().OrderId;

                foreach (var product in productList)
                {
                    var orderDetail = new OrderDetail()
                    {
                        ProductId = product.ProductID, Order = order, UnitPrice = Decimal.Parse(product.ProductPrice), Discount = 0, Quantity = 1
                    };
                    db.OrderDetails.Add(orderDetail);
                    db.SaveChanges();
                }

                return(GetReceipt(order));
            }
        }
예제 #2
0
        public static void DeleteOrder(int orderId)
        {
            using (var db = new SouthwindContext())
            {
                var orderDetailsForThisOrder =
                    from od in db.OrderDetails
                    where od.OrderId == orderId
                    select od;

                bool shipped = GetOrder(orderId).ShippedDate != null;

                foreach (var od in orderDetailsForThisOrder)
                {
                    if (!shipped)
                    {
                        DecreaseProductStock(od.ProductId, -1 * od.Quantity);
                    }
                    DeleteOrderDetail(od.OrderDetailId);
                }
            }

            using (var db = new SouthwindContext())
            {
                var orderToBeDeleted = db.Orders.Find(orderId);

                db.Orders.Remove(orderToBeDeleted);

                db.SaveChanges();
            }
        }
예제 #3
0
 public void DeleteCustomer(string customerID)
 {
     using (var db = new SouthwindContext())
     {
         var selectedCustomer = db.Customers.Where(c => c.CustomerId == customerID);
         db.Customers.RemoveRange(selectedCustomer);
         db.SaveChanges();
     }
 }
예제 #4
0
        public static void DeleteProduct(int productId)
        {
            using (var db = new SouthwindContext())
            {
                db.Products.Find(productId).Discontinued = true;

                db.SaveChanges();
            }
        }
예제 #5
0
        public static void DeleteOrderDetail(int orderDetailId)
        {
            using (var db = new SouthwindContext())
            {
                db.OrderDetails.Remove(db.OrderDetails.Find(orderDetailId));

                db.SaveChanges();
            }
        }
예제 #6
0
 public void RemoveCustomer(string customerId)
 {
     using (var db = new SouthwindContext())
     {
         var selectedCustomer =
             from c in db.Customers
             where c.CustomerId == customerId
             select c;
         db.Customers.RemoveRange(selectedCustomer);
         db.SaveChanges();
     }
 }
예제 #7
0
        public static void UpdateOrder(int orderId, DateTime shippedDate, string shipCountry)
        {
            using (var db = new SouthwindContext())
            {
                var order = db.Orders.Find(orderId);

                order.ShippedDate = shippedDate;
                order.ShipCountry = shipCountry;

                db.SaveChanges();
            }
        }
예제 #8
0
        public void CreateEmployee(string employeeID, string employeeName, string postalCode, string city = null)
        {
            var newEmployee = new Employee()
            {
                EmployeeId = employeeID, EmployeeName = employeeName, City = city, PostalCode = postalCode
            };

            using (var db = new SouthwindContext())
            {
                db.Employees.Add(newEmployee);
                db.SaveChanges();
            }
        }
예제 #9
0
        public void DeleteEmployee(string employeeID)
        {
            using (var db = new SouthwindContext())
            {
                var selectedEmployee =
                    from e in db.Employees
                    where e.EmployeeId == employeeID
                    select e;

                db.Employees.RemoveRange(selectedEmployee);
                db.SaveChanges();
            }
        }
예제 #10
0
        public void TearDown()
        {
            using (var db = new SouthwindContext())
            {
                var selectedEmployees =
                    from e in db.Employees
                    where e.EmployeeId == "MANDA"
                    select e;

                db.Employees.RemoveRange(selectedEmployees);
                db.SaveChanges();
            }
        }
예제 #11
0
        public void CreateCustomer(string customerID, string contactName, string city, string country, string postcode, string username, string password, char role)
        {
            var newCustomer = new Customer()
            {
                CustomerId = customerID, ContactName = contactName, City = city, Country = country, PostalCode = postcode, Username = username, Password = password, Role = role
            };

            using (var db = new SouthwindContext())
            {
                db.Customers.Add(newCustomer);
                db.SaveChanges();
            }
        }
예제 #12
0
        public void TearDown()
        {
            using (var db = new SouthwindContext())
            {
                var selectedCustomers =
                    from c in db.Customers
                    where c.CustomerId == "BLACK"
                    select c;

                db.Customers.RemoveRange(selectedCustomers);
                db.SaveChanges();
            }
        }
예제 #13
0
        public void UpdateCustomer(string customerID, string contactName, string city, string country, string postcode)
        {
            using (var db = new SouthwindContext())
            {
                SelectedCustomer             = db.Customers.Find(customerID);
                SelectedCustomer.ContactName = contactName;
                SelectedCustomer.City        = city;
                SelectedCustomer.Country     = country;
                SelectedCustomer.PostalCode  = postcode;

                db.SaveChanges();
            }
        }
예제 #14
0
        public void CreateCustomer(string customerId, string contactName, string companyName, string city = null)
        {
            var newCust = new Customer()
            {
                CustomerId = customerId, ContactName = contactName
            };

            using (var db = new SouthwindContext())
            {
                db.Customers.Add(newCust);
                db.SaveChanges();
            }
        }
예제 #15
0
 public void Setup()
 {
     _customerManager = new CustomerManager();
     using (var db = new SouthwindContext())
     {
         var query =
             from c in db.Customers
             where c.CustomerId == "WILLC"
             select c;
         db.RemoveRange(query);
         db.SaveChanges();
     }
 }
예제 #16
0
 public void UpdateCustomer(string customerId, string contactName, string city, string postcode, string country)
 {
     using (var db = new SouthwindContext())
     {
         SelectedCustomer             = db.Customers.Where(c => c.CustomerId == customerId).FirstOrDefault();
         SelectedCustomer.ContactName = contactName;
         SelectedCustomer.City        = city;
         SelectedCustomer.PostalCode  = postcode;
         SelectedCustomer.Country     = country;
         // write changes to database
         db.SaveChanges();
     }
 }
예제 #17
0
        public void Setup()
        {
            _crudmanager = new CRUDManager();
            using (var db = new SouthwindContext())
            {
                var selectedCustomers =
                    from c in db.Customers
                    where c.CustomerId == "BLACK"
                    select c;

                db.Customers.RemoveRange(selectedCustomers);
                db.SaveChanges();
            }
        }
예제 #18
0
        public static void UpdateCustomer(string customerId, string contactName, string city, string postalCode, string country)
        {
            using (var db = new SouthwindContext())
            {
                var customer = db.Customers.Find(customerId);

                customer.ContactName = contactName;
                customer.City        = city;
                customer.PostalCode  = postalCode;
                customer.Country     = country;

                db.SaveChanges();
            }
        }
예제 #19
0
        public static void UpdateProduct(int productId, string productName, double price, int stock, DateTime dateAdded)
        {
            using (var db = new SouthwindContext())
            {
                var product = db.Products.Find(productId);

                product.ProductName = productName;
                product.Price       = price;
                product.Stock       = stock;
                product.DateAdded   = dateAdded;

                db.SaveChanges();
            }
        }
예제 #20
0
        public static void DecreaseProductStock(int productId, int decreaseAmount)
        {
            using (var db = new SouthwindContext())
            {
                var product = db.Products.Find(productId);

                if (decreaseAmount > product.Stock)
                {
                    throw new Exception("Should not be able to decrease stock to less than 0");
                }
                product.Stock -= decreaseAmount;

                db.SaveChanges();
            }
        }
예제 #21
0
 public void WhenAEmployeeIsRemoved_TheyAreNoLongerInTheDatabase()
 {
     using (var db = new SouthwindContext())
     {
         var newEmployee = new Employee()
         {
             EmployeeId = "MANDA", EmployeeName = "Nish Mandal", PostalCode = "BRUM10", City = "Birmingham"
         };
         db.Employees.Add(newEmployee);
         db.SaveChanges();
         _employeeManager.DeleteEmployee("MANDA");
         var MANDACount = db.Employees.Where(e => e.EmployeeId == "MANDA").Count();
         Assert.AreEqual(0, MANDACount);
     }
 }
예제 #22
0
        public void Setup()
        {
            _employeeManager = new EmployeeManager();
            // remove test entry in DB if present
            using (var db = new SouthwindContext())
            {
                var selectedEmployee =
                    from e in db.Employees
                    where e.EmployeeId == "MANDA"
                    select e;

                db.Employees.RemoveRange(selectedEmployee);
                db.SaveChanges();
            }
        }
예제 #23
0
        public static void CreateNewOrder(string customerId, DateTime orderDate, string shipCountry, List <OrderDetail> orderDetails)
        {
            using (var db = new SouthwindContext())
            {
                db.Add(new Order()
                {
                    CustomerId   = customerId,
                    OrderDate    = orderDate,
                    ShipCountry  = shipCountry,
                    OrderDetails = orderDetails
                });

                db.SaveChanges();
            }
        }
예제 #24
0
        public static void CreateNewProduct(string productName, double price, int stock = 0)
        {
            using (var db = new SouthwindContext())
            {
                db.Add(new Product()
                {
                    ProductName  = productName,
                    Price        = price,
                    Stock        = stock,
                    DateAdded    = DateTime.Now,
                    Discontinued = false
                });

                db.SaveChanges();
            }
        }
예제 #25
0
        public static void CreateNewCustomer(string customerId, string contactName, string city, string postalCode, string country)
        {
            using (var db = new SouthwindContext())
            {
                db.Add(new Customer()
                {
                    CustomerId  = customerId,
                    ContactName = contactName,
                    City        = city,
                    PostalCode  = postalCode,
                    Country     = country
                });

                db.SaveChanges();
            }
        }
예제 #26
0
        public static void DeleteCustomer(string customerId)
        {
            using (var db = new SouthwindContext())
            {
                var ordersByThisCustomer =
                    from o in db.Orders
                    where o.CustomerId == customerId
                    select o.OrderId;

                foreach (var o in ordersByThisCustomer)
                {
                    DeleteOrder(o);
                }

                db.Customers.Remove(db.Customers.Find(customerId));

                db.SaveChanges();
            }
        }