Skip to content

Commit

Permalink
list cmd now 5 per page and proper page counting + orderby name
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiao921 committed Jun 18, 2020
1 parent 45a5169 commit 3bde2dd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
14 changes: 9 additions & 5 deletions CHEF/Components/Commands/Cooking/CookModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ public async Task GetRecipe(
("Prints a list of all availables cooking recipes.")]
[Alias("l", "ls")]
public async Task ListRecipes(
[Summary("Optional word to filter the search.")]
string cmdName = null,
[Summary("Optional page if multiple pages are returned.")]
int page = 1)
int page = 1,
[Summary("Optional word to filter the search.")]
string cmdName = null
)
{
if (page < 1)
page = 1;

List<Recipe> recipes;
int totalRecipeCount;
const int descMaxLength = 10;

using (var context = new RecipeContext())
{
recipes = await context.GetRecipes(cmdName, page - 1);
Expand All @@ -58,13 +61,14 @@ public async Task ListRecipes(
foreach (var recipe in recipes)
{
var recipeText = recipe.Text;
var recipeTextLength = recipeText.Length > 20 ? 20 : recipeText.Length;
var recipeTextLength = recipeText.Length > descMaxLength ? descMaxLength : recipeText.Length;
embedBuilder.AddField($"{recipe.Name}", $"{recipeText.Substring(0, recipeTextLength)}");
}

if (recipes.Count > 0)
{
var totalPage = totalRecipeCount / RecipeContext.NumberPerPage;
var totalPage = totalRecipeCount / RecipeContext.NumberPerPage +
(totalRecipeCount % RecipeContext.NumberPerPage == 0 ? 0 : 1);
if (totalPage == 0)
totalPage += 1;

Expand Down
18 changes: 14 additions & 4 deletions CHEF/Components/Commands/Cooking/Recipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace CHEF.Components.Commands.Cooking
public class RecipeContext : DbContext
{
public DbSet<Recipe> Recipes { get; set; }
public const int NumberPerPage = 25;
public const int NumberPerPage = 5;

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
Expand All @@ -20,8 +20,10 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

optionsBuilder.UseNpgsql(global::CHEF.Database.Connection, builder =>
{
#if RELEASE
// callback for validating the server certificate against a CA certificate file.
builder.RemoteCertificateValidationCallback(global::CHEF.Database.RemoteCertificateValidationCallback);
#endif
});
optionsBuilder.UseSnakeCaseNamingConvention();
}
Expand All @@ -35,12 +37,20 @@ public async Task<List<Recipe>> GetRecipes(string nameFilter = null, int page =
List<Recipe> recipes;
if (nameFilter != null)
{
recipes = await Recipes.AsQueryable().Where(r => r.Name.ToLower().Contains(nameFilter.ToLower()))
.Skip(NumberPerPage * page).Take(NumberPerPage).ToListAsync();
recipes = await Recipes.AsQueryable().
Where(r => r.Name.ToLower().Contains(nameFilter.ToLower())).
Skip(NumberPerPage * page).
Take(NumberPerPage).
OrderBy(r => r.Name).
ToListAsync();
}
else
{
recipes = await Recipes.AsQueryable().Skip(NumberPerPage * page).Take(NumberPerPage).ToListAsync();
recipes = await Recipes.AsQueryable().
Skip(NumberPerPage * page).
Take(NumberPerPage).
OrderBy(r => r.Name).
ToListAsync();
}

return recipes;
Expand Down

0 comments on commit 3bde2dd

Please sign in to comment.