예제 #1
0
        public void MapResponseToRequest()
        {
            var mapper = new ApiCityModelMapper();
            var model  = new ApiCityResponseModel();

            model.SetProperties(1, "A", 1);
            ApiCityRequestModel response = mapper.MapResponseToRequest(model);

            response.Name.Should().Be("A");
            response.ProvinceId.Should().Be(1);
        }
예제 #2
0
        public virtual BOCity MapModelToBO(
            int id,
            ApiCityRequestModel model
            )
        {
            BOCity boCity = new BOCity();

            boCity.SetProperties(
                id,
                model.Name,
                model.ProvinceId);
            return(boCity);
        }
예제 #3
0
        public virtual async Task <IActionResult> Create([FromBody] ApiCityRequestModel model)
        {
            CreateResponse <ApiCityResponseModel> result = await this.CityService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Cities/{result.Record.Id}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
예제 #4
0
        public void CreatePatch()
        {
            var mapper = new ApiCityModelMapper();
            var model  = new ApiCityRequestModel();

            model.SetProperties("A", 1);

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

            patch.ApplyTo(response);
            response.Name.Should().Be("A");
            response.ProvinceId.Should().Be(1);
        }
예제 #5
0
        public virtual async Task <CreateResponse <ApiCityResponseModel> > Create(
            ApiCityRequestModel model)
        {
            CreateResponse <ApiCityResponseModel> response = new CreateResponse <ApiCityResponseModel>(await this.CityModelValidator.ValidateCreateAsync(model));

            if (response.Success)
            {
                var bo     = this.BolCityMapper.MapModelToBO(default(int), model);
                var record = await this.CityRepository.Create(this.DalCityMapper.MapBOToEF(bo));

                response.SetRecord(this.BolCityMapper.MapBOToModel(this.DalCityMapper.MapEFToBO(record)));
            }

            return(response);
        }
예제 #6
0
        private async Task <ApiCityRequestModel> PatchModel(int id, JsonPatchDocument <ApiCityRequestModel> patch)
        {
            var record = await this.CityService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiCityRequestModel request = this.CityModelMapper.MapResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
예제 #7
0
        public virtual async Task <UpdateResponse <ApiCityResponseModel> > Update(
            int id,
            ApiCityRequestModel model)
        {
            var validationResult = await this.CityModelValidator.ValidateUpdateAsync(id, model);

            if (validationResult.IsValid)
            {
                var bo = this.BolCityMapper.MapModelToBO(id, model);
                await this.CityRepository.Update(this.DalCityMapper.MapBOToEF(bo));

                var record = await this.CityRepository.Get(id);

                return(new UpdateResponse <ApiCityResponseModel>(this.BolCityMapper.MapBOToModel(this.DalCityMapper.MapEFToBO(record))));
            }
            else
            {
                return(new UpdateResponse <ApiCityResponseModel>(validationResult));
            }
        }
예제 #8
0
        public async void Create()
        {
            var mock  = new ServiceMockFacade <ICityRepository>();
            var model = new ApiCityRequestModel();

            mock.RepositoryMock.Setup(x => x.Create(It.IsAny <City>())).Returns(Task.FromResult(new City()));
            var service = new CityService(mock.LoggerMock.Object,
                                          mock.RepositoryMock.Object,
                                          mock.ModelValidatorMockFactory.CityModelValidatorMock.Object,
                                          mock.BOLMapperMockFactory.BOLCityMapperMock,
                                          mock.DALMapperMockFactory.DALCityMapperMock,
                                          mock.BOLMapperMockFactory.BOLEventMapperMock,
                                          mock.DALMapperMockFactory.DALEventMapperMock);

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

            response.Should().NotBeNull();
            mock.ModelValidatorMockFactory.CityModelValidatorMock.Verify(x => x.ValidateCreateAsync(It.IsAny <ApiCityRequestModel>()));
            mock.RepositoryMock.Verify(x => x.Create(It.IsAny <City>()));
        }
예제 #9
0
        public async void Delete()
        {
            var mock  = new ServiceMockFacade <ICityRepository>();
            var model = new ApiCityRequestModel();

            mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask);
            var service = new CityService(mock.LoggerMock.Object,
                                          mock.RepositoryMock.Object,
                                          mock.ModelValidatorMockFactory.CityModelValidatorMock.Object,
                                          mock.BOLMapperMockFactory.BOLCityMapperMock,
                                          mock.DALMapperMockFactory.DALCityMapperMock,
                                          mock.BOLMapperMockFactory.BOLEventMapperMock,
                                          mock.DALMapperMockFactory.DALEventMapperMock);

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

            response.Should().NotBeNull();
            mock.RepositoryMock.Verify(x => x.Delete(It.IsAny <int>()));
            mock.ModelValidatorMockFactory.CityModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>()));
        }
예제 #10
0
        public virtual async Task <IActionResult> Update(int id, [FromBody] ApiCityRequestModel model)
        {
            ApiCityRequestModel request = await this.PatchModel(id, this.CityModelMapper.CreatePatch(model));

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

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
예제 #11
0
        public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiCityRequestModel> patch)
        {
            ApiCityResponseModel record = await this.CityService.Get(id);

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

                UpdateResponse <ApiCityResponseModel> result = await this.CityService.Update(id, model);

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
예제 #12
0
        public virtual async Task <UpdateResponse <ApiCityResponseModel> > CityUpdateAsync(int id, ApiCityRequestModel item)
        {
            HttpResponseMessage httpResponse = await this.client.PutAsJsonAsync($"api/Cities/{id}", item).ConfigureAwait(false);

            return(JsonConvert.DeserializeObject <UpdateResponse <ApiCityResponseModel> >(httpResponse.Content.ContentToString()));
        }
예제 #13
0
 public async Task <ValidationResult> ValidateUpdateAsync(int id, ApiCityRequestModel model)
 {
     this.NameRules();
     this.ProvinceIdRules();
     return(await this.ValidateAsync(model, id));
 }