Beispiel #1
0
        public static void Initialize(DataContext context)
        {
            context.Database.EnsureCreated();

            if (context.Contacts.Any())
            {
                return;
            }

            var contacts = new Contact[]
            {
                new Contact {
                    firstName = "kontakt1", lastName = "adaadadad"
                },
                new Contact {
                    firstName = "adwdadadaw", lastName = "22222"
                },
                new Contact {
                    firstName = "bbbbbbbbbb", lastName = "bbbbbbbbbbb"
                },
                new Contact {
                    firstName = "cccccccccc", lastName = "ccccc"
                },
                new Contact {
                    firstName = "ddd", lastName = "ddddddd"
                },
                new Contact {
                    firstName = "eeeeeee", lastName = "eeeeeeeee"
                },
                new Contact {
                    firstName = "gffffffffff", lastName = "fffffffffn"
                },
                new Contact {
                    firstName = "hhhhhh", lastName = "hhhhhhh"
                }
            };

            foreach (Contact c in contacts)
            {
                context.Contacts.Add(c);
            }
            context.SaveChanges();

            var emails = new ContactEmail[]
            {
                new ContactEmail {
                    contactID = 1, email = "*****@*****.**"
                },
                new ContactEmail {
                    contactID = 1, email = "*****@*****.**"
                },
                new ContactEmail {
                    contactID = 1, email = "*****@*****.**"
                },
                new ContactEmail {
                    contactID = 1, email = "*****@*****.**"
                },
                new ContactEmail {
                    contactID = 1, email = "*****@*****.**"
                },
                new ContactEmail {
                    contactID = 1, email = "*****@*****.**"
                },
                new ContactEmail {
                    contactID = 2, email = "*****@*****.**"
                },
                new ContactEmail {
                    contactID = 2, email = "*****@*****.**"
                },
                new ContactEmail {
                    contactID = 2, email = "*****@*****.**"
                },
                new ContactEmail {
                    contactID = 3, email = "*****@*****.**"
                },
                new ContactEmail {
                    contactID = 4, email = "*****@*****.**"
                },
                new ContactEmail {
                    contactID = 4, email = "*****@*****.**"
                },
                new ContactEmail {
                    contactID = 5, email = "*****@*****.**"
                },
                new ContactEmail {
                    contactID = 6, email = "*****@*****.**"
                },
                new ContactEmail {
                    contactID = 7, email = "*****@*****.**"
                },
            };

            foreach (ContactEmail e in emails)
            {
                context.ContactEmails.Add(e);
            }
            context.SaveChanges();

            var phoneNums = new ContactPhoneNum[]
            {
                new ContactPhoneNum {
                    contactID = 1, phoneNum = "11111111"
                },
                new ContactPhoneNum {
                    contactID = 1, phoneNum = "1111111111"
                },
                new ContactPhoneNum {
                    contactID = 1, phoneNum = "11111111111111111"
                },
                new ContactPhoneNum {
                    contactID = 2, phoneNum = "2222222222222222"
                },
                new ContactPhoneNum {
                    contactID = 2, phoneNum = "222222222222222222"
                },
                new ContactPhoneNum {
                    contactID = 2, phoneNum = "22222222222222222"
                },
                new ContactPhoneNum {
                    contactID = 3, phoneNum = "333333333333333333333"
                },
                new ContactPhoneNum {
                    contactID = 4, phoneNum = "44444444444444444"
                },
                new ContactPhoneNum {
                    contactID = 4, phoneNum = "44444444444444444444"
                },
                new ContactPhoneNum {
                    contactID = 5, phoneNum = "555555555555555"
                },
                new ContactPhoneNum {
                    contactID = 6, phoneNum = "6666666"
                },
                new ContactPhoneNum {
                    contactID = 7, phoneNum = "77777777"
                },
                new ContactPhoneNum {
                    contactID = 7, phoneNum = "888888888"
                },
            };

            foreach (ContactPhoneNum n in phoneNums)
            {
                context.ContactPhoneNums.Add(n);
            }
            context.SaveChanges();
        }
Beispiel #2
0
        public async Task PutAsync(int id, [FromBody] object value)
        {
            var existingContact = await _dataContext.Contacts.Include(e => e.emails)
                                  .Include(n => n.phoneNumbers)
                                  .FirstOrDefaultAsync(x => x.id == id);

            var updatedContact = JsonConvert.DeserializeObject <Contact>(value.ToString());

            if (existingContact != null)
            {
                // Update Contact data
                _dataContext.Entry(existingContact).CurrentValues.SetValues(updatedContact);

                // Delete Emails
                foreach (var existingEmail in existingContact.emails.ToList())
                {
                    if (!updatedContact.emails.Any(c => c.id == existingEmail.id)) //check all incoming emails if they match with one in database
                    {
                        _dataContext.ContactEmails.Remove(existingEmail);          //delete email from database if no id match
                    }
                }

                // Update and Insert Emails
                foreach (var updatedEmail in updatedContact.emails.ToList())
                {
                    var existingEmail = existingContact.emails   //get email from database that matches email from update
                                        .Where(c => c.id == updatedEmail.id)
                                        .SingleOrDefault();

                    if (existingEmail != null && existingEmail.id > 0)
                    {
                        // Update existing Email
                        _dataContext.Entry(existingEmail).CurrentValues.SetValues(updatedEmail);
                    }
                    else if (updatedEmail.email.Length > 0)
                    {
                        // Insert new email
                        var newEmail = new ContactEmail
                        {
                            email = updatedEmail.email,
                        };
                        existingContact.emails.Add(newEmail);
                    }
                }

                // Delete Phone Numbers
                foreach (var existingPhoneNum in existingContact.phoneNumbers.ToList())
                {
                    if (!updatedContact.phoneNumbers.Any(c => c.id == existingPhoneNum.id))
                    {
                        _dataContext.ContactPhoneNums.Remove(existingPhoneNum);
                    }
                }
                // Update and Insert phone numbers
                foreach (var updatedPhoneNum in updatedContact.phoneNumbers)
                {
                    var existingPhoneNum = existingContact.phoneNumbers
                                           .Where(c => c.id == updatedPhoneNum.id)
                                           .SingleOrDefault();

                    if (existingPhoneNum != null && existingPhoneNum.id > 0)
                    {
                        // Update phone number
                        _dataContext.Entry(existingPhoneNum).CurrentValues.SetValues(updatedPhoneNum);
                    }
                    else
                    {
                        // Insert phone number
                        var newPhoneNum = new ContactPhoneNum
                        {
                            phoneNum = updatedPhoneNum.phoneNum
                        };
                        existingContact.phoneNumbers.Add(newPhoneNum);
                    }
                }
                _dataContext.SaveChanges();
            }
        }