private async Task <Cube3DIntersectionModel> CreateCubesIntersectionDto(Cube3DIntersectionModel cube3DIntersectionModel)
        {
            var cubesIntersectionMappedEntity = ObjectMapper.Mapper.Map <Cube3DIntersection>(cube3DIntersectionModel);

            if (cubesIntersectionMappedEntity == null)
            {
                throw new ApplicationException($"Entity could not be mapped.");
            }

            var newCubeIntersectionMappedEntity = await _cube3DIntersectionRepository.Create(cubesIntersectionMappedEntity);

            _logger.LogInformation($"Entity successfully added - { serviceName } ");

            var newCubeIntersectionMappedDto = ObjectMapper.Mapper.Map <Cube3DIntersectionModel>(newCubeIntersectionMappedEntity);

            var cubesIntersectionDto = new Cube3DIntersectionModel
            {
                FirstCube3D  = await UpdateCubeDto(newCubeIntersectionMappedDto.FirstCube3D, newCubeIntersectionMappedDto.EdgesLength),
                SecondCube3D = await UpdateCubeDto(newCubeIntersectionMappedDto.SecondCube3D, newCubeIntersectionMappedDto.EdgesLength),
                EdgesLength  = newCubeIntersectionMappedDto.EdgesLength
            };

            cubesIntersectionDto.Id                 = cubesIntersectionDto.FirstCube3D.Id;
            cubesIntersectionDto.SecondCube3DId     = cubesIntersectionDto.SecondCube3D.Id;
            cubesIntersectionDto.Collision          = CubeModelExtensions.Collision(cubesIntersectionDto.FirstCube3D, cubesIntersectionDto.SecondCube3D);
            cubesIntersectionDto.IntersectionVolume = CubeModelExtensions.IntersectionVolume(cubesIntersectionDto.FirstCube3D, cubesIntersectionDto.SecondCube3D);

            await UpdateCubesIntersectionDto(cubesIntersectionDto);

            return(cubesIntersectionDto);
        }
Example #2
0
 private void ValidatePostCubes3DIntersection(Cube3DIntersectionModel cubes3DIntersectionModel)
 {
     if (cubes3DIntersectionModel == null)
     {
         _logger.LogError($"The List of Cubes cannot be null", nameof(cubes3DIntersectionModel));
         throw new ArgumentNullException(nameof(cubes3DIntersectionModel));
     }
 }
        public async Task PostCubes3DIntersectionShouldOverlap()
        {
            // Arrange
            var cubes3DIntersectionApiController = CreateCubes3DIntersectionApiController();

            var firstCube3D = new Cube3DModel
            {
                PointCoordinates = PointModelFactory.Create(2, 2, 2)
            };

            var secondCube3D = new Cube3DModel
            {
                PointCoordinates = PointModelFactory.Create(3, 2, 2)
            };

            var edgesLength = 2;

            var cube3DIntersectionRequest = new Cube3DIntersectionModel
            {
                FirstCube3D  = firstCube3D,
                SecondCube3D = secondCube3D,
                EdgesLength  = edgesLength
            };

            var cube3DIntersectionExpectedResponse = new Cube3DIntersectionModel
            {
                FirstCube3D        = firstCube3D,
                SecondCube3D       = secondCube3D,
                EdgesLength        = edgesLength,
                Collision          = true,
                IntersectionVolume = 4
            };

            _mockCube3DIntersectionService
            .Setup(ci => ci.Create(cube3DIntersectionRequest))
            .ReturnsAsync(cube3DIntersectionExpectedResponse);

            // Act
            var createdResponse = await cubes3DIntersectionApiController.PostCubes3DIntersection(
                cube3DIntersectionRequest);

            Assert.NotNull(createdResponse);

            var result = createdResponse.Result as ObjectResult;

            Assert.Equal(result?.StatusCode, (int)HttpStatusCode.Created);

            var actualResult = result?.Value as Cube3DIntersectionModel;

            Assert.IsType <Cube3DIntersectionModel>(actualResult);

            Assert.True(actualResult.Collision == cube3DIntersectionExpectedResponse.Collision);
            Assert.True(actualResult.IntersectionVolume.Equals(cube3DIntersectionExpectedResponse.IntersectionVolume));

            _mockCube3DIntersectionService.VerifyAll();
        }
        private async Task ValidateIfExist(Cube3DIntersectionModel Cube3DIntersectionModel)
        {
            var firstCube3D = await _cube3DIntersectionRepository.GetById(Cube3DIntersectionModel.Id);

            var secondCube3D = await _cube3DIntersectionRepository.GetById(Cube3DIntersectionModel.SecondCube3DId);

            if (firstCube3D != null && secondCube3D != null)
            {
                throw new ApplicationException($"The pair of cubes with Ids = { firstCube3D.Id } { secondCube3D.Id } already exists");
            }
        }
        private async Task <Cube3DIntersectionModel> UpdateCubesIntersectionDto(Cube3DIntersectionModel cube3DIntersectionModel)
        {
            var mappedEntity = ObjectMapper.Mapper.Map <Cube3DIntersection>(cube3DIntersectionModel);

            if (mappedEntity == null)
            {
                throw new ApplicationException($"Entity could not be mapped.");
            }

            await _cube3DIntersectionRepository.Update(mappedEntity);

            _logger.LogInformation($"Entity successfully added - AspnetRunAppService");

            var newMappedEntity = ObjectMapper.Mapper.Map <Cube3DIntersectionModel>(mappedEntity);

            return(newMappedEntity);
        }
 private void MakeCube3DIntersectionRequest(
     out Cubes3DIntersectionApiController cubes3DIntersectionApiController,
     out Cube3DModel firstCube3D,
     out Cube3DModel secondCube3D,
     out int edgesLength,
     out Cube3DIntersectionModel cube3DIntersectionRequest)
 {
     cubes3DIntersectionApiController = CreateCubes3DIntersectionApiController();
     firstCube3D = new Cube3DModel
     {
         PointCoordinates = PointModelFactory.Create(2, 2, 2)
     };
     secondCube3D = new Cube3DModel
     {
         PointCoordinates = PointModelFactory.Create(4, 2, 2)
     };
     edgesLength = 2;
     cube3DIntersectionRequest = new Cube3DIntersectionModel
     {
         FirstCube3D  = firstCube3D,
         SecondCube3D = secondCube3D,
         EdgesLength  = edgesLength
     };
 }
        public async Task <Cube3DIntersectionModel> Create(Cube3DIntersectionModel cube3DIntersectionModel)
        {
            await ValidateIfExist(cube3DIntersectionModel);

            return(await CreateCubesIntersectionDto(cube3DIntersectionModel));
        }
Example #8
0
        public async Task <ActionResult <Cube3DIntersectionModel> > PostCubes3DIntersection([FromBody] Cube3DIntersectionModel cube3DIntersectionModel)
        {
            ValidatePostCubes3DIntersection(cube3DIntersectionModel);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelErrors()));
            }

            try
            {
                var cubes3DDto = await _cube3DIntersectionService.Create(cube3DIntersectionModel);

                _logger.LogInformation($"Cubes 3D Intersection successfully generated.");
                return(CreatedAtAction(nameof(PostCubes3DIntersection), new { id = cubes3DDto.Id }, cubes3DDto));
            }
            catch (Exception ex)
            {
                _logger.LogError($"An error occurred when posting the Cubes 3D Intersection.", ex.Message);
                throw;
            }
        }