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