Inheritance: IPrimaryContactViewModel
        public List<KeyValuePair<string, string>> Validate(OrganizationEditViewModel model)
        {
            var result = new List<KeyValuePair<string, string>>();

            if (string.IsNullOrEmpty(model.PrivacyPolicy) && string.IsNullOrEmpty(model.PrivacyPolicyUrl))
            {
                result.Add(new KeyValuePair<string, string>(nameof(model.PrivacyPolicyUrl), "You must either supply a URL to a privacy policy or supply the privacy policy content."));
            }

            return result;
        }
 public OrganizationControllerTests()
 {
   _organizationEditModel = new OrganizationEditViewModel
   {
     Id = 0,
     LogoUrl = "http://www.example.com/image.jpg",
     Name = "Test",
     PrimaryContactFirstName = "FirstName",
     PrimaryContactLastName = "LastName",
     PrimaryContactPhoneNumber = "0123456798",
     PrimaryContactEmail = "*****@*****.**",
     WebUrl = "http://www.example.com"
   };
 }
        /// <summary>
        /// Creates an OrganizationEditModel from an existing Organization 
        /// </summary>
        /// <param name="organization">An Organization that will be supply the initial values for editing.</param>
        /// <returns>An OrganizationEditModel that can be used to capture the fields for an Organization
        /// and it's location and primary contact built from values from the supplied organization. </returns>
        public static OrganizationEditViewModel ToEditModel_Organization(Organization organization)
        {
            var ret = new OrganizationEditViewModel
            {
                Id = organization.Id,
                Name = organization.Name,
                Location = organization.Location.ToEditModel(),
                LogoUrl = organization.LogoUrl,
                WebUrl = organization.WebUrl,
                PrivacyPolicy = organization.PrivacyPolicy
            };

            if (organization.OrganizationContacts?.SingleOrDefault(tc => tc.ContactType == (int)ContactTypes.Primary)?.Contact != null)
            {
                ret = (OrganizationEditViewModel)organization.OrganizationContacts?.SingleOrDefault(tc => tc.ContactType == (int)ContactTypes.Primary)?.Contact.ToEditModel(ret);
            }

            return ret;
        }
        public async Task<IActionResult> Edit(OrganizationEditViewModel organization)
        {
            if (organization == null)
            {
                return BadRequest();
            }

            var errors = _organizationValidator.Validate(organization);
            errors.ToList().ForEach(e => ModelState.AddModelError(e.Key, e.Value));

            if (ModelState.IsValid)
            {
                var isNameUnique = await _mediator.SendAsync(new OrganizationNameUniqueQueryAsync { OrganizationName = organization.Name, OrganizationId = organization.Id });
                if (isNameUnique)
                {
                    var id = await _mediator.SendAsync(new EditOrganizationAsync { Organization = organization });
                    return RedirectToAction(nameof(Details), new { id, area = "Admin" });
                }

                ModelState.AddModelError(nameof(organization.Name), "Organization with same name already exists. Please use different name.");
            }

            return View("Edit", organization);
        }
        public async Task EditPostRedirectsToCorrectActionWithCorrectData_WhenModelStateIsValid_AndOrganizationNameIsUnique()
        {
            var model = new OrganizationEditViewModel();
            var mockMediator = new Mock<IMediator>();
            mockMediator.Setup(y => y.SendAsync(It.IsAny<OrganizationNameUniqueQueryAsync>())).ReturnsAsync(true);
            mockMediator.Setup(x => x.SendAsync(It.Is<EditOrganizationAsync>(y => y.Organization == model))).ReturnsAsync(Id);            

            var controller = new OrganizationController(mockMediator.Object, SuccessValidator());
            var result = (RedirectToActionResult) await controller.Edit(model);

      Assert.Equal("Details", result.ActionName);
      Assert.Equal("Admin", result.RouteValues["area"]);
      Assert.Equal(Id, result.RouteValues["id"]);
    }
        public async Task EditPostSendsOrganizationNameUniqueQueryWithCorrectParametersWhenModelStateIsValid()
        {
            var mediator = new Mock<IMediator>();
            var organizationEditModel = new OrganizationEditViewModel { Name = "name", Id = 1 };

            var controller = new OrganizationController(mediator.Object, SuccessValidator());
            await controller.Edit(organizationEditModel);

            mediator.Verify(x => x.SendAsync(It.Is<OrganizationNameUniqueQueryAsync>(y => y.OrganizationName == organizationEditModel.Name && y.OrganizationId == organizationEditModel.Id)), 
                Times.Once());
        }
        public async Task EditPostReturnsTheCorrectViewAndViewModelWhenModelStateIsInvalid()
        {
            var model = new OrganizationEditViewModel();

            var controller = new OrganizationController(new Mock<IMediator>().Object, SuccessValidator());
            controller.ModelState.AddModelError("foo", "bar");
            var result = (ViewResult)await controller.Edit(model);

            Assert.Equal("Edit", result.ViewName);
            Assert.Same(model, result.ViewData.Model);
        }
        public async Task EditGetReturnsCorrectViewAndViewModel_WhenMediatorReturnsOrganizationEditModel()
        {
            CreateSut();

      var model = new OrganizationEditViewModel();

            _mediator.Setup(x => x.SendAsync(It.Is<OrganizationEditQueryAsync>(y => y.Id == Id))).ReturnsAsync(model);

            var result = (ViewResult) await _sut.Edit(Id);

      Assert.Equal("Edit", result.ViewName);
      Assert.Same(model, result.ViewData.Model);
    }
        public async Task EditPostInvokesOrganizationValidatorsValidateMethodWithTheCorrectOrganizationEditModel_WhenModelIsNotNull()
        {
            //Arrange
            var organizationValidatorMock = new Mock<IOrganizationEditModelValidator>();
            var errors = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("FakeProperty", "FakeError") };
            var organizationEditModel = new OrganizationEditViewModel {Id = 1, Name = "Test1"};
            organizationValidatorMock.Setup(o => o.Validate(organizationEditModel)).Returns(errors);
            var sut = new OrganizationController(null, organizationValidatorMock.Object);

            //Act
            var result = await sut.Edit(organizationEditModel) as ViewResult;

            //Assert
            organizationValidatorMock.Verify(o => o.Validate(organizationEditModel), Times.Once);
        }