Exemple #1
0
        public static void Main(string[] args)
        {
            //display items
            var db       = new DataAccess.Models.CSharpContext();
            var keywords = db.Keywords;

            foreach (var k in keywords)
            {
                System.Console.WriteLine("{0} - {1}", k.Parent.Name, k.Name);
            }
            System.Console.ReadLine();
            foreach (var c in db.Contacts)
            {
                System.Console.WriteLine("{0} {1} - {2}", c.FirstName, c.LastName, c.Keyword.Name);
                foreach (var item in c.ContactMethods)
                {
                    System.Console.WriteLine("\t -- {0} {1} ", item.Keyword.Name, item.Value);
                }
            }
            //add item
            System.Console.WriteLine("Enter a contact in the following format (FirstName LastName Phone)");
            var results = System.Console.ReadLine();
            var values  = results.Split();
            var contact = new DataAccess.Models.Contact {
                FirstName = values[0], LastName = values[1], KeywordsId = 5
            };

            db.Contacts.Add(contact);
            contact.ContactMethods.Add(new DataAccess.Models.ContactMethod {
                KeywordID = 7, Value = values[2]
            });
            db.SaveChanges();

            /*
             * System.Console.WriteLine("Hello World");
             * //System.Console.ReadLine();
             * System.Console.WriteLine("What is your username");
             * var response = System.Console.ReadLine();
             *
             * System.Console.WriteLine("Here's your profile info");
             * System.Console.Write(GetProfile(response)+"\n");
             *
             * var db = new DataAccess.Test1Entities();
             * foreach (var item in db.Contacts) {
             *  System.Console.WriteLine(item.FirstName + " " + item.LastName);
             * }
             * System.Console.ReadLine();
             */
        }
        public void CreateContact(CreateEditContact contact, string uploadPathBase)
        {
            using (var context = new AddressBookContext(nameOrConnectionString))
            {
                if (contact.File != null && contact.File.ContentLength > 0)
                {
                    contact.Image = Guid.NewGuid() + Path.GetExtension(contact.File.FileName);                    
                    contact.File.SaveAs(Path.Combine(uploadPathBase, contact.Image));
                }

                var mapped = new DataAccess.Models.Contact
                {
                    Id = contact.Id,
                    FullName = contact.FullName,
                    LastName = contact.LastName,
                    Title = contact.Title,
                    Email = contact.Email,
                    OfficeId = contact.OfficeId,
                    DepartmentId = contact.DepartmentId,
                    OrganisationId = contact.OrganisationId,
                    Image = contact.Image,
                    WebLinks = contact.WebLinks
                        .Where(w => !string.IsNullOrWhiteSpace(w.Url))
                        .Select(w => new WebLink {  Id = w.Id, ContactId = w.ContactId, WebLinkTypeId = w.WebLinkTypeId})
                        .ToList(),
                    PhoneNumbers = contact.PhoneNumbers
                        .Where(p => !string.IsNullOrWhiteSpace(p.PhoneNumber))
                        .Select(p => new ContactPhoneNumber {  Id = p.Id, ContactId = p.ContactId, PhoneNumberTypeId = p.PhoneNumberTypeId})
                        .ToList()
                };

                context.Contacts.Add(mapped);
                context.SaveChanges();

                // Update the lucene index
                var luceneUpdate = new CreateUpdateLuceneContact(mapped);
                luceneUpdate.Execute();
            }
        }
        public void UpdateContact(CreateEditContact contact, string uploadPath)
        {
            var mappedContact = new DataAccess.Models.Contact
            {
                Id = contact.Id,
                FullName = contact.FullName,
                LastName = contact.LastName,
                Email = contact.Email,
                Title = contact.Title,
                DepartmentId = contact.DepartmentId,
                OfficeId = contact.OfficeId,
                OrganisationId = contact.OrganisationId,
                Image = contact.Image,
                PhoneNumbers = contact.PhoneNumbers
                    .Where(p => !string.IsNullOrWhiteSpace(p.PhoneNumber))
                    .Select(p => new ContactPhoneNumber {  Id = p.Id, ContactId = p.ContactId, PhoneNumber = p.PhoneNumber, PhoneNumberTypeId = p.PhoneNumberTypeId})
                    .ToList(),
                WebLinks = contact.WebLinks
                    .Where(w => !string.IsNullOrWhiteSpace(w.Url))
                    .Select(w => new WebLink {  Id = w.Id, ContactId = w.ContactId, Url = w.Url, WebLinkTypeId = w.WebLinkTypeId})
                    .ToList()
            };

            using (var context = new AddressBookContext(nameOrConnectionString))
            {
                var originalContact = context
                    .Contacts
                    .Include(c => c.PhoneNumbers)
                    .Include(c => c.WebLinks)
                    .FirstOrDefault(c => c.Id == mappedContact.Id);

                if (originalContact == null)
                    throw new Exception($"No contact with id {mappedContact.Id} exists");

                var originalPhoneNumbers = originalContact
                    .PhoneNumbers
                    .ToList();

                var deletedPhoneNumbers = originalPhoneNumbers
                    .Except(mappedContact.PhoneNumbers, c => c.Id)
                    .ToList();

                var addedPhoneNumbers = mappedContact
                    .PhoneNumbers
                    .Where(p => p.Id == 0)
                    .ToList();

                var modifiedPhoneNumbers = mappedContact
                    .PhoneNumbers
                    .Except(deletedPhoneNumbers, c => c.Id)
                    .Except(addedPhoneNumbers, c => c.Id)
                    .ToList();

                foreach (var delete in deletedPhoneNumbers)
                {
                    context.Entry(delete).State = EntityState.Deleted;
                }

                foreach (var added in addedPhoneNumbers)
                {
                    context.Entry(added).State = EntityState.Added;
                }

                // Get existing items to update
                foreach (var modified in modifiedPhoneNumbers)
                {
                    var existing = context.PhoneNumbers.Find(modified.Id);
                    if (existing == null)
                        continue;

                    context.Entry(existing).CurrentValues.SetValues(modified);
                }

                var originalWebLinks = originalContact
                    .WebLinks
                    .ToList();

                var deletedWebLinks = originalWebLinks
                    .Except(mappedContact.WebLinks, c => c.Id)
                    .ToList();

                var addedWebLinks = mappedContact.WebLinks
                    .Where(c => c.Id == 0)
                    .ToList();

                var modifiedWebLinks = mappedContact.WebLinks
                    .Except(deletedWebLinks, c => c.Id)
                    .Except(addedWebLinks, c => c.Id)
                    .ToList();

                foreach (var delete in deletedWebLinks)
                {
                    context.Entry(delete).State = EntityState.Deleted;
                }

                foreach (var added in addedWebLinks)
                {
                    context.Entry(added).State = EntityState.Added;
                }

                // Get existing items to update
                foreach (var modified in modifiedWebLinks)
                {
                    var existing = context.WebLinks.Find(modified.Id);
                    if (existing == null)
                        continue;

                    context.Entry(existing).CurrentValues.SetValues(modified);
                }

                // If we have an image attached
                if (contact.File != null && contact.File.ContentLength > 0)
                {
                    var uploadedImageName = Guid.NewGuid() + Path.GetExtension(contact.File.FileName);

                    mappedContact.Image = uploadedImageName;

                    contact.File.SaveAs(Path.Combine(uploadPath, uploadedImageName));

                    // Delete the original
                    DeleteImage(originalContact.Image, uploadPath);
                }

                // If no image is attached it means it should be deleted (if possible)
                if (string.IsNullOrWhiteSpace(contact.Image))
                {
                    // Delete the original
                    DeleteImage(originalContact.Image, uploadPath);
                }

                // Update the other properties with the new values
                context.Entry(originalContact).CurrentValues.SetValues(mappedContact);

                context.SaveChanges();

                // Update the lucene index
                var luceneUpdate = new CreateUpdateLuceneContact(mappedContact);
                luceneUpdate.Execute();
            }
        }
