示例#1
0
        public async Task <IActionResult> GetDetail(long recipeId, String apiKey)
        {
            var recipe = await this.context.Recipes
                         .Include(r => r.comments)
                         .Include(r => r.extendedIngredients)
                         .Include(r => r.analyzedInstructions)
                         .ThenInclude(i => i.steps)
                         .SingleOrDefaultAsync(m => m.id == recipeId);

            var responseModel = new Recipe();

            if (recipe == null)
            {
                String actualUri = string.Format(detailUriTemplate, recipeId, httpClientService.GetApiKey());
                var    response  = await httpClient.GetAsync(actualUri);

                response.EnsureSuccessStatusCode();
                var responseText = await response.Content.ReadAsStringAsync();

                responseModel = JsonSerializer.Deserialize <Recipe>(responseText);

                recipe = new Recipe
                {
                    id = responseModel.id,
                    spoonacularSourceUrl = responseModel.spoonacularSourceUrl,
                    title                = responseModel.title,
                    summary              = responseModel.summary,
                    readyInMinutes       = responseModel.readyInMinutes,
                    image                = responseModel.image,
                    comments             = new List <Comment>(),
                    analyzedInstructions = responseModel.analyzedInstructions,
                    extendedIngredients  = responseModel.extendedIngredients
                };
                this.context.Recipes.Add(recipe);
                await this.context.SaveChangesAsync();
            }

            else
            {
                responseModel = recipe;
            }

            recipe.comments = recipe.comments.OrderBy(c => c.Created).ToList();
            return(Ok(responseModel));
        }