private static void ImportMessageIfCorrect(RootObject userMessage)
        {
            var context = new PhonebookEntities();

            string content = userMessage.Content;
            if (string.IsNullOrEmpty(content))
            {
                throw new ArgumentException("Error: {0} is required", "Content");
            }

            DateTime datetime = DateTime.Now;
            bool isValidDateTime = DateTime.TryParse(userMessage.DateTime, out datetime);
            if (!isValidDateTime)
            {
                throw new ArgumentException("Error: {0} is required or is not datetime string", "Datetime");
            }

            User recipient = context.Users
                .FirstOrDefault(u => u.Username == userMessage.Recipient);
            if (recipient == null)
            {
                throw new ArgumentException("Error: {0} is required or not such user", "Recipient");
            }

            User sender = context.Users
                .FirstOrDefault(u => u.Username == userMessage.Sender);
            if (sender == null)
            {
                throw new ArgumentException("Error: {0} is required or not such user", "Sender");
            }

            UserMessage newUserMessage = new UserMessage();
            newUserMessage.Content = content;
            newUserMessage.DateTime = datetime;
            newUserMessage.RecipientUser = recipient;
            newUserMessage.SenderUser = sender;

            context.UserMessages.Add(newUserMessage);
            context.SaveChanges();
        }
        private static void InsertNewContact(RootObject newContact, PhonebookEntities context)
        {
            Contact contact = new Contact();

            if (newContact.Name == null)
            {
                throw new ArgumentException("Error: Name is required");
            }

            contact.Name = newContact.Name;

            if (newContact.Company != null)
            {
                contact.Company = newContact.Company;
            }

            if (newContact.Notes != null)
            {
                contact.Notes = newContact.Notes;
            }

            if (newContact.Position != null)
            {
                contact.Position = newContact.Position;
            }

            if (newContact.Site != null)
            {
                contact.Url = newContact.Site;
            }

            if (newContact.Emails != null && newContact.Emails.Count > 0)
            {
                foreach (var email in newContact.Emails)
                {
                    if (!email.Contains("@"))
                    {
                        throw new ArgumentException("Not valid email!");
                    }

                    Email newEmail = new Email();
                    newEmail.EmailAddress = email;
                    contact.Emails.Add(newEmail);
                }
            }

            if (newContact.Phones != null && newContact.Phones.Count > 0)
            {
                foreach (var phone in newContact.Phones)
                {
                    Phone newPhone = new Phone();
                    newPhone.Number = phone;
                    contact.Phones.Add(newPhone);
                }
            }

            context.Contacts.Add(contact);
            context.SaveChanges();
        }