Пример #1
0
        public async Task <ActionResult <CityResource> > UpdateCity(int id, [FromBody] SaveCityResource saveCityResource)
        {
            var validator        = new SaveCityResourceValidator();
            var validationResult = await validator.ValidateAsync(saveCityResource);

            var requestIsInvalid = id == 0 || !validationResult.IsValid;

            if (requestIsInvalid)
            {
                return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok
            }
            var cityToBeUpdate = await _cityService.GetCityById(id);

            if (cityToBeUpdate == null)
            {
                return(NotFound());
            }

            var city = _mapper.Map <SaveCityResource, City>(saveCityResource);

            await _cityService.UpdateCity(cityToBeUpdate, city);

            var updated = await _cityService.GetCityById(id);

            var updatedResource = _mapper.Map <City, CityResource>(updated);

            return(Ok(updatedResource));
        }
Пример #2
0
        public async Task <ActionResult <CityResource> > Add([FromBody] SaveCityResource saveCityResource)
        {
            var validator        = new SaveCityResourceValidator();
            var validationResult = await validator.ValidateAsync(saveCityResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors)); // this needs refining, but for demo it is ok
            }
            var cityToCreate = _mapper.Map <SaveCityResource, City>(saveCityResource);

            var newCity = await _cityService.CreateCity(cityToCreate);

            var city = await _cityService.GetCityById(newCity.Id);

            var cityResource = _mapper.Map <City, CityResource>(city);

            return(Ok(cityResource));
        }