コード例 #1
0
        public async Task <ActionResult <RecipeDTO> > GetRecipe(Guid id)
        {
            //var recipe = await _context.Recipe.FindAsync(id);
            var    recipes          = await(recipeRepo.GetByExpressionAsync(r => r.Id == id));
            Recipe recipe           = recipes.First(); //collecties
            var    recipeCategories = await categoryRepo.GetByExpressionAsync(cat => cat.Id == recipe.CategoryId);

            recipe.Category = recipeCategories.First(); //nodig in mapper (lazy loading)

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

            var recipeDTO = mapper.Map <RecipeDTO>(recipe);

            return(Ok(recipeDTO));
        }
コード例 #2
0
        public async Task <ActionResult <IEnumerable <CategoryDTO> > > GetCategories()
        {
            IEnumerable <Category> categoriesCached;

            // if cache ? aanmaken of gebruiken
            if (!memoryCache.TryGetValue(CacheKeys.CategoriesCacheKey, out categoriesCached))
            {
                categoriesCached = await genericRepo.GetAllAsync();

                //relaties -> recipes
                foreach (Category c in categoriesCached)
                {
                    var recipes = await genericRecipeRepo.GetByExpressionAsync(r => r.CategoryId == c.Id);

                    c.Recipes = (ICollection <Recipe>)recipes;
                }

                //2. Set options
                var cacheEntryOptions = new MemoryCacheEntryOptions()
                                        .SetSize(10)
                                        .SetSlidingExpiration(TimeSpan.FromMinutes(1));

                //3. Save data in cache
                memoryCache.Set(CacheKeys.CategoriesCacheKey, categoriesCached, cacheEntryOptions);
            }
            else
            {
                categoriesCached = (ICollection <Category>)memoryCache.Get(CacheKeys.CategoriesCacheKey);
            }

            //3. mappen naar DTO
            var categoriesDTO = mapper.Map <IEnumerable <CategoryDTO> >(categoriesCached);

            //4. Notificatie met geserialiseerde categoriesDTO naar een Hub sturen (registreren)
            await hubContext.Clients.All.SendAsync("ServerMessage", new { message = $"{JsonSerializer.Serialize(categoriesDTO)}" });

            //alternatief: methode in RepoHub oproepen

            return(Ok(categoriesDTO));
        }