Return Array of KeyValuePairs instead of Dictionary (so it does not appear to be an object)

This commit is contained in:
glax 2025-05-26 19:07:48 +02:00
parent e11390f632
commit 227346e096

View File

@ -10,16 +10,17 @@ public class TimeTrackController(Context databaseContext) : ApiController(typeof
{
[HttpGet("{steamId}")]
[ProducesResponseType<Dictionary<ulong, TrackedTime[]>>(Status200OK)]
[ProducesResponseType<KeyValuePair<ulong, TrackedTime[]>[]>(Status200OK)]
[ProducesResponseType(Status404NotFound)]
public IActionResult GetTrackedTime(ulong steamId)
{
if (databaseContext.Players.Find(steamId) is not { } player)
return NotFound();
databaseContext.Entry(player).Collection(p => p.TrackedTimes).Load();
Dictionary<ulong, TrackedTime[]> ret = player.TrackedTimes
KeyValuePair<ulong, TrackedTime[]>[] ret = player.TrackedTimes
.GroupBy(t => t.Game)
.ToDictionary(t => t.Key.AppId, t => t.ToArray());
.Select(t => new KeyValuePair<ulong, TrackedTime[]>(t.Key.AppId, t.ToArray()))
.ToArray();
return Ok(ret);
}
@ -54,16 +55,17 @@ public class TimeTrackController(Context databaseContext) : ApiController(typeof
}
[HttpGet("{steamId}/Total/PerGame")]
[ProducesResponseType<Dictionary<ulong, ulong>>(Status200OK)]
[ProducesResponseType<KeyValuePair<ulong, ulong>[]>(Status200OK)]
[ProducesResponseType(Status404NotFound)]
public IActionResult GetTrackedTimeAll(ulong steamId)
{
if (databaseContext.Players.Find(steamId) is not { } player)
return NotFound();
databaseContext.Entry(player).Collection(p => p.TrackedTimes).Load();
Dictionary<ulong, ulong> trackedTimes = player.TrackedTimes
KeyValuePair<ulong, ulong>[] trackedTimes = player.TrackedTimes
.GroupBy(t => t.Game)
.ToDictionary(t => t.Key.AppId, t => t.MaxBy(time => time.TimePlayed)?.TimePlayed??0);
.Select(t => new KeyValuePair<ulong, ulong>(t.Key.AppId, t.MaxBy(time => time.TimePlayed)?.TimePlayed ?? 0))
.ToArray();
return Ok(trackedTimes);
}