This commit is contained in:
2025-05-26 01:44:26 +02:00
parent ebddd3c3ed
commit 1a08f932af
23 changed files with 526 additions and 256 deletions

View File

@ -0,0 +1,84 @@
using Microsoft.AspNetCore.Mvc;
using SQLiteEF;
using static Microsoft.AspNetCore.Http.StatusCodes;
namespace API.Controllers;
[ApiController]
[Route("[controller]")]
public class ActionsController(Context databaseContext) : ApiController(typeof(ActionsController))
{
[HttpPut("Player/{steamId}")]
[ProducesResponseType<Player>(Status202Accepted)]
[ProducesResponseType<string>(Status500InternalServerError)]
public IActionResult AddPlayerToTrack(Tracker tracker, ulong steamId)
{
Player nPlayer = new (steamId, "", "", "");
try
{
databaseContext.Players.Add(nPlayer);
databaseContext.SaveChanges();
tracker.ForceLoop();
return Accepted(nPlayer);
}
catch (Exception e)
{
Log.Error(e);
return StatusCode(Status500InternalServerError);
}
}
[HttpPost("Update/Player/{steamId}/All")]
[ProducesResponseType(Status200OK)]
[ProducesResponseType(Status404NotFound)]
public IActionResult UpdatePlayer(Tracker tracker, ulong steamId)
{
if (databaseContext.Players.Find(steamId) is not { } player)
return NotFound();
tracker.UpdatePlayer(databaseContext, player);
tracker.UpdateOwnedGamesPlayer(databaseContext, player);
tracker.UpdateGameTimesPlayer(databaseContext, player);
return Ok();
}
[HttpPost("Update/Player/{steamId}/Info")]
[ProducesResponseType(Status200OK)]
[ProducesResponseType(Status404NotFound)]
public IActionResult UpdatePlayerInfo(Tracker tracker, ulong steamId)
{
if (databaseContext.Players.Find(steamId) is not { } player)
return NotFound();
tracker.UpdatePlayer(databaseContext, player);
return Ok();
}
[HttpPost("Update/Player/{steamId}/OwnedGames")]
[ProducesResponseType(Status200OK)]
[ProducesResponseType(Status404NotFound)]
public IActionResult UpdatePlayerOwnedGames(Tracker tracker, ulong steamId)
{
if (databaseContext.Players.Find(steamId) is not { } player)
return NotFound();
tracker.UpdateOwnedGamesPlayer(databaseContext, player);
return Ok();
}
[HttpPost("Update/Player/{steamId}/TimeTracked")]
[ProducesResponseType(Status200OK)]
[ProducesResponseType(Status404NotFound)]
public IActionResult UpdatePlayerTimeTracked(Tracker tracker, ulong steamId)
{
if (databaseContext.Players.Find(steamId) is not { } player)
return NotFound();
tracker.UpdateGameTimesPlayer(databaseContext, player);
return Ok();
}
[HttpPost("Update/All")]
[ProducesResponseType(Status202Accepted)]
public IActionResult Update(Tracker tracker)
{
tracker.ForceLoop();
return Accepted();
}
}

View File

@ -0,0 +1,9 @@
using log4net;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
public abstract class ApiController(Type type) : Controller
{
protected ILog Log { get; init; } = LogManager.GetLogger(type);
}

View File

@ -0,0 +1,66 @@
using Microsoft.AspNetCore.Mvc;
using SQLiteEF;
using static Microsoft.AspNetCore.Http.StatusCodes;
namespace API.Controllers;
[ApiController]
[Route("[controller]")]
public class DataController(Context databaseContext) : ApiController(typeof(DataController))
{
[HttpGet("Players")]
[ProducesResponseType<Player[]>(Status200OK)]
public IActionResult GetPlayers()
{
return Ok(databaseContext.Players.ToArray());
}
[HttpGet("Player/{steamId}")]
[ProducesResponseType<Player>(Status200OK)]
[ProducesResponseType(Status404NotFound)]
public IActionResult GetPlayer(ulong steamId)
{
if (databaseContext.Players.Find(steamId) is not { } player)
return NotFound();
return Ok(player);
}
[HttpGet("Player/{steamId}/Games")]
[ProducesResponseType<Game[]>(Status200OK)]
[ProducesResponseType(Status404NotFound)]
public IActionResult GetGamesPlayedByPlayer(ulong steamId)
{
if (databaseContext.Players.Find(steamId) is not { } player)
return NotFound();
databaseContext.Entry(player).Collection(p => p.Games).Load();
return Ok(player.Games.ToArray());
}
[HttpGet("Games")]
[ProducesResponseType<Game[]>(Status200OK)]
public IActionResult GetGames()
{
return Ok(databaseContext.Games.ToArray());
}
[HttpGet("Game/{appId}")]
[ProducesResponseType<Game>(Status200OK)]
[ProducesResponseType(Status404NotFound)]
public IActionResult GetGame(ulong appId)
{
if (databaseContext.Games.Find(appId) is not { } game)
return NotFound();
return Ok(game);
}
[HttpGet("Game/{appId}/Players")]
[ProducesResponseType<Player[]>(Status200OK)]
[ProducesResponseType(Status404NotFound)]
public IActionResult GetPlayersPlayingGame(ulong appId)
{
if (databaseContext.Games.Find(appId) is not { } game)
return NotFound();
databaseContext.Entry(game).Collection(g => g.PlayedBy).Load();
return Ok(game.PlayedBy.ToArray());
}
}

View File

@ -0,0 +1,55 @@
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}/{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}/PerGame")]
[ProducesResponseType<Dictionary<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
.GroupBy(t => t.Game)
.ToDictionary(t => t.Key.AppId, t => t.MaxBy(time => time.TimePlayed)?.TimePlayed??0);
return Ok(trackedTimes);
}
}