public IEnumerable <Recipe> GetRandomRecipes(RandomRecipeParameters paramteres) { int total = 0; if (paramteres.Tags != null) { total = _db.Recipes.Where(r => r.Tags.Any(t => paramteres.Tags.Contains(t.Name))).Count() - paramteres.WeekLength; } else { total = _db.Recipes.Count() - paramteres.WeekLength; } Random r = new Random(); int offset = total > 0 ? r.Next(0, total) : 0; return(_db.Recipes .Where(r => (paramteres.Tags == null || paramteres.Tags.Count <= 0) || r.Tags.Any(t => paramteres.Tags.Contains(t.Name))) .Include(r => r.Ingredients) .Include(r => r.Votes) .Include(r => r.Tags) .AsSplitQuery() .OrderBy(r => r.Id) .Skip(offset) .Take(paramteres.WeekLength)); }
public List <ReadRecipeDto> GetRandomRecipes([FromQuery] RandomRecipeParameters parameters) { return(_recipeService.GetRandomRecipes(parameters, GetUserId())); }
internal List <ReadRecipeDto> GetRandomRecipes(RandomRecipeParameters parameters, string id) { return(_mapper.Map <List <ReadRecipeDto> >(_recipeRepository.GetRandomRecipes(parameters), opt => opt.Items["UserId"] = id)); }