public void Verify_MapToEntity_WithExistingEntity_AssignsConceptProperties() { // Arrange var mapper = new ConceptMapper(); var model = ConceptsMockingSetup.DoMockingSetupForConceptModel(); // Act IConcept existingEntity = new Concept { Id = 1 }; mapper.MapToEntity(model.Object, ref existingEntity); // Assert Assert.Equal(model.Object.StartYear, existingEntity.StartYear); // Related Objects Assert.Equal(model.Object.PrimaryImageFileId, existingEntity.PrimaryImageFileId); Assert.Equal(model.Object.FirstIssueAppearanceId, existingEntity.FirstIssueAppearanceId); // Associated Objects model.VerifyGet(x => x.ConceptAliases, Times.Once); //Assert.Equal(model.Object.ConceptAliases?.Count, existingEntity.ConceptAliases?.Count); model.VerifyGet(x => x.ConceptIssuesAppearedIn, Times.Once); //Assert.Equal(model.Object.ConceptIssuesAppearedIn?.Count, existingEntity.ConceptIssuesAppearedIn?.Count); model.VerifyGet(x => x.ConceptIssues, Times.Once); //Assert.Equal(model.Object.ConceptIssues?.Count, existingEntity.ConceptIssues?.Count); model.VerifyGet(x => x.ConceptMovies, Times.Once); //Assert.Equal(model.Object.ConceptMovies?.Count, existingEntity.ConceptMovies?.Count); model.VerifyGet(x => x.ConceptVolumes, Times.Once); //Assert.Equal(model.Object.ConceptVolumes?.Count, existingEntity.ConceptVolumes?.Count); }
public IConceptModel Update(IConceptModel model) { // Validate model BusinessWorkflowBase.ValidateRequiredNullableID(model.Id); //BusinessWorkflowBase.ValidateRequiredString(model.Name, nameof(model.Name)); // Find existing entity // ReSharper disable once PossibleInvalidOperationException var existingEntity = ConceptsRepository.Get(model.Id.Value); // Check if we would be applying identical information, if we are, just return the original // ReSharper disable once SuspiciousTypeConversion.Global if (ConceptMapper.AreEqual(model, existingEntity)) { return(ConceptMapper.MapToModel(existingEntity)); } // Map model to an existing entity ConceptMapper.MapToEntity(model, ref existingEntity); existingEntity.UpdatedDate = BusinessWorkflowBase.GenDateTime; // Update it ConceptsRepository.Update(ConceptMapper.MapToEntity(model)); // Try to Save Changes ConceptsRepository.SaveChanges(); // Return the new value return(Get(existingEntity.Id)); }
public IConceptModel Create(IConceptModel model) { // Validate model BusinessWorkflowBase.ValidateIDIsNull(model.Id); //BusinessWorkflowBase.ValidateRequiredString(model.Name, nameof(model.Name)); // Search for an Existing Record (Don't allow Duplicates var results = Search(ConceptMapper.MapToSearchModel(model)); if (results.Any()) { return(results.First()); } // Return the first that matches // Map model to a new entity var newEntity = ConceptMapper.MapToEntity(model); newEntity.CreatedDate = BusinessWorkflowBase.GenDateTime; newEntity.UpdatedDate = null; newEntity.Active = true; // Add it ConceptsRepository.Add(newEntity); // Try to Save Changes ConceptsRepository.SaveChanges(); // Return the new value return(Get(newEntity.Id)); }