internal ActionData(ActionDataContext context) { _context = context; _attachment = new Field <Attachment>(_context, nameof(Attachment)); _board = new Field <Board>(_context, nameof(Board)); _boardSource = new Field <Board>(_context, nameof(BoardSource)); _boardTarget = new Field <Board>(_context, nameof(BoardTarget)); _card = new Field <Card>(_context, nameof(Card)); _cardSource = new Field <Card>(_context, nameof(CardSource)); _checkItem = new Field <CheckItem>(_context, nameof(CheckItem)); _checkList = new Field <CheckList>(_context, nameof(CheckList)); _label = new Field <Label>(_context, nameof(Label)); _lastEdited = new Field <DateTime?>(_context, nameof(LastEdited)); _list = new Field <List>(_context, nameof(List)); _listAfter = new Field <List>(_context, nameof(ListAfter)); _listBefore = new Field <List>(_context, nameof(ListBefore)); _member = new Field <Member>(_context, nameof(Member)); _wasArchived = new Field <bool?>(_context, nameof(WasArchived)); _oldDescription = new Field <string>(_context, nameof(OldDescription)); _oldList = new Field <List>(_context, nameof(OldList)); _oldPosition = new Field <Position>(_context, nameof(OldPosition)); _oldText = new Field <string>(_context, nameof(OldText)); _organization = new Field <Organization>(_context, nameof(Organization)); _powerUp = new Field <PowerUpBase>(_context, nameof(PowerUp)); _text = new Field <string>(_context, nameof(Text)); _text.AddRule(OldValueNotNullOrWhiteSpaceRule.Instance); _value = new Field <string>(_context, nameof(Value)); }
/// <summary> /// Gets list of product categories /// </summary> /// <returns>List of categories.</returns> public IList <Category> GetCategories() { using (ActionDataContext db = DataContextFactory.CreateContext()) { return(db.CategoryEntities.Select(c => Mapper.ToBusinessObject(c)).ToList()); } }
/// <summary> /// Updates a customer record in the database. /// </summary> /// <param name="customer">The customer with updated values.</param> /// <returns>Number of rows affected.</returns> public int UpdateCustomer(Customer customer) { using (ActionDataContext db = DataContextFactory.CreateContext()) { try { CustomerEntity entity = Mapper.ToEntity(customer); db.CustomerEntities.Attach(entity, true); db.SubmitChanges(); // Update business object with new version customer.Version = VersionConverter.ToString(entity.Version); return(1); } catch (ChangeConflictException) { throw new Exception("A change to customer record was made before your changes."); } catch { return(0); } } }
/// <summary> /// Gets a product given a product identifier. /// </summary> /// <param name="productId">Product identifier.</param> /// <returns>The product.</returns> public Product GetProduct(int productId) { using (ActionDataContext db = DataContextFactory.CreateContext()) { return(Mapper.ToBusinessObject(db.ProductEntities .SingleOrDefault(p => p.ProductId == productId))); } }
/// <summary> /// Gets a customer given a customer identifier. /// </summary> /// <param name="customerId">The customer identifier.</param> /// <returns>The customer.</returns> public Customer GetCustomer(int customerId) { using (ActionDataContext db = DataContextFactory.CreateContext()) { return(Mapper.ToBusinessObject(db.CustomerEntities .SingleOrDefault(p => p.CustomerId == customerId))); } }
/// <summary> /// Gets order given an order identifier. /// </summary> /// <param name="orderId">Order identifier.</param> /// <returns>The order.</returns> public Order GetOrder(int orderId) { using (ActionDataContext db = DataContextFactory.CreateContext()) { return(Mapper.ToBusinessObject(db.OrderEntities .SingleOrDefault(o => o.OrderId == orderId))); } }
/// <summary> /// Gets the orders between a given data range. /// </summary> /// <param name="dateFrom">Start date.</param> /// <param name="dateThru">End date.</param> /// <returns></returns> public IList <Order> GetOrdersByDate(DateTime dateFrom, DateTime dateThru) { using (ActionDataContext db = DataContextFactory.CreateContext()) { return(db.OrderEntities .Where(o => o.OrderDate >= dateFrom && o.OrderDate <= dateThru) .Select(c => Mapper.ToBusinessObject(c)).ToList()); } }
/// <summary> /// Gets all orders for a given customer. /// </summary> /// <param name="customerId">Unique customer identifier.</param> /// <returns>List of orders.</returns> public IList <Order> GetOrders(int customerId) { using (ActionDataContext db = DataContextFactory.CreateContext()) { return(db.OrderEntities .Where(o => o.CustomerId == customerId) .Select(c => Mapper.ToBusinessObject(c)).ToList()); } }
/// <summary> /// Gets customer given an order. /// </summary> /// <param name="orderId">The identifier for the order for which customer is requested.</param> /// <returns>The customer.</returns> public Customer GetCustomerByOrder(int orderId) { using (ActionDataContext db = DataContextFactory.CreateContext()) { return(Mapper.ToBusinessObject(db.CustomerEntities.SelectMany(c => db.OrderEntities .Where(o => c.CustomerId == o.CustomerId && o.OrderId == orderId), (c, o) => c).SingleOrDefault(c => true))); } }
/// <summary> /// Gets the orderdetails for a given order. /// </summary> /// <param name="orderId">Unique order identifier.</param> /// <returns>List of orderdetails.</returns> public IList <OrderDetail> GetOrderDetails(int orderId) { using (ActionDataContext db = DataContextFactory.CreateContext()) { return(db.OrderDetailEntities .Where(od => od.OrderId == orderId) .Select(od => Mapper.ToBusinessObject(od)) .ToList()); } }
/// <summary> /// Gets category for a given a product. /// </summary> /// <param name="productId">The product identifier.</param> /// <returns>The category.</returns> public Category GetCategoryByProduct(int productId) { using (ActionDataContext db = DataContextFactory.CreateContext()) { return(Mapper.ToBusinessObject(db.CategoryEntities.SelectMany(c => db.ProductEntities .Where(p => c.CategoryId == p.CategoryId) .Where(p => p.ProductId == productId), (c, p) => c).SingleOrDefault(c => true))); } }
/// <summary> /// Inserts a new customer record to the database. /// </summary> /// <param name="customer">The customer to be inserted.</param> public void InsertCustomer(Customer customer) { using (ActionDataContext db = DataContextFactory.CreateContext()) { try { CustomerEntity entity = Mapper.ToEntity(customer); db.CustomerEntities.InsertOnSubmit(entity); db.SubmitChanges(); // update business object with new version and id customer.CustomerId = entity.CustomerId; customer.Version = VersionConverter.ToString(entity.Version); } catch (ChangeConflictException) { throw new Exception("A change to customer record was made before your changes."); } } }
/// <summary> /// Deletes a customer record from the database. /// </summary> /// <param name="customer">The customer to be deleted.</param> /// <returns>Number of rows affected.</returns> public int DeleteCustomer(Customer customer) { using (ActionDataContext db = DataContextFactory.CreateContext()) { try { CustomerEntity entity = Mapper.ToEntity(customer); db.CustomerEntities.Attach(entity, false); db.CustomerEntities.DeleteOnSubmit(entity); db.SubmitChanges(); return(1); } catch (ChangeConflictException) { throw new Exception("A change to customer record was made before your changes."); } catch (Exception) { return(0); } } }
/// <summary> /// Gets list of customers in given sortorder. /// </summary> /// <param name="sortExpression">The required sort order.</param> /// <returns>List of customers.</returns> public IList <Customer> GetCustomers(string sortExpression) { using (ActionDataContext db = DataContextFactory.CreateContext()) { IQueryable <CustomerEntity> query = db.CustomerEntities; if (sortExpression.Length > 0) { string[] sort = sortExpression.Split(' '); string sortColumn = sort[0]; string sortOrder = sort[1]; switch (sortColumn) { case "CustomerId": if (sortOrder == "ASC") { query = query.OrderBy(c => c.CustomerId); } else { query = query.OrderByDescending(c => c.CustomerId); } break; case "CompanyName": if (sortOrder == "ASC") { query = query.OrderBy(c => c.CompanyName); } else { query = query.OrderByDescending(c => c.CompanyName); } break; case "City": if (sortOrder == "ASC") { query = query.OrderBy(c => c.City); } else { query = query.OrderByDescending(c => c.City); } break; case "Country": if (sortOrder == "ASC") { query = query.OrderBy(c => c.Country); } else { query = query.OrderByDescending(c => c.Country); } break; } } return(query.Select(c => Mapper.ToBusinessObject(c)).ToList()); } }
/// <summary> /// Gets list of product categories for a given category /// </summary> /// <param name="categoryId">The category for which products are requested.</param> /// <param name="sortExpression">Sort order.</param> /// <returns>List of products.</returns> public IList <Product> GetProductsByCategory(int categoryId, string sortExpression) { string[] sort = sortExpression.Split(' '); string sortColumn = sort[0]; string sortOrder = sort[1]; using (ActionDataContext db = DataContextFactory.CreateContext()) { // build query tree var query = db.ProductEntities.Where(p => p.CategoryId == categoryId); switch (sortColumn) { case "ProductId": if (sortOrder == "ASC") { query = query.OrderBy(p => p.ProductId); } else { query = query.OrderByDescending(p => p.ProductId); } break; case "ProductName": if (sortOrder == "ASC") { query = query.OrderBy(p => p.ProductName); } else { query = query.OrderByDescending(p => p.ProductName); } break; case "Weight": if (sortOrder == "ASC") { query = query.OrderBy(p => p.Weight); } else { query = query.OrderByDescending(p => p.Weight); } break; case "UnitPrice": if (sortOrder == "ASC") { query = query.OrderBy(p => p.UnitPrice); } else { query = query.OrderByDescending(p => p.UnitPrice); } break; } return(query.Select(p => Mapper.ToBusinessObject(p)).ToList()); } }
/// <summary> /// Gets a list of customers with order summary statistics. /// </summary> /// <param name="customers"></param> /// <param name="sortExpression"></param> /// <returns></returns> public IList <Customer> GetOrderStatistics(IList <Customer> customers, string sortExpression) { // Place customerIds in an integer list IList <int> customerIds = new List <int>(); foreach (Customer customer in customers) { customerIds.Add(customer.CustomerId); } using (ActionDataContext db = DataContextFactory.CreateContext()) { var query = from o in db.OrderEntities where customerIds.Contains <int>(o.CustomerId) group o by o.CustomerId into g select new { CustomerId = g.Key, LastOrderDate = g.Max(d => d.OrderDate), NumOrders = g.Count() }; IList <Customer> list = new List <Customer>(); // Loop over customer list first to preserve sort order foreach (Customer customer in customers) { foreach (var item in query) { if (item.CustomerId == customer.CustomerId) { customer.NumOrders = item.NumOrders; customer.LastOrderDate = item.LastOrderDate; list.Add(customer); break; } } } // Here we perform in-memory postprocessing of sort order if (sortExpression.Length > 0) { string[] sort = sortExpression.Split(' '); string sortColumn = sort[0]; string sortOrder = sort[1]; switch (sortColumn) { case "NumOrders": if (sortOrder == "ASC") { list = list.OrderBy(c => c.NumOrders).ToList(); } else { list = list.OrderByDescending(c => c.NumOrders).ToList(); } break; case "LastOrderDate": if (sortOrder == "ASC") { list = list.OrderBy(c => c.LastOrderDate).ToList(); } else { list = list.OrderByDescending(c => c.LastOrderDate).ToList(); } break; } } return(list); } }
/// <summary> /// Searches for products given a set of criteria /// </summary> /// <param name="productName">Product Name criterium. Could be partial.</param> /// <param name="priceFrom">Minimumn price criterium.</param> /// <param name="priceThru">Maximumn price criterium.</param> /// <param name="sortExpression">Sort order in which to return product list.</param> /// <returns>List of found products.</returns> public IList <Product> SearchProducts(string productName, double priceFrom, double priceThru, string sortExpression) { using (ActionDataContext db = DataContextFactory.CreateContext()) { IQueryable <ProductEntity> query = db.ProductEntities; if (!string.IsNullOrEmpty(productName)) { query = query.Where(p => p.ProductName.StartsWith(productName)); } if (priceFrom != -1 && priceThru != -1) { query = query.Where(p => p.UnitPrice >= (decimal)priceFrom && p.UnitPrice <= (decimal)priceThru); } string[] sort = sortExpression.Split(' '); string sortColumn = sort[0]; string sortOrder = sort[1]; switch (sortColumn) { case "ProductId": if (sortOrder == "ASC") { query = query.OrderBy(p => p.ProductId); } else { query = query.OrderByDescending(p => p.ProductId); } break; case "ProductName": if (sortOrder == "ASC") { query = query.OrderBy(p => p.ProductName); } else { query = query.OrderByDescending(p => p.ProductName); } break; case "Weight": if (sortOrder == "ASC") { query = query.OrderBy(p => p.Weight); } else { query = query.OrderByDescending(p => p.Weight); } break; case "UnitPrice": if (sortOrder == "ASC") { query = query.OrderBy(p => p.UnitPrice); } else { query = query.OrderByDescending(p => p.UnitPrice); } break; } return(query.Select(p => Mapper.ToBusinessObject(p)).ToList()); } }