Пример #1
0
 /// <summary>
 /// Gets a list of customers for use in drop-downs
 /// </summary>
 /// <returns></returns>
 public IList<Customer> GetCustomers()
 {
     using (var context = new HandledNSTEntities())
     {
         return context.Customers.ToList();
     }
 }
Пример #2
0
        /// <summary>
        /// Adds a new order and order details
        /// </summary>
        /// <param name="newOrder"></param>
        /// <returns></returns>
        public int AddOrder(Order newOrder)
        {
            using (var context = new HandledNSTEntities())
            {
                context.Orders.Add(newOrder);
                context.SaveChanges();

                return newOrder.Id;
            }
        }
Пример #3
0
 /// <summary>
 /// Gets a single order by Id
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public Order GetOrderById(int id)
 {
     using (var context = new HandledNSTEntities())
     {
         return context.Orders
             .Include("Customer")
             .Include("OrderDetails")
             .Include("OrderDetails.Product")
             .FirstOrDefault(x => x.Id == id);
     }
 }
Пример #4
0
        /// <summary>
        /// Implements a quasi-repository for searching orders
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public IList<OrderSearchResult> GetOrders(OrderSearchCriteria criteria)
        {
            using (var context = new HandledNSTEntities())
            {
                IQueryable<Order> matches = context.Orders
                    .Include("Customer")
                    .Include("OrderDetails")
                    .Include("OrderDetails.Product");

                if (!string.IsNullOrWhiteSpace(criteria.OrderNumber.Trim()))
                {
                    matches = matches.Where(x => x.Number.Equals(criteria.OrderNumber));
                }

                if (!string.IsNullOrWhiteSpace(criteria.SKU.Trim()))
                {
                    matches = matches.Where(x => x.OrderDetails.Any(y => y.Product.SKU.Equals(criteria.SKU)));
                }

                if (!string.IsNullOrWhiteSpace(criteria.FirstName.Trim()))
                {
                    matches = matches.Where(x => x.Customer.FirstName.Contains(criteria.FirstName));
                }

                if (!string.IsNullOrWhiteSpace(criteria.LastName.Trim()))
                {
                    matches = matches.Where(x => x.Customer.LastName.Contains(criteria.LastName));
                }

                if (criteria.OrderStartDate.HasValue)
                {
                    matches = matches.Where(x => x.OrderDate >= criteria.OrderStartDate);
                }

                if (criteria.OrderEndDate.HasValue)
                {
                    matches = matches.Where(x => x.OrderDate <= criteria.OrderEndDate);
                }

                // The following call to .ToList() is what actually executes the query against
                // the database.
                return (from entity in matches.ToList()
                        select new OrderSearchResult
                                   {
                                       Id = entity.Id,
                                       OrderNumber = entity.Number,
                                       OrderDate = entity.OrderDate,
                                       FirstName = entity.Customer.FirstName,
                                       LastName = entity.Customer.LastName
                                   }).ToList();

            }
        }
Пример #5
0
        /// <summary>
        /// Updates an existing order and order details
        /// </summary>
        /// <param name="order"></param>
        public void UpdateOrder(Order order)
        {
            using (var context = new HandledNSTEntities())
            {
                // Attaching the parent object will automatically
                // attach all the child details.
                context.Orders.Attach(order);

                // Since these entities can't be self-tracking due
                // to MVC, we need to manually set their state.
                DbEntityEntry orderEntry = context.Entry(order);
                orderEntry.State = EntityState.Modified;

                // We also need to set the state for all the children individually as
                // this will not cascade down from the parent.
                foreach (var detailEntry in order.OrderDetails.Select(context.Entry))
                {
                    ((DbEntityEntry) detailEntry).State = EntityState.Modified;
                }

                context.SaveChanges();
            }
        }
Пример #6
0
 /// <summary>
 /// Gets a list of products for use in drop-downs
 /// </summary>
 /// <returns></returns>
 public IList<Product> GetProducts()
 {
     using (var context = new HandledNSTEntities())
     {
         return context.Products.ToList();
     }
 }