Esempio n. 1
0
        public static List <Order> FilterOrders(string?customerId, int?productId)
        {
            using (var db = new SouthwindContext())
            {
                var orders           = RetrieveAllOrders();
                var filteredCustomer = new List <Order>();
                var filteredProducts = new List <Order>();

                if (customerId == null)
                {
                    filteredCustomer = orders;
                }
                else
                {
                    foreach (var order in orders)
                    {
                        if (order.CustomerId == customerId)
                        {
                            filteredCustomer.Add(order);
                            continue;
                        }
                    }
                }

                if (productId == null)
                {
                    filteredProducts = filteredCustomer;
                }
                else
                {
                    foreach (var order in filteredCustomer)
                    {
                        if (DoesOrderHaveThisProduct(order, productId))
                        {
                            filteredProducts.Add(order);
                            continue;
                        }
                    }
                }

                return(filteredProducts);
            }
        }
Esempio n. 2
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();
            }
        }
Esempio n. 3
0
        public static bool DoesOrderHaveThisProduct(Order order, int?productId)
        {
            using (var db = new SouthwindContext())
            {
                var orderDetailsForThisOrder =
                    db.Orders
                    .Include(o => o.OrderDetails)
                    .Where(o => o.OrderId == order.OrderId)
                    .Select(p => p.OrderDetails)
                    .FirstOrDefault();

                foreach (var orderDetail in orderDetailsForThisOrder.ToList())
                {
                    if (orderDetail.ProductId == productId)
                    {
                        return(true);
                    }
                }

                return(false);
            }
        }
Esempio n. 4
0
        public string GetReceipt(Order order)
        {
            using (var db = new SouthwindContext())
            {
                var orderList = db.OrderDetails.Include(o => o.Order).ThenInclude(c => c.Customer).Include(p => p.Product).Where(o => o.OrderId == order.OrderId).ToList();
                var message   = $"OrderID :{order.OrderId}\n{orderList[0].Order.Customer.ContactName}\nOrder made on: {order.OrderDate}";
                var total     = 0m;

                if (orderList != null)
                {
                    foreach (var item in orderList)
                    {
                        message += $"\n\t{item.Product.ProductName} - {item.UnitPrice.ToString("c")}";
                        total   += item.UnitPrice;
                    }

                    message += $"\n -------------\n  Total Price: {total.ToString("c")}";
                    return(message);
                }

                return("");
            }
        }
Esempio n. 5
0
 public CustomerService(SouthwindContext dbContext)
 {
     // 呼叫端有注入 DbContext 物件,就用對方提供的。
     this.db = dbContext;
 }
Esempio n. 6
0
 public CustomerService(SouthwindContext dbContext)
 {
     // 呼叫端有注入 DbContext 物件,就用對方提供的。
     this.db = dbContext;
 }
Esempio n. 7
0
 public CustomerService()
 {
     // 提供預設的 DbContext 物件。
     db = SouthwindContext.InstanceInCurrentRequest;
 }
Esempio n. 8
0
 public CustomerService()
 {
     // 提供預設的 DbContext 物件。
     db = new SouthwindContext();
 }
Esempio n. 9
0
 public CustomerService()
 {
     // 提供預設的 DbContext 物件。
     db = SouthwindContext.InstanceInCurrentRequest;
 }
Esempio n. 10
0
 public CustomerService()
 {
     // 提供預設的 DbContext 物件。
     db = new SouthwindContext();
 }
Esempio n. 11
0
 /// <summary>
 /// 调用方注入DbContext对象
 /// </summary>
 public CustomerService(SouthwindContext dbContext)
 {
     this.db = dbContext;
 }
Esempio n. 12
0
 public CustomerService()
 {
     // 提供默认的DbContext对象
     db = new SouthwindContext();
 }