Пример #1
0
        public void UpdateMails(ContactViewModel newContact)
        {
            var emails = _db.EmailsTable.Where(r => r.ContactId == newContact.ContactId);

            var create = newContact.Emails.Where(r => r.IdEmail == 0)
                         .Select(r => new EmailsTable
            {
                Email = r.Email, ContactId = newContact.ContactId
            })
                         .ToList();

            _db.EmailsTable.AddRange(create);

            var update = newContact.Emails.Where(r => r.IdEmail != 0).ToList();

            foreach (var item in update)
            {
                var old = emails.Single(r => r.IdEmail == item.IdEmail);
                old.Email            = item.Email;
                _db.Entry(old).State = EntityState.Modified;
            }

            var newIds  = update.Select(r => r.IdEmail);
            var deleted = emails.Where(r => !newIds.Contains(r.IdEmail));

            foreach (var email in deleted)
            {
                _db.Entry(email).State = EntityState.Deleted;
            }
            _db.SaveChanges();
        }
        public void UpdateNumbers(ContactViewModel newContact)
        {
            var numbers       = _db.NumbersTable.Where(r => r.ContactId == newContact.ContactId);
            var createNumbers = newContact.Numbers.Where(r => r.IdNumber == 0)
                                .Select(r => new NumbersTable
            {
                Number = r.Number, ContactId = newContact.ContactId
            })
                                .ToList();

            _db.NumbersTable.AddRange(createNumbers);

            var updateNumbers = newContact.Numbers.Where(r => r.IdNumber != 0).ToList();

            foreach (var item in updateNumbers)
            {
                var old = numbers.Single(r => r.IdNumber == item.IdNumber);
                old.Number           = item.Number;
                _db.Entry(old).State = EntityState.Modified;
            }
            var newNumberIds   = updateNumbers.Select(r => r.IdNumber);
            var deletedNumbers = numbers.Where(r => !newNumberIds.Contains(r.IdNumber));

            foreach (var number in deletedNumbers)
            {
                _db.Entry(number).State = EntityState.Deleted;
            }
            _db.SaveChanges();
        }
        public void UpdateContact(Guid guid, ContactViewModel newContact)
        {
            var oldContact = _db.ContactsTable.SingleOrDefault(r => r.ContactId == guid);

            oldContact.Name       = newContact.Name;
            oldContact.Surname    = newContact.Surname;
            oldContact.Adress     = newContact.Adress;
            oldContact.PostalCode = newContact.PostalCode;
            oldContact.City       = newContact.City;
            oldContact.Country    = newContact.Country;
            _emailsService.UpdateMails(newContact);
            _numbersService.UpdateNumbers(newContact);
            _db.Entry(oldContact).State = EntityState.Modified;
            _db.SaveChanges();
        }