Example #1
0
        public async Task <IActionResult> CreateCityForCountry(int countryId, [FromBody] CityAddResource city)
        {
            if (city == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            if (!await _countryRepository.CountryExistAsync(countryId))
            {
                return(NotFound());
            }

            var cityModel = _mapper.Map <City>(city);

            _cityRepository.AddCityForCountry(countryId, cityModel);
            if (!await _unitOfWork.SaveAsync())
            {
                throw new Exception("Error occurred when adding");
            }

            var cityResource = Mapper.Map <CityResource>(cityModel);

            return(CreatedAtRoute("GetCityForCountry", new { countryId, cityId = cityModel.Id }, CreateLinksForCity(cityResource)));
        }
Example #2
0
        public async Task <IActionResult> CreateCityForCountry(int countryId, [FromBody] CityAddResource city)
        {
            if (city == null)
            {
                return(BadRequest());
            }

            //直接在方法里写验证逻辑 要求City的name属性值不可以是“中国”
            if (city.Name == "中国")
            {
                ModelState.AddModelError(nameof(city.Name), "城市的名称不可以叫中国");
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            if (!await _countryRepository.CountryExistAsync(countryId))
            {
                return(NotFound());
            }

            var cityModel = _mapper.Map <City>(city);

            _cityRepository.AddCityForCountry(countryId, cityModel);

            if (!await _unitOfWork.SaveAsync())
            {
                return(StatusCode(500, "Error occurred when adding"));
            }

            var cityResource = _mapper.Map <CityResource>(cityModel);

            return(CreatedAtRoute("GetCity", new { countryId, cityId = cityModel.Id }, cityResource));
        }
Example #3
0
        public async Task <IActionResult> AddCity(Guid countryId, [FromBody] CityAddViewModel city)
        {
            if (city == null)
            {
                return(BadRequest());
            }

            if (!await countryRepository.CountryExists(countryId))
            {
                return(NotFound());
            }

            var cityModel = mapper.Map <City>(city);
            await cityRepository.AddCityForCountry(countryId, cityModel);

            if (!await unitOfWork.SaveAsync())
            {
                return(StatusCode(500, "server error"));
            }
            var cityResource = mapper.Map <CityResource>(cityModel);

            return(CreatedAtAction(nameof(GetCity),
                                   new { countryId, cityId = cityModel.Id }, CreateLinksFactory(cityResource)));
        }
        public async Task <IActionResult> CreateCityForCountry(int countryId, [FromBody] CityAddResource city)
        {
            if (city == null)
            {
                return(BadRequest());
            }

            if (!await _countryRepository.CountryExistAsync(countryId))
            {
                return(NotFound());
            }

            var cityModel = _mapper.Map <City>(city);

            _cityRepository.AddCityForCountry(countryId, cityModel);
            if (!await _unitOfWork.SaveAsync())
            {
                return(StatusCode(500, "Error occurred when adding"));
            }

            var cityResource = Mapper.Map <CityResource>(cityModel);

            return(CreatedAtRoute("GetCity", new { countryId, cityId = cityModel.Id }, cityResource));
        }