/// <summary> /// This function will update the tag as deleted in DB and wll de-associate modules /// </summary> /// <param name="tagId"></param> /// <param name="userId"></param> /// <returns></returns> public bool DeleteTag(int tagId, int userId) { Tag tag = new Tag(); tag = tagRepository.SingleOrDefault(x => x.TagId == tagId); if (tag != null) { tag.RecordDeleted = true; tag.ModifiedBy = userId; tag.ModifiedDate = DateTime.Now; //Delete tag associated Accounts accountTagRepository.Delete(x => x.TagId == tag.TagId); //Delete tag associated Contacts contactTagRepository.Delete(x => x.TagId == tag.TagId); //Delete tag associatd with lead leadTagRepository.Delete(x => x.TagId == tag.TagId); //update tag in db as deleted tagRepository.Update(tag); return(true); } else { return(false); } }
public async Task <IActionResult> UpdateTag(int contactId, int tagId, DetailInfoForUpdateDto detailInfoForUpdateDto) { var tag = await _tagsRepo.GetTag(contactId, tagId); if ((await _contactTagRepo.GetNumberOfContactsForTag(tagId)) == 1) { tag.TagName = detailInfoForUpdateDto.tagName; if (await _tagsRepo.SaveAll()) { return(NoContent()); } } else { _contactTagRepo.Delete(new ContactTag { TagId = tagId, ContactId = contactId }); var tagIdFromRepo = await _contactTagRepo.getTagId(detailInfoForUpdateDto.tagName); if (tagIdFromRepo >= 0) { _contactTagRepo.Update(new ContactTag { ContactId = contactId, TagId = tagIdFromRepo }); } else { await CreateTag(contactId, detailInfoForUpdateDto); } await _tagsRepo.SaveAll(); await _contactTagRepo.SaveAll(); return(NoContent()); } throw new Exception($"Updating Tag with {tagId} failed on save"); }
public void UpdateContact(ContactModel contactModel) { Contact contact = contactRepository.SingleOrDefault(u => u.ContactId == contactModel.ContactId && u.RecordDeleted == false); if (contact != null) { contactModel.ModifiedDate = DateTime.UtcNow; AutoMapper.Mapper.Map(contactModel, contact); if (contact.AddressId == null || contact.AddressId == 0) { contact.Address = null; } else if (contact.Address.CountryId == 0) { contact.Address.CountryId = null; } contactRepository.Update(contact); List <int> listAllExistingTagIds = new List <int>(); List <int> listNewTagIds = new List <int>(); List <int> listContactAssociatedTagIds = new List <int>(); List <int> listDeleteTagIds = new List <int>(); listContactAssociatedTagIds = contact.ContactTags.Select(x => x.TagId).ToList(); if (!string.IsNullOrEmpty(contactModel.ContactTagIds)) { string[] arrTagIds = contactModel.ContactTagIds.Split(','); int tagId = 0; for (int i = 0; i < arrTagIds.Length; i++) { tagId = arrTagIds[i].Decrypt(); if (listContactAssociatedTagIds.Where(x => x == tagId).Count() == 0) { listNewTagIds.Add(tagId); } else { listAllExistingTagIds.Add(tagId); } } } if (!string.IsNullOrEmpty(contactModel.NewTagNames)) { List <int> lisNewAddedTagIds = new List <int>(); lisNewAddedTagIds = AddTagsToSystem(contactModel.NewTagNames, contactModel.CompanyId, contactModel.CreatedBy); if (lisNewAddedTagIds.Count > 0) { listNewTagIds = listNewTagIds.Concat(lisNewAddedTagIds).ToList(); } } for (int i = 0; i < listContactAssociatedTagIds.Count; i++) { if (listAllExistingTagIds.Where(x => x == listContactAssociatedTagIds[i]).Count() == 0) { listDeleteTagIds.Add(listContactAssociatedTagIds[i]); } } //Associate Tagids to contact if (listNewTagIds.Count > 0) { ContactTag contactTag; for (int i = 0; i < listNewTagIds.Count; i++) { contactTag = new ContactTag(); contactTag.ContactId = contact.ContactId; contactTag.TagId = listNewTagIds[i]; contactTagRepository.Insert(contactTag); } } if (listDeleteTagIds.Count > 0) { contactTagRepository.Delete(x => x.ContactId == contact.ContactId && listDeleteTagIds.Contains(x.TagId)); } } }