Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.85 KB | None | 0 0
  1. let c = contacts.load::<Contacts>(cx).expect("loading contacts went wrong");
  2. let ce = ContactsEmails::belonging_to(&c).load::<ContactsEmails>(cx).expect("loading emails went wrong");
  3. let cp = ContactsPhonenumbers::belonging_to(&c).load::<ContactsPhonenumbers>(cx).expect("loading numbers went wrong ");
  4.  
  5. let t = tags.load::<Tags>(cx).expect("loading all tags went wrong");
  6. let ct = ContactsTags::belonging_to(&c).load::<ContactsTags>(cx).expect("loading contacts tags went wrong");
  7. let mut tags_map: HashMap<i32, Tags> = HashMap::new();
  8. for contactTag in ct.iter() {
  9.     for tt in t.iter() {
  10.         match contactTag.ctag_tag_id {
  11.             Some(_tag_id) if _tag_id == tt.id => { tags_map.insert(contactTag.id, tt.clone()); }
  12.             _ => {}
  13.         }
  14.     }
  15. }
  16. let grouped_ct: Vec<Vec<ContactsTags>> = ct.grouped_by(&c);
  17. let grouped_ce: Vec<Vec<ContactsEmails>> = ce.grouped_by(&c);
  18. let grouped_cp: Vec<Vec<ContactsPhonenumbers>> = cp.grouped_by(&c);
  19. let c_and_ce: Vec<(Contacts, Vec<ContactsEmails>)> = c.into_iter().zip(grouped_ce).collect();
  20. let c_and_ce_and_cp: Vec<((Contacts, Vec<ContactsEmails>), Vec<ContactsPhonenumbers>)> = c_and_ce.into_iter().zip(grouped_cp).collect();
  21. let c_and_ce_and_cp_and_ct: Vec<(((Contacts, Vec<ContactsEmails>), Vec<ContactsPhonenumbers>), Vec<ContactsTags>)> = c_and_ce_and_cp.into_iter().zip(grouped_ct).collect();
  22.  
  23. let mut c_out = vec![];
  24. for (((contact, email_list), phone_list), contacts_tags) in c_and_ce_and_cp_and_ct {
  25.  
  26.     let mut c_tags = vec![];
  27.     for ct in contacts_tags {
  28.         if let Some(tt) = tags_map.remove(&ct.id) {
  29.             c_tags.push(tt);
  30.         }
  31.     }
  32.  
  33.     c_out.push(ApiContact{
  34.         id: contact.id,
  35.         firstname: contact.firstname,
  36.         lastname: contact.lastname,
  37.         emails: Some(email_list),
  38.         phone: Some(phone_list),
  39.         tags: Some(c_tags),
  40.     })
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement