예제 #1
0
        public ActionResult UpdateBrew(int id,
                                       [FromBody] BrewForUpdateDto brew,
                                       [FromHeader(Name = ExtendedControllerBase.ACCEPT)] string mediaTypes)
        {
            var splitMediaTypes = mediaTypes.Split(',');

            if (!MediaTypeHeaderValue.TryParseList(splitMediaTypes,
                                                   out IList <MediaTypeHeaderValue> parsedMediaTypes))
            {
                return(BadRequest());
            }

            if (!_homebrewRepository.RecipeExists(brew.RecipeId))
            {
                ModelState.AddModelError(
                    "Recipe ID",
                    $"The associated recipe id [{brew.RecipeId}] for the {brew.Name} brew must be valid.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var includeAdditionalInfo = true;
            var brewEntity            = _homebrewRepository.GetBrew(id, includeAdditionalInfo);

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

            _mapper.Map(brew, brewEntity);
            _homebrewRepository.UpdateBrew(brewEntity);
            _homebrewRepository.Save();

            if (parsedMediaTypes.Any(pmt => pmt.SubTypeWithoutSuffix.EndsWith(this.HATEOAS, StringComparison.InvariantCultureIgnoreCase)))
            {
                var links = CreateLinksForBrew(brewEntity.Id, includeAdditionalInfo);

                var linkedResourceToReturn = _mapper.Map <BrewDto>(brewEntity)
                                             .ShapeData(null) as IDictionary <string, object>;

                linkedResourceToReturn.Add(this.LINKS, links);

                return(Ok(linkedResourceToReturn));
            }

            return(NoContent());
        }