public void MapRequestToResponse()
        {
            var mapper = new ApiCountryRegionModelMapper();
            var model  = new ApiCountryRegionRequestModel();

            model.SetProperties(DateTime.Parse("1/1/1987 12:00:00 AM"), "A");
            ApiCountryRegionResponseModel response = mapper.MapRequestToResponse("A", model);

            response.CountryRegionCode.Should().Be("A");
            response.ModifiedDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.Name.Should().Be("A");
        }
        public virtual async Task <IActionResult> Create([FromBody] ApiCountryRegionRequestModel model)
        {
            CreateResponse <ApiCountryRegionResponseModel> result = await this.CountryRegionService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/CountryRegions/{result.Record.CountryRegionCode}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
        public virtual BOCountryRegion MapModelToBO(
            string countryRegionCode,
            ApiCountryRegionRequestModel model
            )
        {
            BOCountryRegion boCountryRegion = new BOCountryRegion();

            boCountryRegion.SetProperties(
                countryRegionCode,
                model.ModifiedDate,
                model.Name);
            return(boCountryRegion);
        }
        public void CreatePatch()
        {
            var mapper = new ApiCountryRegionModelMapper();
            var model  = new ApiCountryRegionRequestModel();

            model.SetProperties(DateTime.Parse("1/1/1987 12:00:00 AM"), "A");

            JsonPatchDocument <ApiCountryRegionRequestModel> patch = mapper.CreatePatch(model);
            var response = new ApiCountryRegionRequestModel();

            patch.ApplyTo(response);
            response.ModifiedDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.Name.Should().Be("A");
        }
        private async Task <ApiCountryRegionRequestModel> PatchModel(string id, JsonPatchDocument <ApiCountryRegionRequestModel> patch)
        {
            var record = await this.CountryRegionService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiCountryRegionRequestModel request = this.CountryRegionModelMapper.MapResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
        public virtual async Task <CreateResponse <ApiCountryRegionResponseModel> > Create(
            ApiCountryRegionRequestModel model)
        {
            CreateResponse <ApiCountryRegionResponseModel> response = new CreateResponse <ApiCountryRegionResponseModel>(await this.countryRegionModelValidator.ValidateCreateAsync(model));

            if (response.Success)
            {
                var bo     = this.bolCountryRegionMapper.MapModelToBO(default(string), model);
                var record = await this.countryRegionRepository.Create(this.dalCountryRegionMapper.MapBOToEF(bo));

                response.SetRecord(this.bolCountryRegionMapper.MapBOToModel(this.dalCountryRegionMapper.MapEFToBO(record)));
            }

            return(response);
        }
        public virtual async Task <UpdateResponse <ApiCountryRegionResponseModel> > Update(
            string countryRegionCode,
            ApiCountryRegionRequestModel model)
        {
            var validationResult = await this.countryRegionModelValidator.ValidateUpdateAsync(countryRegionCode, model);

            if (validationResult.IsValid)
            {
                var bo = this.bolCountryRegionMapper.MapModelToBO(countryRegionCode, model);
                await this.countryRegionRepository.Update(this.dalCountryRegionMapper.MapBOToEF(bo));

                var record = await this.countryRegionRepository.Get(countryRegionCode);

                return(new UpdateResponse <ApiCountryRegionResponseModel>(this.bolCountryRegionMapper.MapBOToModel(this.dalCountryRegionMapper.MapEFToBO(record))));
            }
            else
            {
                return(new UpdateResponse <ApiCountryRegionResponseModel>(validationResult));
            }
        }
        public async void Create()
        {
            var mock  = new ServiceMockFacade <ICountryRegionRepository>();
            var model = new ApiCountryRegionRequestModel();

            mock.RepositoryMock.Setup(x => x.Create(It.IsAny <CountryRegion>())).Returns(Task.FromResult(new CountryRegion()));
            var service = new CountryRegionService(mock.LoggerMock.Object,
                                                   mock.RepositoryMock.Object,
                                                   mock.ModelValidatorMockFactory.CountryRegionModelValidatorMock.Object,
                                                   mock.BOLMapperMockFactory.BOLCountryRegionMapperMock,
                                                   mock.DALMapperMockFactory.DALCountryRegionMapperMock,
                                                   mock.BOLMapperMockFactory.BOLStateProvinceMapperMock,
                                                   mock.DALMapperMockFactory.DALStateProvinceMapperMock);

            CreateResponse <ApiCountryRegionResponseModel> response = await service.Create(model);

            response.Should().NotBeNull();
            mock.ModelValidatorMockFactory.CountryRegionModelValidatorMock.Verify(x => x.ValidateCreateAsync(It.IsAny <ApiCountryRegionRequestModel>()));
            mock.RepositoryMock.Verify(x => x.Create(It.IsAny <CountryRegion>()));
        }
        public async void Delete()
        {
            var mock  = new ServiceMockFacade <ICountryRegionRepository>();
            var model = new ApiCountryRegionRequestModel();

            mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <string>())).Returns(Task.CompletedTask);
            var service = new CountryRegionService(mock.LoggerMock.Object,
                                                   mock.RepositoryMock.Object,
                                                   mock.ModelValidatorMockFactory.CountryRegionModelValidatorMock.Object,
                                                   mock.BOLMapperMockFactory.BOLCountryRegionMapperMock,
                                                   mock.DALMapperMockFactory.DALCountryRegionMapperMock,
                                                   mock.BOLMapperMockFactory.BOLStateProvinceMapperMock,
                                                   mock.DALMapperMockFactory.DALStateProvinceMapperMock);

            ActionResponse response = await service.Delete(default(string));

            response.Should().NotBeNull();
            mock.RepositoryMock.Verify(x => x.Delete(It.IsAny <string>()));
            mock.ModelValidatorMockFactory.CountryRegionModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <string>()));
        }
        public virtual async Task <IActionResult> Update(string id, [FromBody] ApiCountryRegionRequestModel model)
        {
            ApiCountryRegionRequestModel request = await this.PatchModel(id, this.CountryRegionModelMapper.CreatePatch(model));

            if (request == null)
            {
                return(this.StatusCode(StatusCodes.Status404NotFound));
            }
            else
            {
                UpdateResponse <ApiCountryRegionResponseModel> result = await this.CountryRegionService.Update(id, request);

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
        public virtual async Task <IActionResult> Patch(string id, [FromBody] JsonPatchDocument <ApiCountryRegionRequestModel> patch)
        {
            ApiCountryRegionResponseModel record = await this.CountryRegionService.Get(id);

            if (record == null)
            {
                return(this.StatusCode(StatusCodes.Status404NotFound));
            }
            else
            {
                ApiCountryRegionRequestModel model = await this.PatchModel(id, patch);

                UpdateResponse <ApiCountryRegionResponseModel> result = await this.CountryRegionService.Update(id, model);

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
 public async Task <ValidationResult> ValidateUpdateAsync(string id, ApiCountryRegionRequestModel model)
 {
     this.ModifiedDateRules();
     this.NameRules();
     return(await this.ValidateAsync(model, id));
 }