Skip to content

Commit 7208f34

Browse files
committed
add facets to search method
1 parent 99dd578 commit 7208f34

File tree

6 files changed

+39
-12
lines changed

6 files changed

+39
-12
lines changed

Modrinth.Net.Test/FacetCollectionTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ public void CollectionWithOnlyOneFacet()
1111
{
1212
var collection = new FacetCollection();
1313
collection.Add(Facet.Category("test"));
14-
Assert.That(collection.ToString(), Is.EqualTo("[[\"category:test\"]]"));
14+
Assert.That(collection.ToString(), Is.EqualTo("[[\"categories:test\"]]"));
1515
}
1616

1717
[Test]
1818
public void CollectionWithMultipleFacets_OR()
1919
{
2020
var collection = new FacetCollection();
2121
collection.Add(Facet.Category("test"), Facet.Category("test2"));
22-
Assert.That(collection.ToString(), Is.EqualTo("[[\"category:test\",\"category:test2\"]]"));
22+
Assert.That(collection.ToString(), Is.EqualTo("[[\"categories:test\",\"categories:test2\"]]"));
2323
}
2424

2525
[Test]
@@ -28,7 +28,7 @@ public void CollectionWithMultipleFacets_AND()
2828
var collection = new FacetCollection();
2929
collection.Add(Facet.Category("test"));
3030
collection.Add(Facet.Category("test2"));
31-
Assert.That(collection.ToString(), Is.EqualTo("[[\"category:test\"],[\"category:test2\"]]"));
31+
Assert.That(collection.ToString(), Is.EqualTo("[[\"categories:test\"],[\"categories:test2\"]]"));
3232
}
3333

3434
[Test]
@@ -37,6 +37,6 @@ public void CollectionWithMultipleFacets_AND_OR()
3737
var collection = new FacetCollection();
3838
collection.Add(Facet.Category("test"));
3939
collection.Add(Facet.Category("test2"), Facet.Category("test3"));
40-
Assert.That(collection.ToString(), Is.EqualTo("[[\"category:test\"],[\"category:test2\",\"category:test3\"]]"));
40+
Assert.That(collection.ToString(), Is.EqualTo("[[\"categories:test\"],[\"categories:test2\",\"categories:test3\"]]"));
4141
}
4242
}

Modrinth.Net.Test/ModrinthApiTests/SearchTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Index = Modrinth.Models.Enums.Index;
1+
using Modrinth.Models.Facets;
2+
using Index = Modrinth.Models.Enums.Index;
23

34
namespace Modrinth.Net.Test.ModrinthApiTests;
45

@@ -125,4 +126,18 @@ public async Task Search_WithUpdatedSort_ShouldReturnSortedByUpdatedList()
125126
search.Hits.Select(p => p.DateModified),
126127
Is.EqualTo(search.Hits.Select(p => p.DateModified).OrderByDescending(d => d)));
127128
}
129+
130+
// Search with facets
131+
[Test]
132+
public async Task Search_WithFacets_ShouldReturnFilteredResults()
133+
{
134+
var facets = new FacetCollection();
135+
136+
facets.Add(Facet.Category("adventure"));
137+
138+
var search = await _client.Project.SearchAsync("", facets: facets);
139+
140+
// Check that every search result has the adventure category
141+
Assert.That(search.Hits.Select(p => p.Categories).All(c => c.Contains("adventure")));
142+
}
128143
}

Modrinth.Net/Endpoints/Project/IProjectApi.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Modrinth.Models;
2+
using Modrinth.Models.Facets;
23
using Index = Modrinth.Models.Enums.Index;
34

45
namespace Modrinth.Endpoints.Project;
@@ -9,6 +10,7 @@ public interface IProjectApi
910
/// Search Modrinth for project by it's name
1011
/// </summary>
1112
/// <param name="query">The query to search for</param>
13+
/// <param name="facets">Facets to filter the search by</param>
1214
/// <param name="index">The sorting method used for sorting search results</param>
1315
/// <param name="offset">The offset into the search. Skips this number of results</param>
1416
/// <param name="limit">The number of results returned by the search</param>
@@ -17,7 +19,8 @@ Task<SearchResponse> SearchAsync(
1719
string query,
1820
Index index = Index.Downloads,
1921
ulong offset = 0,
20-
ulong limit = 10);
22+
ulong limit = 10,
23+
FacetCollection? facets = null);
2124

2225
/// <summary>
2326
/// Gets project by slug or ID

Modrinth.Net/Endpoints/Project/ProjectApi.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Flurl.Http;
22
using Modrinth.Extensions;
33
using Modrinth.Models;
4+
using Modrinth.Models.Facets;
45
using Index = Modrinth.Models.Enums.Index;
56

67
namespace Modrinth.Endpoints.Project;
@@ -64,13 +65,19 @@ public async Task UnfollowAsync(string slugOrId)
6465

6566
/// <inheritdoc />
6667
public async Task<SearchResponse> SearchAsync(string query, Index index = Index.Downloads, ulong offset = 0,
67-
ulong limit = 10)
68+
ulong limit = 10, FacetCollection? facets = null)
6869
{
69-
return await _client.Request("search")
70+
var request = _client.Request("search")
7071
.SetQueryParam("query", query)
7172
.SetQueryParam("index", index.ToString().ToLower())
7273
.SetQueryParam("offset", offset)
73-
.SetQueryParam("limit", limit)
74-
.GetJsonAsync<SearchResponse>();
74+
.SetQueryParam("limit", limit);
75+
76+
if (facets is {Count: > 0})
77+
{
78+
request = request.SetQueryParam("facets", facets.ToString());
79+
}
80+
81+
return await request.GetJsonAsync<SearchResponse>();
7582
}
7683
}

Modrinth.Net/Models/Facets/Facet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public override string ToString()
2323
{
2424
return Type switch
2525
{
26-
FacetType.Categories => $"category:{Value}",
27-
FacetType.Versions => $"version:{Value}",
26+
FacetType.Categories => $"categories:{Value}",
27+
FacetType.Versions => $"versions:{Value}",
2828
FacetType.License => $"license:{Value}",
2929
FacetType.ProjectType => $"project_type:{Value}",
3030
_ => string.Empty

Modrinth.Net/Models/Facets/FacetCollection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ namespace Modrinth.Models.Facets;
22

33
public class FacetCollection
44
{
5+
public int Count => _facets.Count;
6+
57
private readonly List<Facet[]> _facets = new();
68

79
/// <summary>

0 commit comments

Comments
 (0)