Beispiel #1
0
        public async Task <IActionResult> PutCountry(Guid id, CountryCreationDto countryDto)
        {
            var country = _countryService.GetCountryById(id);

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

            try
            {
                country.CountryName = countryDto.CountryName;
                country.Deleted     = countryDto.Deleted;
                _countryService.UpdateCountry(country);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (_countryService.GetCountryById(id) == null)
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Beispiel #2
0
        public async Task <ActionResult <Country> > PostCountry(CountryCreationDto countryDto)
        {
            var country = _mapper.Map <Country>(countryDto);

            _countryService.InsertCountry(country);
            return(Created("!api/Countries", country));
        }
Beispiel #3
0
        public async Task <IActionResult> CreateCountry([FromBody] CountryCreationDto country,
                                                        [FromHeader(Name = "Accept")] string mediaType)
        {
            if (country == null)
            {
                return(BadRequest());
            }

            if (country.Description == country.Name)
            {
                ModelState.AddModelError(nameof(CountryCreationDto),
                                         "The provided description should be different from the country name");
            }

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

            var newCountry = _mapper.Map <Country>(country);
            await _countryService.CreateCountry(newCountry);

            if (!await _countryService.Save())
            {
                throw new Exception("Creating a country failed on save.");
            }

            var returnedCountry = _mapper.Map <CountryDto>(newCountry);

            if (mediaType == "application/json+hateoas")
            {
                var links          = CreateCountryLinks(returnedCountry.Id, null);
                var linkedResource = returnedCountry.ShapeData(null) as IDictionary <string, object>;

                linkedResource.Add("links", links);

                return(CreatedAtRoute("GetCountry",
                                      new { id = returnedCountry.Id },
                                      linkedResource));
            }
            else
            {
                return(CreatedAtRoute("GetCountry",
                                      new { id = returnedCountry.Id },
                                      returnedCountry));
            }
        }