Skip to content

Commit c17fe04

Browse files
committed
make facet class generic to allow more types in the future
1 parent 7208f34 commit c17fe04

File tree

3 files changed

+46
-32
lines changed

3 files changed

+46
-32
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Modrinth.Models.Enums;
2+
3+
namespace Modrinth.Extensions;
4+
5+
public static class ProjectTypeExtensions
6+
{
7+
/// <summary>
8+
/// Convert ProjectType to string for Modrinth API
9+
/// </summary>
10+
/// <param name="projectType"></param>
11+
/// <returns></returns>
12+
public static string ToModrinthString(this ProjectType projectType)
13+
{
14+
return projectType switch
15+
{
16+
ProjectType.Mod => "mod",
17+
ProjectType.Modpack => "modpack",
18+
ProjectType.Resourcepack => "resourcepack",
19+
ProjectType.Shader => "shader",
20+
// Return lower string, this should work for all, but it is not guaranteed
21+
_ => projectType.ToString().ToLower()
22+
};
23+
}
24+
}
Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using Modrinth.Models;
2-
using Modrinth.Models.Enums;
1+
using Modrinth.Extensions;
2+
using Modrinth.Models;
33

44
namespace Modrinth.Helpers;
55

@@ -13,23 +13,7 @@ public static class UrlCreatorHelper
1313
/// </summary>
1414
public const string ModrinthUrl = "https://modrinth.com";
1515

16-
/// <summary>
17-
/// Returns formatted type used in Modrinth links to specific project
18-
/// </summary>
19-
/// <param name="projectType"></param>
20-
/// <returns></returns>
21-
private static string GetProjectUrlType(ProjectType projectType)
22-
{
23-
return projectType switch
24-
{
25-
ProjectType.Mod => "mod",
26-
ProjectType.Modpack => "modpack",
27-
ProjectType.Resourcepack => "resourcepack",
28-
ProjectType.Shader => "shader",
29-
// Return lower string, this should work for all, but it is not guaranteed
30-
_ => projectType.ToString().ToLower()
31-
};
32-
}
16+
3317

3418
/// <summary>
3519
/// Return direct link to the user on Modrinth
@@ -38,7 +22,7 @@ private static string GetProjectUrlType(ProjectType projectType)
3822
/// <returns></returns>
3923
public static string GetDirectUrl(this Project project)
4024
{
41-
return $"{ModrinthUrl}/{GetProjectUrlType(project.ProjectType)}/{project.Id}";
25+
return $"{ModrinthUrl}/{project.ProjectType.ToModrinthString()}/{project.Id}";
4226
}
4327

4428
/// <summary>
@@ -58,6 +42,6 @@ public static string GetDirectUrl(this User user)
5842
/// <returns></returns>
5943
public static string GetDirectUrl(this SearchResult searchResult)
6044
{
61-
return $"{ModrinthUrl}/{GetProjectUrlType(searchResult.ProjectType)}/{searchResult.ProjectId}";
45+
return $"{ModrinthUrl}/{searchResult.ProjectType.ToModrinthString()}/{searchResult.ProjectId}";
6246
}
6347
}

Modrinth.Net/Models/Facets/Facet.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1+
using Modrinth.Extensions;
2+
using Modrinth.Models.Enums;
3+
14
namespace Modrinth.Models.Facets;
25

3-
public class Facet
6+
public abstract class Facet
7+
{
8+
public static Facet<string> Category(string value) => new (FacetType.Categories, value);
9+
10+
public static Facet<string> Version(string value) => new(FacetType.Versions, value);
11+
12+
public static Facet<string> License(string value) => new(FacetType.License, value);
13+
14+
public static Facet<string> ProjectType(ProjectType projectType) => new(FacetType.ProjectType, projectType.ToModrinthString());
15+
}
16+
17+
public class Facet<T> : Facet
418
{
519
public FacetType Type { get; }
6-
public string Value { get; }
20+
public T Value { get; }
721

8-
private Facet(FacetType type, string value)
22+
public Facet(FacetType type, T value)
923
{
1024
Type = type;
1125
Value = value;
1226
}
13-
14-
public static Facet Category(string value) => new(FacetType.Categories, value);
15-
16-
public static Facet Version(string value) => new(FacetType.Versions, value);
17-
18-
public static Facet License(string value) => new(FacetType.License, value);
19-
20-
public static Facet ProjectType(string value) => new(FacetType.ProjectType, value);
2127

2228
public override string ToString()
2329
{

0 commit comments

Comments
 (0)