/// <summary>
 /// Constructs a ContactViewModel from a Contact object
 /// </summary>
 /// <param name="contact"></param>
 public ContactViewModel(Contact contact)
 {
     ContactId = contact.ContactId;
     Email = contact.Email;
     FirstName = contact.FirstName;
     LastName = contact.LastName;
     Phone = contact.Phone;
     Twitter = contact.Twitter;
 }
        public void AddContact_Throws_On_InValid_PortalId()
        {
            //Arrange
            var repository = new ContactRepository();
            var contact = new Contact { PortalId = PORTAL_InValidId };

            //Act

            //Assert
            Assert.Throws<ArgumentOutOfRangeException>(() => repository.AddContact(contact));
        }
        public void AddContact_Calls_IRepository_Insert_On_Valid_Contact()
        {
            //Arrange
            var portalId = PORTAL_ValidId;

            var repository = new ContactRepository();
            var contact = new Contact { PortalId = portalId };

            //Act
            repository.AddContact(contact);

            //Assert
            _mockRepository.Verify(r => r.Insert(contact));
        }
        public ActionResult Edit(Contact contact)
        {
            if (ModelState.IsValid)
            {
                if (contact.ContactId == -1)
                {
                    _repository.AddContact(contact);
                }
                else
                {
                    _repository.UpdateContact(contact);
                }

                return RedirectToDefaultRoute();
            }
            else
            {
                return View(contact);
            }
        }
        public HttpResponseMessage SaveContact(ContactViewModel viewModel)
        {
            Contact contact;

            if (viewModel.ContactId == -1)
            {
                contact = new Contact
                                {
                                    FirstName = viewModel.FirstName,
                                    LastName = viewModel.LastName,
                                    Email = viewModel.Email,
                                    Phone = viewModel.Phone,
                                    Twitter = viewModel.Twitter,
                                    PortalId = PortalSettings.PortalId
                                };
                _repository.AddContact(contact);
            }
            else
            {
                //Update
                contact = _repository.GetContact(viewModel.ContactId, PortalSettings.PortalId);

                if (contact != null)
                {
                    contact.FirstName = viewModel.FirstName;
                    contact.LastName = viewModel.LastName;
                    contact.Email = viewModel.Email;
                    contact.Phone = viewModel.Phone;
                    contact.Twitter = viewModel.Twitter;
                }
                _repository.UpdateContact(contact);
            }
            var response = new
            {
                success = true,
                data = new
                        {
                            contactId = contact.ContactId
                        }
            };

            return Request.CreateResponse(response);

        }
        public void AddContact_Returns_ValidId_On_Valid_Contact()
        {
            //Arrange
            var portalId = PORTAL_ValidId;
            _mockRepository.Setup(r => r.Insert(It.IsAny<Contact>()))
                            .Callback((Contact ct) => ct.ContactId = CONTACT_ValidId);

            var repository = new ContactRepository();
            var contact = new Contact { PortalId = portalId };

            //Act
            int contactId = repository.AddContact(contact);

            //Assert
            Assert.AreEqual(CONTACT_ValidId, contactId);
        }
        public void UpdateContact_Throws_On_InValid_ContactId()
        {
            //Arrange
            var repository = new ContactRepository();
            var contact = new Contact { ContactId = CONTACT_InValidId };

            //Act

            //Assert
            Assert.Throws<ArgumentOutOfRangeException>(() => repository.UpdateContact(contact));
        }