ContactLookups.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Linq;
  3. using InABox.Clients;
  4. using InABox.Core;
  5. namespace Comal.Classes
  6. {
  7. public class ContactLookups : EntityLookup<Contact>, ILookupDefinition<Contact, Quote>
  8. {
  9. public Filter<Contact> DefineFilter(Quote[] items)
  10. {
  11. if (items == null || !items.Any())
  12. return new Filter<Contact>(x => x.ID).IsEqualTo(CoreUtils.FullGuid);
  13. var customer = items.First().Customer;
  14. if (!customer.IsValid())
  15. return new Filter<Contact>(x => x.ID).IsEqualTo(CoreUtils.FullGuid);
  16. var contacts = new Client<CustomerContact>().Query(
  17. new Filter<CustomerContact>(x => x.Customer.ID).IsEqualTo(customer.ID),
  18. new Columns<CustomerContact>(x => x.Contact.ID)
  19. );
  20. var ids = contacts.Rows.Select(x => x.Get<CustomerContact, Guid>(c => c.Contact.ID));
  21. if (!ids.Any())
  22. return new Filter<Contact>(x => x.ID).IsEqualTo(CoreUtils.FullGuid);
  23. return new Filter<Contact>(x => x.ID).InList(ids.ToArray());
  24. }
  25. public override Columns<Contact> DefineColumns()
  26. {
  27. return new Columns<Contact>(
  28. x => x.ID,
  29. x => x.Name,
  30. x => x.Email
  31. );
  32. }
  33. public override Filter<Contact> DefineFilter()
  34. {
  35. return null;
  36. }
  37. public override SortOrder<Contact> DefineSortOrder()
  38. {
  39. return new SortOrder<Contact>(x => x.Name);
  40. }
  41. }
  42. }