Example #1
0
 public static void Main()
 {
     var context = new PhonebookEntities();
     var parsedContacts = JsonConvert.DeserializeObject<IList<ContactDTO>>(File.ReadAllText(@"..\..\..\Import\contacts.json"));
     
     
 }
        public static void Main()
        {
            Database.SetInitializer(
            new MigrateDatabaseToLatestVersion<PhonebookEntities, PhoneBookConfiguration>());

            var context = new PhonebookEntities();
            var contactsCount = context.Contacts.Count();

            var contacts = context.Contacts
                .Select(c => new
                {
                    c.Name,
                    c.Phones,
                    c.Emails
                });

            foreach (var contact in contacts)
            {
                Console.WriteLine(contact.Name);
                Console.WriteLine("- Phones:");
                foreach (var phone in contact.Phones)
                {
                    Console.WriteLine("-- {0}", phone.Number);
                }

                Console.WriteLine("- Emails:");
                foreach (var email in contact.Emails)
                {
                    Console.WriteLine("-- {0}", email.EmailAddress);
                }
            }
        }
        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();
        }
        public static void Main()
        {
            var context = new PhonebookEntities();
            string jsonData = File.ReadAllText("../../contacts.json");
            RootObject[] newContacts = JsonConvert.DeserializeObject<RootObject[]>(jsonData);

            foreach (var newContact in newContacts)
            {
                try
                {
                    InsertNewContact(newContact, context);
                    Console.WriteLine("Contact {0} imported", newContact.Name);
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message + ", " + ae.StackTrace);
                }
            }
        }
        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();
        }
        public static void Main()
        {
            Database.SetInitializer(
                new MigrateDatabaseToLatestVersion<PhonebookEntities, PhonebookConfiguration>());

            var context = new PhonebookEntities();
            var usersCount = context.Users.Count();

            var channelsWithMessages = context.Channels
                .Select(ch => new
                {
                    ch.Name,
                    Messages = ch.ChannelMessages
                        .Select(m => new
                        {
                            m.Content,
                            m.DateTime,
                            m.User.Username
                        })
                });

            foreach (var channelsWithMessage in channelsWithMessages)
            {
                Console.WriteLine(channelsWithMessage.Name);
                Console.WriteLine("-- Messages: --");
                foreach (var message in channelsWithMessage.Messages)
                {
                    Console.WriteLine(
                        "Content: {0}, DateTime: {1}, User: {2}",
                        message.Content,
                        message.DateTime.ToString("dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture),
                        message.Username);
                }

                Console.WriteLine();
            }
        }