public void Validate(CreatePieceViewModel viewModel, ModelStateDictionary modelState) { if (string.IsNullOrWhiteSpace(viewModel.Piece.Title)) { modelState.AddModelError("Piece.Title", "A title is required."); } if (viewModel.Piece.PermanentLocationId < 0 && string.IsNullOrWhiteSpace(viewModel.Piece.PermanentLocationName)) { modelState.AddModelError("Piece.PermanentLocationName", "A location requires a name."); } if (viewModel.Piece.CurrentLocationId < 0 && string.IsNullOrWhiteSpace(viewModel.Piece.CurrentLocationName)) { modelState.AddModelError("Piece.CurrentLocationName", "A location requires a name."); } if (viewModel.Piece.ArtistId < 0 && string.IsNullOrWhiteSpace(viewModel.Piece.ArtistName)) { modelState.AddModelError("Piece.ArtistName", "An artist requires a name."); } if (viewModel.Piece.AcquisitionId < 0 && viewModel.Piece.AcquisitionDate == null && (viewModel.Piece.AcquisitionFundingSourceId == null || viewModel.Piece.AcquisitionFundingSourceId < 0 && string.IsNullOrWhiteSpace(viewModel.Piece.FundingSourceName))) { modelState.AddModelError("Piece.AcquisitionDate", "A new acquisition requires a date or source."); modelState.AddModelError("Piece.AcquisitionFundingSourceId", "A new acquisition requires a date or source."); } }
public void SetUp() { viewModel = new CreatePieceViewModel { Piece = new CreatePieceModel(), }; modelState = new ModelStateDictionary(); }
public void PostCreate_WithInvalidCreatePieceViewModel_ReturnsViewResultWithCreatePieceViewModel() { // Arrange var viewModel = new CreatePieceViewModel { Piece = new CreatePieceModel() }; collectionController.ModelState.AddModelError("ErrorKey", "Error Message"); // Act var result = collectionController.Create(viewModel) as ViewResult; // Assert Assert.IsInstanceOfType(result.ViewData.Model, typeof(CreatePieceViewModel)); }
public void PostCreate_WithValidViewModel_ReturnsRedirectToActionResult() { // Arrange var viewModel = new CreatePieceViewModel { Piece = new CreatePieceModel { Title = "something" } }; // Act var result = collectionController.Create(viewModel); // Assert Assert.IsInstanceOfType(result, typeof(RedirectToActionResult)); }
public void PostCreate_WithInvalidModelStateFromBinding_ReturnsViewResultWithCreatePieceViewModel() { // Arrange var viewModel = new CreatePieceViewModel { Piece = new CreatePieceModel { Title = "something" } }; collectionController.ModelState.AddModelError("key", "error"); // Act var result = collectionController.Create(viewModel) as ViewResult; // Assert Assert.IsInstanceOfType(result.ViewData.Model, typeof(CreatePieceViewModel)); }
public void PostCreate_WithValidCreatePieceViewModel_CallsCreatePieceCommand() { // Arrange var viewModel = new CreatePieceViewModel { Piece = new CreatePieceModel { Title = "something" } }; mockUserManager.Setup(u => u.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser { MuseumId = 1 }); // Act var result = collectionController.Create(viewModel); // Assert mockCreatePieceCommand.Verify(c => c.Execute(viewModel.Piece)); }
public IActionResult Create(CreatePieceViewModel viewModel) { createPieceViewModelValidator.Validate(viewModel, ModelState); if (ModelState.IsValid) { var user = userManager.FindByIdAsync(User.FindFirstValue(ClaimTypes.NameIdentifier)).Result; viewModel.Piece.MuseumId = user.MuseumId; createPieceCommand.Execute(viewModel.Piece); return(RedirectToAction(nameof(Index))); } var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var newViewModel = createPieceViewModelFactory.Create(userId); newViewModel.Piece = viewModel.Piece; return(View(newViewModel)); }
public CreatePieceViewModel Create(string userId) { var museum = museumQuery.Execute(userId); var viewModel = new CreatePieceViewModel { Piece = new CreatePieceModel() }; var media = mediumListQuery.Execute(museum.Id); viewModel.Media = media .Select(m => new SelectListItem { Value = m.Id.ToString(), Text = m.Name }); var genres = genreListQuery.Execute(museum.Id); viewModel.Genres = genres .Select(g => new SelectListItem { Value = g.Id.ToString(), Text = g.Name }); var subgenres = subgenreListQuery.Execute(museum.Id); viewModel.Subgenres = subgenres .Select(s => new SelectListItem { Value = s.Id.ToString(), Text = s.Name }); var subjectMatters = subjectMatterListQuery.Execute(museum.Id); viewModel.SubjectMatters = subjectMatters .Select(s => new SelectListItem { Value = s.Id.ToString(), Text = s.Name }); var locations = locationListQuery.Execute(museum.Id); viewModel.Locations = locations .Select(l => new SelectListItem { Value = l.Id.ToString(), Text = l.Name }); var countries = countryListQuery.Execute(); viewModel.Countries = countries .Select(c => new SelectListItem { Value = c.Id.ToString(), Text = c.Name }).ToList(); var unitsOfMeasure = unitOfMeasureListQuery.Execute(); viewModel.UnitsOfMeasure = unitsOfMeasure .Select(u => new SelectListItem { Value = u.Id.ToString(), Text = u.Abbreviation }); var artists = artistListQuery.Execute(museum.Id); viewModel.Artists = artists .Select(a => new SelectListItem { Value = a.Id.ToString(), Text = string.IsNullOrWhiteSpace(a.AlsoKnownAs) ? a.Name : string.Format("{0} aka \"{1}\"", a.Name, a.AlsoKnownAs) }); var acquisitions = acquisitionListQuery.Execute(museum.Id); viewModel.Acquisitions = acquisitions .Select(a => new SelectListItem { Value = a.Id.ToString(), Text = a.Date?.ToString("MM/dd/yyyy") + " " + a.PieceSource }); var pieceSources = pieceSourceListQuery.Execute(museum.Id); viewModel.PieceSources = pieceSources .Select(p => new SelectListItem { Value = p.Id.ToString(), Text = p.Name }); var fundingSources = fundingSourceListQuery.Execute(museum.Id); viewModel.FundingSources = fundingSources .Select(f => new SelectListItem { Value = f.Id.ToString(), Text = f.Name }); var collections = collectionListQuery.Execute(museum.Id); viewModel.Collections = collections .Select(c => new SelectListItem { Value = c.Id.ToString(), Text = c.Name }); return(viewModel); }