예제 #1
0
        public IActionResult GetBrew(int id,
                                     [FromHeader(Name = ExtendedControllerBase.ACCEPT)] string mediaTypes,
                                     bool includeAdditionalInfo = false)
        {
            var splitMediaTypes = mediaTypes.Split(',');

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

            var brew = _homebrewRepository.GetBrew(id, includeAdditionalInfo);

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

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

                var linkedResourceToReturn = includeAdditionalInfo ?
                                             _mapper.Map <BrewDto>(brew).ShapeData(null) as IDictionary <string, object> :
                                             _mapper.Map <BrewWithoutAdditionalInfoDto>(brew).ShapeData(null) as IDictionary <string, object>;

                linkedResourceToReturn.Add(this.LINKS, links);

                return(Ok(linkedResourceToReturn));
            }

            return(includeAdditionalInfo ?
                   Ok(_mapper.Map <BrewDto>(brew).ShapeData(null)) :
                   Ok(_mapper.Map <BrewWithoutAdditionalInfoDto>(brew).ShapeData(null)));
        }
        public ActionResult <TastingNoteDto> CreateTastingNote(int brewId,
                                                               [FromBody] TastingNoteForCreationDto tastingNote,
                                                               [FromHeader(Name = ExtendedControllerBase.ACCEPT)] string mediaTypes)
        {
            var splitMediaTypes = mediaTypes.Split(',');

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

            if (!_homebrewRepository.BrewExists(brewId))
            {
                return(NotFound());
            }

            var includeAdditionalInfo = true;
            var finalTastingNote      = _mapper.Map <Entities.TastingNote>(tastingNote);

            _homebrewRepository.AddTastingNote(finalTastingNote);
            finalTastingNote.Brew = _homebrewRepository.GetBrew(brewId, includeAdditionalInfo);
            _homebrewRepository.Save();

            var tastingNoteToReturn = _mapper.Map <Models.TastingNoteDto>(finalTastingNote);

            if (parsedMediaTypes.Any(pmt => pmt.SubTypeWithoutSuffix.EndsWith(this.HATEOAS, StringComparison.InvariantCultureIgnoreCase)))
            {
                var links = CreateLinksForTastingNote(brewId, tastingNoteToReturn.Id);

                var linkedResourcesToReturn = tastingNote.ShapeData(null) as IDictionary <string, object>;

                linkedResourcesToReturn.Add(this.LINKS, links);

                return(CreatedAtRoute(
                           "GetTastingNote",
                           new { brewId, id = linkedResourcesToReturn["Id"] },
                           linkedResourcesToReturn));
            }

            return(CreatedAtRoute(
                       "GetTastingNote",
                       new { brewId, id = tastingNoteToReturn.Id },
                       tastingNoteToReturn));
        }