Esempio n. 1
0
        public void Verify_MapToEntity_WithExistingEntity_AssignsLocationProperties()
        {
            // Arrange
            var mapper = new LocationMapper();
            var model  = LocationsMockingSetup.DoMockingSetupForLocationModel();
            // Act
            ILocation existingEntity = new Location {
                Id = 1
            };

            mapper.MapToEntity(model.Object, ref existingEntity);
            // Assert
            Assert.Equal(model.Object.StartYear, existingEntity.StartYear);
            // Related Objects
            Assert.Equal(model.Object.FirstIssueAppearanceId, existingEntity.FirstIssueAppearanceId);
            Assert.Equal(model.Object.PrimaryImageFileId, existingEntity.PrimaryImageFileId);
            // Associated Objects
            model.VerifyGet(x => x.LocationAliases, Times.Once);
            //Assert.Equal(model.Object.LocationAliases?.Count, existingEntity.LocationAliases?.Count);
            model.VerifyGet(x => x.LocationAppearedInIssues, Times.Once);
            //Assert.Equal(model.Object.LocationAppearedInIssues?.Count, existingEntity.LocationAppearedInIssues?.Count);
            model.VerifyGet(x => x.LocationIssues, Times.Once);
            //Assert.Equal(model.Object.LocationIssues?.Count, existingEntity.LocationIssues?.Count);
            model.VerifyGet(x => x.LocationMovies, Times.Once);
            //Assert.Equal(model.Object.LocationMovies?.Count, existingEntity.LocationMovies?.Count);
            model.VerifyGet(x => x.LocationStoryArcs, Times.Once);
            //Assert.Equal(model.Object.LocationStoryArcs?.Count, existingEntity.LocationStoryArcs?.Count);
            model.VerifyGet(x => x.LocationVolumes, Times.Once);
            //Assert.Equal(model.Object.LocationVolumes?.Count, existingEntity.LocationVolumes?.Count);
        }
        public ILocationModel Create(ILocationModel 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(LocationMapper.MapToSearchModel(model));

            if (results.Any())
            {
                return(results.First());
            }                                              // Return the first that matches
            // Map model to a new entity
            var newEntity = LocationMapper.MapToEntity(model);

            newEntity.CreatedDate = BusinessWorkflowBase.GenDateTime;
            newEntity.UpdatedDate = null;
            newEntity.Active      = true;
            // Add it
            LocationsRepository.Add(newEntity);
            // Try to Save Changes
            LocationsRepository.SaveChanges();
            // Return the new value
            return(Get(newEntity.Id));
        }
        public ILocationModel Update(ILocationModel model)
        {
            // Validate model
            BusinessWorkflowBase.ValidateRequiredNullableID(model.Id);
            //BusinessWorkflowBase.ValidateRequiredString(model.Name, nameof(model.Name));
            // Find existing entity
            // ReSharper disable once PossibleInvalidOperationException
            var existingEntity = LocationsRepository.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 (LocationMapper.AreEqual(model, existingEntity))
            {
                return(LocationMapper.MapToModel(existingEntity));
            }
            // Map model to an existing entity
            LocationMapper.MapToEntity(model, ref existingEntity);
            existingEntity.UpdatedDate = BusinessWorkflowBase.GenDateTime;
            // Update it
            LocationsRepository.Update(LocationMapper.MapToEntity(model));
            // Try to Save Changes
            LocationsRepository.SaveChanges();
            // Return the new value
            return(Get(existingEntity.Id));
        }
 public void Verify_MapToEntity_WithExistingEntity_AssignsLocationProperties()
 {
     // Arrange
     var mapper = new LocationMapper();
     var model = LocationsMockingSetup.DoMockingSetupForLocationModel();
     // Act
     ILocation existingEntity = new Location { Id = 1 };
     mapper.MapToEntity(model.Object, ref existingEntity);
     // Assert
     Assert.Equal(model.Object.StartYear, existingEntity.StartYear);
     // Related Objects
     Assert.Equal(model.Object.FirstIssueAppearanceId, existingEntity.FirstIssueAppearanceId);
     Assert.Equal(model.Object.PrimaryImageFileId, existingEntity.PrimaryImageFileId);
     // Associated Objects
     model.VerifyGet(x => x.LocationAliases, Times.Once);
     //Assert.Equal(model.Object.LocationAliases?.Count, existingEntity.LocationAliases?.Count);
     model.VerifyGet(x => x.LocationAppearedInIssues, Times.Once);
     //Assert.Equal(model.Object.LocationAppearedInIssues?.Count, existingEntity.LocationAppearedInIssues?.Count);
     model.VerifyGet(x => x.LocationIssues, Times.Once);
     //Assert.Equal(model.Object.LocationIssues?.Count, existingEntity.LocationIssues?.Count);
     model.VerifyGet(x => x.LocationMovies, Times.Once);
     //Assert.Equal(model.Object.LocationMovies?.Count, existingEntity.LocationMovies?.Count);
     model.VerifyGet(x => x.LocationStoryArcs, Times.Once);
     //Assert.Equal(model.Object.LocationStoryArcs?.Count, existingEntity.LocationStoryArcs?.Count);
     model.VerifyGet(x => x.LocationVolumes, Times.Once);
     //Assert.Equal(model.Object.LocationVolumes?.Count, existingEntity.LocationVolumes?.Count);
 }