public IPowerModel Create(IPowerModel 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(PowerMapper.MapToSearchModel(model)); if (results.Any()) { return(results.First()); } // Return the first that matches // Map model to a new entity var newEntity = PowerMapper.MapToEntity(model); newEntity.CreatedDate = BusinessWorkflowBase.GenDateTime; newEntity.UpdatedDate = null; newEntity.Active = true; // Add it PowersRepository.Add(newEntity); // Try to Save Changes PowersRepository.SaveChanges(); // Return the new value return(Get(newEntity.Id)); }
public IPowerModel Update(IPowerModel model) { // Validate model BusinessWorkflowBase.ValidateRequiredNullableID(model.Id); //BusinessWorkflowBase.ValidateRequiredString(model.Name, nameof(model.Name)); // Find existing entity // ReSharper disable once PossibleInvalidOperationException var existingEntity = PowersRepository.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 (PowerMapper.AreEqual(model, existingEntity)) { return(PowerMapper.MapToModel(existingEntity)); } // Map model to an existing entity PowerMapper.MapToEntity(model, ref existingEntity); existingEntity.UpdatedDate = BusinessWorkflowBase.GenDateTime; // Update it PowersRepository.Update(PowerMapper.MapToEntity(model)); // Try to Save Changes PowersRepository.SaveChanges(); // Return the new value return(Get(existingEntity.Id)); }
public void Verify_MapToEntity_AssignsPowerProperties() { // Arrange var mapper = new PowerMapper(); var model = PowersMockingSetup.DoMockingSetupForPowerModel(); // Act var entity = mapper.MapToEntity(model.Object); // Assert // <None> // Related Objects // <None> // Associated Objects //Assert.Equal(model.Object.CharacterPowers?.Count, entity.CharacterPowers?.Count); model.VerifyGet(x => x.CharacterPowers, Times.Once); //Assert.Equal(model.Object.PowerAliases?.Count, entity.PowerAliases?.Count); model.VerifyGet(x => x.PowerAliases, Times.Once); }
public void Verify_MapToEntity_WithExistingEntity_AssignsPowerProperties() { // Arrange var mapper = new PowerMapper(); var model = PowersMockingSetup.DoMockingSetupForPowerModel(); // Act IPower existingEntity = new Power { Id = 1 }; mapper.MapToEntity(model.Object, ref existingEntity); // Assert // <None> // Related Objects // <None> // Associated Objects model.VerifyGet(x => x.CharacterPowers, Times.Once); //Assert.Equal(model.Object.CharacterPowers?.Count, existingEntity.CharacterPowers?.Count); model.VerifyGet(x => x.PowerAliases, Times.Once); //Assert.Equal(model.Object.PowerAliases?.Count, existingEntity.PowerAliases?.Count); }