SteamGameTimeTrack/API/Controllers/TimeTrackController.cs

87 lines
3.4 KiB
C#

using Microsoft.AspNetCore.Mvc;
using SQLiteEF;
using static Microsoft.AspNetCore.Http.StatusCodes;
namespace API.Controllers;
[ApiController]
[Route("[controller]")]
public class TimeTrackController(Context databaseContext) : ApiController(typeof(TimeTrackController))
{
[HttpGet("{steamId}")]
[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();
KeyValuePair<ulong, TrackedTime[]>[] ret = player.TrackedTimes
.GroupBy(t => t.Game)
.Select(t => new KeyValuePair<ulong, TrackedTime[]>(t.Key.AppId, t.ToArray()))
.ToArray();
return Ok(ret);
}
[HttpGet("{steamId}/{appId}")]
[ProducesResponseType<TrackedTime[]>(Status200OK)]
[ProducesResponseType(Status404NotFound)]
public IActionResult GetTrackedTimeForApp(ulong steamId, ulong appId)
{
if (databaseContext.Players.Find(steamId) is not { } player)
return NotFound();
if (databaseContext.Games.Find(appId) is not { } game)
return NotFound();
databaseContext.Entry(player).Collection(p => p.TrackedTimes).Load();
return Ok(player.TrackedTimes.Where(t => t.Game == game).ToArray());
}
[HttpGet("{steamId}/Total")]
[ProducesResponseType<ulong>(Status200OK)]
[ProducesResponseType(Status404NotFound)]
public IActionResult GetTrackedTimeTotal(ulong steamId)
{
if (databaseContext.Players.Find(steamId) is not { } player)
return NotFound();
databaseContext.Entry(player).Collection(p => p.TrackedTimes).Load();
ulong[] times = player.TrackedTimes.GroupBy(t => t.Game)
.Select(t => t.MaxBy(time => time.TimePlayed)?.TimePlayed ?? 0)
.ToArray();
ulong sum = 0;
foreach (ulong time in times)
sum += time;
return Ok(sum);
}
[HttpGet("{steamId}/Total/{appId}")]
[ProducesResponseType<ulong>(Status200OK)]
[ProducesResponseType(Status404NotFound)]
public IActionResult GetTrackedTimeAll(ulong steamId, ulong appId)
{
if (databaseContext.Players.Find(steamId) is not { } player)
return NotFound();
if (databaseContext.Games.Find(appId) is not { } game)
return NotFound();
databaseContext.Entry(player).Collection(p => p.TrackedTimes).Load();
ulong? maxTime = player.TrackedTimes.Where(t => t.Game == game).MaxBy(t => t.TimePlayed)?.TimePlayed;
return maxTime is not null ? Ok(maxTime) : NoContent();
}
[HttpGet("{steamId}/Total/PerGame")]
[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();
KeyValuePair<ulong, ulong>[] trackedTimes = player.TrackedTimes
.GroupBy(t => t.Game)
.Select(t => new KeyValuePair<ulong, ulong>(t.Key.AppId, t.MaxBy(time => time.TimePlayed)?.TimePlayed ?? 0))
.ToArray();
return Ok(trackedTimes);
}
}