Exemplo n.º 1
0
        public ContactModel Create(ContactModel model)
        {
            using (var session = _database.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var contact = ConvertToDbo(model);

                    session.Save(contact);
                    transaction.Commit();

                    model.Id = contact.Id;
                    return model;
                }
            }
        }
Exemplo n.º 2
0
        public ContactModel Update(ContactModel model)
        {
            using (var session = _database.OpenSession())
            {
                using(var transaction = session.BeginTransaction())
                {
                    var contact = ConvertToDbo(model);
                    session.Update(contact);

                    var orphans = session.Query<PhoneNumber>().Where(n => n.Contact == null).ToList();
                    orphans.ForEach(session.Delete);

                    transaction.Commit();

                    return model;
                }
            }
        }
Exemplo n.º 3
0
        private Contact ConvertToDbo(ContactModel model)
        {
            var contact = new Contact
                    {
                        Id = model.Id ?? 0,
                        FirstName = model.FirstName,
                        SecondName = model.SecondName,
                        Email = model.Email,
                        PhoneNumbers = model.PhoneNumbers.Select(ConvertToDbo).ToList()
                    };

            foreach(var phoneNumber in contact.PhoneNumbers)
            {
                phoneNumber.Contact = contact;
            }

            return contact;
        }