public IQueryable <Customer> SearchCustomers( SearchCustomerOptions options) { if (options == null) { return(null); } var query = context_ .Set <Customer>() .AsQueryable(); if (!string.IsNullOrWhiteSpace(options.VatNumber)) { query = query.Where(c => c.VatNumber == options.VatNumber); } if (!string.IsNullOrWhiteSpace(options.Email)) { query = query.Where(c => c.Email == options.Email); } if (options.CustomerId != null) { query = query.Where(c => c.Id == options.CustomerId.Value); } return(query.Take(500)); }
public Order CreateOrder(int customerId, ICollection <string> productIds) { if (customerId <= 0) { return(null); } if (productIds == null || productIds.Count == 0) { return(null); } var customer = customers_.SearchCustomers( new SearchCustomerOptions() { CustomerId = customerId }) .Where(c => c.IsActive) .SingleOrDefault(); if (customer == null) { return(null); } var products = context_ .Set <Product>() .Where(p => productIds.Contains(p.Id)) .ToList(); if (products.Count != productIds.Count) { return(null); } var order = new Order() { Customer = customer }; foreach (var p in products) { order.Products.Add( new OrderProduct() { ProductId = p.Id }); } context_.Add(order); try { context_.SaveChanges(); } catch (Exception ex) { return(null); } return(order); }