Exemple #1
0
        public IActionResult CreateCountry([FromBody] CountryForCreation country)
        {
            if (country == null)
            {
                return(BadRequest());
            }

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

            var countryEntity = Mapper.Map <Entities.Country>(country);

            _repository.AddCountry(countryEntity);

            if (!_repository.Save())
            {
                throw new Exception("Creating a country failed on save.");
                // return StatusCode(500, "A problem happened with handling your request.");
            }

            var countryToReturn = Mapper.Map <Models.CountryWithPostalCodesDto>(countryEntity);

            return(CreatedAtRoute("GetCountry",
                                  new { id = countryToReturn.Id },
                                  countryToReturn));
        }
Exemple #2
0
        public IActionResult CreatePostalCodeForCountry(int countryId,
                                                        [FromBody] PostalCodeForCreation postalCode)
        {
            if (postalCode == null)
            {
                return(BadRequest());
            }

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

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

            var postalEntity = Mapper.Map <Entities.AreaPostalCode>(postalCode);

            _repository.AddPostalCodeForCountry(countryId, postalEntity);

            if (!_repository.Save())
            {
                throw new Exception($"Creating a postal code for country {countryId} failed on save.");
            }

            var postalToReturn = Mapper.Map <Models.AreaPostalCodeDto>(postalEntity);

            return(CreatedAtRoute("GetPostalCodeForCountry",
                                  new { countryId, id = postalToReturn.Id },
                                  CreateLinksForPostalCode(postalToReturn)));
        }