ContactLookups.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. Columns<Quote> ILookupDefinition<Contact, Quote>.DefineFilterColumns()
  26. => new Columns<Quote>(x => x.Customer.ID).Add(x => x.Customer.Deleted);
  27. public override Columns<Contact> DefineColumns()
  28. {
  29. return new Columns<Contact>(
  30. x => x.ID,
  31. x => x.Name,
  32. x => x.Email
  33. );
  34. }
  35. public override Filter<Contact>? DefineFilter()
  36. {
  37. return null;
  38. }
  39. public override SortOrder<Contact> DefineSortOrder()
  40. {
  41. return new SortOrder<Contact>(x => x.Name);
  42. }
  43. }
  44. }