-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from edgiardina/tournament-event-type
Tournament event type
- Loading branch information
Showing
10 changed files
with
143 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json; | ||
using Humanizer; | ||
|
||
namespace PinballApi.Converters | ||
{ | ||
public class CapitalizedEnumConverter<T> : JsonConverter<T> where T : struct, Enum | ||
{ | ||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var enumValue = reader.GetString(); | ||
|
||
if (enumValue == null) | ||
{ | ||
throw new JsonException($"Unable to convert null to enum {typeof(T)}."); | ||
} | ||
|
||
// Convert the snake_case string (START_DATE) to PascalCase (StartDate) | ||
var pascalCaseValue = enumValue.Pascalize(); | ||
|
||
// Try parsing the enum from the PascalCase string | ||
if (Enum.TryParse(pascalCaseValue, true, out T parsedEnum)) | ||
{ | ||
return parsedEnum; | ||
} | ||
|
||
throw new JsonException($"Unable to convert \"{enumValue}\" to enum {typeof(T)}."); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) | ||
{ | ||
// Convert the enum value (PascalCase) to snake_case (START_DATE) | ||
string enumString = value.ToString(); | ||
if (!string.IsNullOrEmpty(enumString)) | ||
{ | ||
string snakeCase = enumString.Underscore().ToUpper(); // Ensure it's uppercase for consistency | ||
writer.WriteStringValue(snakeCase); | ||
} | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
PinballApi/Converters/TournamentSearchSortOrderConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using PinballApi.Models.WPPR.Universal.Tournaments.Search; | ||
using System; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json; | ||
|
||
namespace PinballApi.Converters | ||
{ | ||
public class TournamentSearchSortOrderConverter : JsonConverter<TournamentSearchSortOrder> | ||
{ | ||
public override TournamentSearchSortOrder Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var enumValue = reader.GetString(); | ||
|
||
if (enumValue == null) | ||
{ | ||
throw new JsonException($"Unable to convert null to enum {typeof(TournamentSearchSortOrder)}."); | ||
} | ||
|
||
// Convert the string to the corresponding enum value | ||
return enumValue.ToUpper() switch | ||
{ | ||
"ASC" => TournamentSearchSortOrder.Ascending, | ||
"DESC" => TournamentSearchSortOrder.Descending, | ||
_ => throw new JsonException($"Unknown value \"{enumValue}\" for enum {typeof(TournamentSearchSortOrder)}.") | ||
}; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, TournamentSearchSortOrder value, JsonSerializerOptions options) | ||
{ | ||
// Convert the enum value to the corresponding string (ASC or DESC) | ||
string stringValue = value switch | ||
{ | ||
TournamentSearchSortOrder.Ascending => "ASC", | ||
TournamentSearchSortOrder.Descending => "DESC", | ||
_ => throw new JsonException($"Unknown enum value {value}.") | ||
}; | ||
|
||
writer.WriteStringValue(stringValue); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
PinballApi/Models/WPPR/Universal/Tournaments/Search/TournamentEventType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace PinballApi.Models.WPPR.Universal.Tournaments.Search | ||
{ | ||
public enum TournamentEventType | ||
{ | ||
Tournament, | ||
League | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 1 addition & 7 deletions
8
PinballApi/Models/WPPR/Universal/Tournaments/Search/TournamentSearchSortOrder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters