Esempio n. 1
0
        public Contact UpdateContact(Contact contact)
        {
            DbContact dbContact = _context.Contacts
                                  .Include(c => c.PhoneNumbers)
                                  .FirstOrDefault(c => c.Id == contact.Id);

            if (dbContact == default)
            {
                throw new ContactNotFoundException();
            }

            UpdateContactProperties(dbContact, contact);
            _context.Contacts.Update(dbContact);
            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateException ex)
                when(ex.InnerException is Npgsql.PostgresException sqlException &&
                     sqlException.SqlState == UniqueConstraintException)
                {
                    throw new ContactAlreadyExistsException();
                }

            return(dbContact.CreateDomainContact());
        }
Esempio n. 2
0
        public Contact GetContact(Guid id)
        {
            DbContact contact = _context.Contacts
                                .Include(c => c.PhoneNumbers).FirstOrDefault(c => c.Id == id);

            if (contact == default)
            {
                throw new ContactNotFoundException();
            }
            return(contact.CreateDomainContact());
        }
Esempio n. 3
0
        public void InsertContact(Contact contact)
        {
            DbContact dbContact = _context.Contacts
                                  .FirstOrDefault(c => c.Name == contact.Name &&
                                                  c.AddressLine1 == contact.Address.AddressLine1 &&
                                                  c.AddressLine2 == contact.Address.AddressLine2 &&
                                                  c.AddressLine3 == contact.Address.AddressLine3 &&
                                                  c.City == contact.Address.City &&
                                                  c.State == contact.Address.State &&
                                                  c.Zip == contact.Address.Zip &&
                                                  c.Country == contact.Address.Country);

            if (dbContact != default)
            {
                throw new ContactAlreadyExistsException(dbContact.CreateDomainContact());
            }

            _context.Contacts.Add(contact.MapToDbContact());
            _context.SaveChanges();
        }