Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions src/Core/Context/StaticSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,45 +156,52 @@ public SearchResults Search(AuthIdentity identity, RecipeQuery query)
case RecipeQuery.SortOrder.Title:
q =
(query.Direction == RecipeQuery.SortDirection.Ascending)
? q.OrderBy(p => p.Recipe.Title)
: q.OrderByDescending(p => p.Recipe.Title);
? q.OrderBy(p => p.Recipe.Title).ThenBy(p => p.Recipe.RecipeId)
: q.OrderByDescending(p => p.Recipe.Title).ThenBy(p => p.Recipe.RecipeId);
break;
case RecipeQuery.SortOrder.PrepTime:
q =
(query.Direction == RecipeQuery.SortDirection.Ascending)
? q.OrderBy(p => p.Recipe.PrepTime)
: q.OrderByDescending(p => p.Recipe.PrepTime);
? q.OrderBy(p => p.Recipe.PrepTime).ThenBy(p => p.Recipe.RecipeId)
: q.OrderByDescending(p => p.Recipe.PrepTime).ThenBy(p => p.Recipe.RecipeId);
break;
case RecipeQuery.SortOrder.CookTime:
q =
(query.Direction == RecipeQuery.SortDirection.Ascending)
? q.OrderBy(p => p.Recipe.CookTime)
: q.OrderByDescending(p => p.Recipe.CookTime);
? q.OrderBy(p => p.Recipe.CookTime).ThenBy(p => p.Recipe.RecipeId)
: q.OrderByDescending(p => p.Recipe.CookTime).ThenBy(p => p.Recipe.RecipeId);
break;
case RecipeQuery.SortOrder.TotalTime:
q =
(query.Direction == RecipeQuery.SortDirection.Ascending)
? q.OrderBy(p => p.Recipe.PrepTime + p.Recipe.CookTime)
: q.OrderByDescending(p => p.Recipe.PrepTime + p.Recipe.CookTime);
.ThenBy(p => p.Recipe.RecipeId)
: q.OrderByDescending(p => p.Recipe.PrepTime + p.Recipe.CookTime)
.ThenBy(p => p.Recipe.RecipeId);
break;
case RecipeQuery.SortOrder.Image:
q =
(query.Direction == RecipeQuery.SortDirection.Ascending)
? q.OrderBy(p => p.Recipe.ImageUrl)
: q.OrderByDescending(p => p.Recipe.ImageUrl);
? q.OrderBy(p => p.Recipe.ImageUrl).ThenBy(p => p.Recipe.RecipeId)
: q.OrderByDescending(p => p.Recipe.ImageUrl).ThenBy(p => p.Recipe.RecipeId);
break;
default:
q =
(query.Direction == RecipeQuery.SortDirection.Ascending)
? q.OrderBy(p => p.Recipe.Rating)
: q.OrderByDescending(p => p.Recipe.Rating);
? q.OrderBy(p => p.Recipe.Rating).ThenBy(p => p.Recipe.RecipeId)
: q.OrderByDescending(p => p.Recipe.Rating).ThenBy(p => p.Recipe.RecipeId);
break;
}

var totalCount = q.LongCount();

return new SearchResults
{
Briefs = q.Select(r => Provisioning.DTO.Recipes.ToRecipeBrief(r.Recipe)).ToArray(),
TotalCount = q.Count(),
Briefs = q.Skip(query.Offset)
.Take(RecipeQuery.PageSize)
.Select(r => Provisioning.DTO.Recipes.ToRecipeBrief(r.Recipe))
.ToArray(),
TotalCount = totalCount,
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/Core/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("UnitTests")]
2 changes: 2 additions & 0 deletions src/Core/Recipes/RecipeQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace KitchenPC.Core.Recipes;

public class RecipeQuery
{
public const int PageSize = 100;

public enum PhotoFilter
{
All = 0,
Expand Down
13 changes: 7 additions & 6 deletions src/DB/NHSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,19 +224,20 @@ public async Task<SearchResults> SearchAsync(
RecipeQuery.SortOrder.Image => q.OrderBy(p => p.ImageUrl),
_ => q.OrderBy(p => p.Rating),
};
var results = (
var totalCount = await q.ToRowCountInt64Query().RowCountInt64Async(cancellationToken);
var results = await (
query.Direction == RecipeQuery.SortDirection.Descending ? orderBy.Desc() : orderBy.Asc()
)
.ThenBy(p => p.RecipeId)
.Asc()
.Skip(query.Offset)
.Take(100)
.Take(RecipeQuery.PageSize)
.ListAsync(cancellationToken);

var loadedResults = await results;

return new SearchResults
{
Briefs = loadedResults.Select(r => r.AsRecipeBrief()).ToArray(),
TotalCount = loadedResults.Count, // TODO: This needs to be the total matches, not the returned matches
Briefs = results.Select(r => r.AsRecipeBrief()).ToArray(),
TotalCount = totalCount,
};
}
}
69 changes: 69 additions & 0 deletions src/UnitTests/RecipeSearch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Linq;
using KitchenPC.Core;
using KitchenPC.Core.Context;
using KitchenPC.Core.Provisioning;
using KitchenPC.Core.Recipes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RecipeDto = KitchenPC.Core.Provisioning.DTO.Recipes;

namespace KitchenPC.UnitTests;

[TestClass]
public class RecipeSearchTest
{
[TestMethod]
public void StaticSearchReturnsStablePagesAndTotalCount()
{
var recipes = Enumerable
.Range(0, 205)
.Select(index => new RecipeDto
{
RecipeId = new Guid(index + 1, 0, 0, new byte[8]),
Title = $"Recipe {index:D3}",
})
.ToList();
var store = new DataStore
{
Recipes = recipes,
RecipeIngredients = [],
RecipeMetadata = [],
};
var search = new StaticSearch(store);

var firstPage = search.Search(AuthIdentity.Anonymous, CreateQuery(0));
var secondPage = search.Search(AuthIdentity.Anonymous, CreateQuery(100));
var finalPage = search.Search(AuthIdentity.Anonymous, CreateQuery(200));
var beyondEnd = search.Search(AuthIdentity.Anonymous, CreateQuery(300));

Assert.AreEqual(205, firstPage.TotalCount);
Assert.AreEqual(RecipeQuery.PageSize, firstPage.Briefs.Length);
Assert.AreEqual(205, secondPage.TotalCount);
Assert.AreEqual(RecipeQuery.PageSize, secondPage.Briefs.Length);
Assert.AreEqual(205, finalPage.TotalCount);
Assert.AreEqual(5, finalPage.Briefs.Length);
Assert.AreEqual(205, beyondEnd.TotalCount);
Assert.AreEqual(0, beyondEnd.Briefs.Length);

CollectionAssert.AreEqual(
recipes.Take(RecipeQuery.PageSize).Select(recipe => recipe.RecipeId).ToArray(),
firstPage.Briefs.Select(recipe => recipe.Id).ToArray()
);
CollectionAssert.AreEqual(
recipes
.Skip(RecipeQuery.PageSize)
.Take(RecipeQuery.PageSize)
.Select(recipe => recipe.RecipeId)
.ToArray(),
secondPage.Briefs.Select(recipe => recipe.Id).ToArray()
);
}

private static RecipeQuery CreateQuery(int offset) =>
new()
{
Offset = offset,
Sort = RecipeQuery.SortOrder.Title,
Direction = RecipeQuery.SortDirection.Ascending,
};
}