public void AddContact(ContactDTO contactDto, long userID) { var contact = new Contact(); var nameValid = _contactService.emailValidation(contactDto.ContactEmail); foreach (var item in nameValid) { if (nameValid != null && item.UserID == userID) throw new NotFoundException(ErrorCodes.RepeatedContactEmail); } contact.ContactEmail = contactDto.ContactEmail; contact.ContactName = contactDto.ContactName; contact.ContactMobileNum = contactDto.ContactMobileNum; contact.UserID = userID; _contactService.Insert(contact); var group = new Group(); var NewGroup_brGroupContact = new Br_GroupContact(); if (contactDto.GroupName != null && contactDto.GroupName != "") { var groupNameValid = _groupService.nameValidationGroup(contactDto.GroupName, userID); if (groupNameValid != null) throw new NotFoundException(ErrorCodes.RepeatedGroupName); group.GroupName = contactDto.GroupName; group.UserID = userID; _groupService.Insert(group); NewGroup_brGroupContact.ContactID = contactDto.ContactID; NewGroup_brGroupContact.GroupID = group.GroupID; NewGroup_brGroupContact.UserID = userID; _brGroupContactService.Insert(NewGroup_brGroupContact); } var listGroups = new List<Br_GroupContact>(); if (contactDto.ContactGroups.Count != 0) { foreach (var item in contactDto.ContactGroups) { var brGroupContact = new Br_GroupContact(); brGroupContact.ContactID = contactDto.ContactID; brGroupContact.GroupID = item.GroupID; brGroupContact.UserID = userID; listGroups.Add(brGroupContact); } _brGroupContactService.InsertRange(listGroups); } SaveChanges(); }
public void UpdateBrGroupContact(long userID, List<GroupDTO> contactGroups, long contactID) { //Find all already joinedGroups var contactsInThisGroup = _brGroupContactService.GetGroupsByContact(userID, contactID); var alreadyJoinedGroupsIDs = new List<long>(); foreach (var item in contactsInThisGroup) { var itemID = item.GroupID; alreadyJoinedGroupsIDs.Add(itemID); } var newJoinedGroupsIDs = new List<long>(); foreach (var item in contactGroups) { var itemID = item.GroupID; newJoinedGroupsIDs.Add(itemID); } var AddGroupsIDs = new List<long>(); for (int i = 0; i < newJoinedGroupsIDs.Count; i++) { var containedThisGroup = alreadyJoinedGroupsIDs.Contains(newJoinedGroupsIDs[i]); if (containedThisGroup == false) { var addID = newJoinedGroupsIDs[i]; AddGroupsIDs.Add(addID); } } var DeleteGroupsIDs = new List<long>(); for (int i = 0; i < alreadyJoinedGroupsIDs.Count; i++) { var containedThisGroup = newJoinedGroupsIDs.Contains(alreadyJoinedGroupsIDs[i]); if (containedThisGroup == false) { var deleteID = alreadyJoinedGroupsIDs[i]; DeleteGroupsIDs.Add(deleteID); } } //insertNewGroups fro this Contact var listGroups = new List<Br_GroupContact>(); if (AddGroupsIDs.Count != 0) { foreach (var item in AddGroupsIDs) { var brGroupContact = new Br_GroupContact(); brGroupContact.GroupID = item; brGroupContact.ContactID = contactID; brGroupContact.UserID = userID; listGroups.Add(brGroupContact); } _brGroupContactService.InsertRange(listGroups); } //Delete Groups from this user if (DeleteGroupsIDs.Count != 0) { foreach (var item in DeleteGroupsIDs) { var deleteUpdateGroup = _brGroupContactService.FindbyGroupIDAndContactID(userID, contactID, item); deleteUpdateGroup.ContactInThisGroupIsDeleted = true; _brGroupContactService.Update(deleteUpdateGroup); } } }