Exemple #4
0
        public static void Main(string[] args)
        {
            //display items
            var db = new DataAccess.Models.CSharpContext();
            var keywords = db.Keywords;
            foreach (var k in keywords) {
                System.Console.WriteLine("{0} - {1}", k.Parent.Name, k.Name);
            }
            System.Console.ReadLine();
            foreach (var c in db.Contacts) {
                System.Console.WriteLine("{0} {1} - {2}", c.FirstName, c.LastName, c.Keyword.Name);
                foreach (var item in c.ContactMethods) {
                    System.Console.WriteLine("\t -- {0} {1} ", item.Keyword.Name, item.Value);
                }
            }
            //add item
            System.Console.WriteLine("Enter a contact in the following format (FirstName LastName Phone)");
            var results = System.Console.ReadLine();
            var values = results.Split();
            var contact = new DataAccess.Models.Contact {FirstName = values[0], LastName = values[1], KeywordsId = 5 };
            db.Contacts.Add(contact);
            contact.ContactMethods.Add(new DataAccess.Models.ContactMethod {KeywordID = 7, Value= values[2] });
            db.SaveChanges();
            /*
            System.Console.WriteLine("Hello World");
            //System.Console.ReadLine();
            System.Console.WriteLine("What is your username");
            var response = System.Console.ReadLine();

            System.Console.WriteLine("Here's your profile info");
            System.Console.Write(GetProfile(response)+"\n");

            var db = new DataAccess.Test1Entities();
            foreach (var item in db.Contacts) {
                System.Console.WriteLine(item.FirstName + " " + item.LastName);
            }
            System.Console.ReadLine();
             */
        }