private void UpdateTagsChildCollection(Contact contact, Contact contactFromDb)
 {
     var tagsToDelete = (from tag in contactFromDb.Tags
                         let item = contact.Tags.SingleOrDefault(i => i.Id == tag.Id)
                         where item == null
                         select tag).ToList();
     if (tagsToDelete.Any())
     {
         foreach (var tag in tagsToDelete)
         {
             db.Entry(tag).State = EntityState.Deleted;
         }
     }
     foreach (var tag in contact.Tags)
     {
         if (tag.Id > 0)
         {
             var tagInDb = contactFromDb.Tags.Single(e => e.Id == tag.Id);
             db.Entry(tagInDb).CurrentValues.SetValues(tag);
             db.Entry(tagInDb).State = EntityState.Modified;
         }
         else
         {
             tag.ContactId = contact.Id;
             db.Tags.Add(tag);
         }
     }
 }
Example #2
0
 public void Save(Contact contact)
 {
     if (contact.Id == Guid.Empty)
         contact.Id = Guid.NewGuid();
     if (!_contactStore.Contains(contact))
         _contactStore.Add(contact);
     Serialize();
 }
        public void UpdateContact(Contact contact)
        {

            var contactFromDb = db.Contacts.Where(x => x.Id == contact.Id).Include("Emails").Include("Tags").Include("Telephones").FirstOrDefault();

            contactFromDb.Address = contact.Address;
            contactFromDb.Name = contact.Name;
            contactFromDb.Surname = contact.Surname;
            UpdateEmailChildCollection(contact, contactFromDb);
            UpdateTelephonesChildCollection(contact, contactFromDb);
            UpdateTagsChildCollection(contact, contactFromDb);

            contactFromDb.DateUpdated = contact.DateUpdated;
            ///   contactFromDb.Tags = contact.Tags;

            db.SaveChanges();
        }
        public void Add(Contact contact)
        {
            List<Contact> items = GetAll();
            int max = items.Count == 0 ? 0 : items.Max(i => i.Id);

            int newId = ++max;

            var xElement = _xmlDoc.Element("Contacts");
            if (xElement != null)
                xElement.Add(
                    new XElement("Contact", new XElement("Id", newId.ToString(CultureInfo.InvariantCulture)),
            new XElement("MembershipDate", contact.MembershipDate),
                             new XElement("FirstName", contact.FirstName),
                              new XElement("LastName", contact.LastName),
                              new XElement("EmailAddress", contact.EmailAddress),
                              new XElement("Phone", contact.Phone),
                              new XElement("Address1",
                                            new XElement("Street1", contact.Address1.Street1),
                                            new XElement("Street2", contact.Address1.Street2),
                                            new XElement("City", contact.Address1.City),
                                            new XElement("State", contact.Address1.State),
                                            new XElement("Zip", contact.Address1.Zip)
                                            ),
                            new XElement("DOB", contact.DOB),
                            new XElement("BusinessPhone", contact.BusinessPhone),
                            new XElement("Fax", contact.Fax),
                            new XElement("AHAMemberNumber", contact.AHAMemberNumber),
                            new XElement("EntryAdded", DateTime.UtcNow),
                            new XElement("EntryUpdated", DateTime.UtcNow),
                            new XElement("IsActive", contact.IsActive)

                            

                        ));


            _xmlDoc.Save(EntryFileFullName);
        }
 public void AddContact(Contact contactToAdd)
 {
     db.Contacts.Add(contactToAdd);
     db.SaveChanges();
 }
 private void UpdateEmailChildCollection(Contact contact, Contact contactFromDb)
 {
     var emailsToDelete = (from email in contactFromDb.Emails
                           let item = contact.Emails.SingleOrDefault(i => i.Id == email.Id)
                           where item == null
                           select email).ToList();
     if (emailsToDelete.Any())
     {
         foreach (var email in emailsToDelete)
         {
             db.Entry(email).State = EntityState.Deleted;
         }
     }
     foreach (var email in contact.Emails)
     {
         if (email.Id > 0)
         {
             var emailInDb = contactFromDb.Emails.Single(e => e.Id == email.Id);
             db.Entry(emailInDb).CurrentValues.SetValues(email);
             db.Entry(emailInDb).State = EntityState.Modified;
         }
         else
         {
             email.ContactId = contact.Id;
             db.Emails.Add(email);
         }
     }
 }
 public void UpdateContact(Contact contact)
 {
     contact.DateUpdated = DateTime.Now;
     _contactRepository.UpdateContact(contact);
 }
 public void AddContact(Contact contactToAdd)
 {
     contactToAdd.DateUpdated = DateTime.Now;
     _contactRepository.AddContact(contactToAdd);
 }
 public void Delete(Contact contact)
 {
     this.contactStore.Remove(contact);
     Serialize();
 }
Example #10
0
        private Contact LoadContactFromInput()
        {
            Contact c = new Contact();
            c.FirstName = txtFirstName.Text;
            c.LastName = txtLastName.Text;
            c.EmailAddress = txtEmail.Text;
            c.Phone = txtPhone.Text;
            c.Address1 = new Address(txtStreet.Text, string.Empty, txtCity.Text, cmbState.Text, txtZip.Text);
            c.MembershipDate = Convert.ToDateTime(dtpMembership.Text);
            c.DOB = Convert.ToDateTime(dtpDOB.Text);
            c.BusinessPhone = txtBusPhone.Text;
            c.Fax = txtFax.Text;
            c.AHAMemberNumber = txtAHANum.Text;
            c.IsActive = chkActive.Checked;
            return c;

        }
Example #11
0
 public void Add(Contact newContact)
 {
     conArray.Add(newContact);
 }
 public void Delete(Contact contact)
 {
     _xmlDoc.Root.Elements().Where(e => e.Attribute("Id").Value.Equals(contact.Id.ToString())).Select(e => e).Single().Remove();
     _xmlDoc.Save(EntryFileFullName);
 }
        public void UpdateEntry(Contact contact)
        {
            string nodeName = "Contact";
            //find original
            var xmlElement = (from item in _xmlDoc.Descendants(nodeName)
                              let xElement = item.Element("Id")
                              where xElement != null && xElement.Value == contact.Id.ToString()
                              select item).Single();
            var oldContact = new Contact(xmlElement);

            xmlElement.ReplaceWith(
                  new XElement("Contact", new XElement("Id", contact.Id),
          new XElement("MembershipDate", contact.MembershipDate),
                           new XElement("FirstName", contact.FirstName),
                            new XElement("LastName", contact.LastName),
                            new XElement("EmailAddress", contact.EmailAddress),
                            new XElement("Phone", contact.Phone),
                            new XElement("Address1",
                                          new XElement("Street1", contact.Address1.Street1),
                                          new XElement("Street2", contact.Address1.Street2),
                                          new XElement("City", contact.Address1.City),
                                          new XElement("State", contact.Address1.State),
                                          new XElement("Zip", contact.Address1.Zip)
                                          ),
                          new XElement("DOB", contact.DOB),
                          new XElement("BusinessPhone", contact.BusinessPhone),
                          new XElement("Fax", contact.Fax),
                          new XElement("AHAMemberNumber", contact.AHAMemberNumber),
                          new XElement("EntryAdded", contact.EntryAdded),
                          new XElement("EntryUpdated", DateTime.UtcNow),
                        new XElement("IsActive", contact.IsActive)

                      ));

            //update with this one.
            _xmlDoc.Save(EntryFileFullName);
        }