Пример #1
0
        public void DeleteUserContact(Models.Contact contact, Guid userId)
        {
            using (ContactEntities db = new ContactEntities())
            {
                IQueryable <User> userQuery = db.Users.Where(users => users.id == userId);
                User userData = userQuery.First();

                IQueryable <ContactDAL.Contact> contactQuery = db.Contacts.Where(contactLocation => contactLocation.userId == userData.id && contactLocation.id == contact.Id);
                ContactDAL.Contact contactData = contactQuery.First();

                db.Addresses.RemoveRange(contactData.Addresses);
                db.Emails.RemoveRange(contactData.Emails);
                db.Images.RemoveRange(contactData.Images);
                db.PhoneNumbers.RemoveRange(contactData.PhoneNumbers);
                db.Contacts.Remove(contactData);
                db.SaveChanges();
            }
        }
Пример #2
0
        public Models.Contact UpsertUserContact(Models.Contact contact, Guid userId)
        {
            using (ContactEntities db = new ContactEntities())
            {
                IQueryable <User> userQuery = db.Users.Where(user => user.id == userId);
                User userData = userQuery.First();

                if (contact.Id == Guid.Empty)
                {
                    ContactDAL.Contact newContact = new ContactDAL.Contact()
                    {
                        User                = userData,
                        userId              = userData.id,
                        contactFirstName    = contact.FirstName,
                        contactLastName     = contact.LastName,
                        contactJobTitle     = contact.JobTitle,
                        contactOrganization = contact.Organization
                    };
                    Email newEmail = new Email()
                    {
                        Contact        = newContact,
                        primaryEmail   = contact.PrimaryEmail,
                        secondaryEmail = contact.SecondaryEmail
                    };
                    Image newImage = new Image()
                    {
                        Contact      = newContact,
                        contactImage = contact.Image
                    };
                    PhoneNumber newPhoneNumber = new PhoneNumber()
                    {
                        Contact      = newContact,
                        cellNumber   = contact.CellPhone,
                        homeNumber   = contact.HomePhone,
                        officeNumber = contact.OfficePhone
                    };
                    ContactDAL.Address newAddress = new ContactDAL.Address()
                    {
                        Contact            = newContact,
                        addressLine1       = contact.Address.Line1,
                        addressLine2       = contact.Address.Line2,
                        addressCity        = contact.Address.City,
                        addressCountry     = contact.Address.Country,
                        addressStateCounty = contact.Address.State,
                        addressZip         = contact.Address.Zip
                    };

                    newContact.Emails.Add(newEmail);
                    newContact.Images.Add(newImage);
                    newContact.PhoneNumbers.Add(newPhoneNumber);
                    newContact.Addresses.Add(newAddress);

                    db.Contacts.Add(newContact);
                    db.Emails.Add(newEmail);
                    db.Images.Add(newImage);
                    db.PhoneNumbers.Add(newPhoneNumber);
                    db.Addresses.Add(newAddress);

                    db.SaveChanges();

                    contact.Id = Guid.Parse(newContact.id.ToString());
                }
                else
                {
                    IQueryable <ContactDAL.Contact> contactQuery = db.Contacts.Where(contactLocatingData => contactLocatingData.userId == userData.id && contactLocatingData.id == contact.Id);
                    ContactDAL.Contact contactData = contactQuery.First();

                    contactData.contactFirstName    = contact.FirstName;
                    contactData.contactLastName     = contact.LastName;
                    contactData.contactJobTitle     = contact.JobTitle;
                    contactData.contactOrganization = contact.Organization;
                    contactData.Emails.First().primaryEmail          = contact.PrimaryEmail;
                    contactData.Emails.First().secondaryEmail        = contact.SecondaryEmail;
                    contactData.Images.First().contactImage          = contact.Image;
                    contactData.PhoneNumbers.First().cellNumber      = contact.CellPhone;
                    contactData.PhoneNumbers.First().homeNumber      = contact.HomePhone;
                    contactData.PhoneNumbers.First().officeNumber    = contact.OfficePhone;
                    contactData.Addresses.First().addressLine1       = contact.Address.Line1;
                    contactData.Addresses.First().addressLine2       = contact.Address.Line2;
                    contactData.Addresses.First().addressCity        = contact.Address.City;
                    contactData.Addresses.First().addressCountry     = contact.Address.Country;
                    contactData.Addresses.First().addressStateCounty = contact.Address.State;
                    contactData.Addresses.First().addressZip         = contact.Address.Zip;

                    db.SaveChanges();
                }
            }
            return(contact);
        }