Working
This commit is contained in:
84
API/Controllers/ActionsController.cs
Normal file
84
API/Controllers/ActionsController.cs
Normal 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();
|
||||
}
|
||||
}
|
9
API/Controllers/ApiController.cs
Normal file
9
API/Controllers/ApiController.cs
Normal 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);
|
||||
}
|
66
API/Controllers/DataController.cs
Normal file
66
API/Controllers/DataController.cs
Normal 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());
|
||||
}
|
||||
}
|
55
API/Controllers/TimeTrackController.cs
Normal file
55
API/Controllers/TimeTrackController.cs
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user