Refactor !rank, add HighscoreManager and add /v1/highscore to the api
This commit is contained in:
parent
a5c70859a4
commit
99245b9ead
15 changed files with 261 additions and 109 deletions
7
Geekbot.net/WebApi/ApiError.cs
Normal file
7
Geekbot.net/WebApi/ApiError.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
namespace Geekbot.net.WebApi
|
||||
{
|
||||
public class ApiError
|
||||
{
|
||||
public string Message { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
using System.Collections.Generic;
|
||||
using Geekbot.net.Lib.Highscores;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Geekbot.net.WebApi.Controllers.Highscores
|
||||
{
|
||||
[EnableCors("AllowSpecificOrigin")]
|
||||
public class HighscoreController : Controller
|
||||
{
|
||||
private readonly IHighscoreManager _highscoreManager;
|
||||
|
||||
public HighscoreController(IHighscoreManager highscoreManager)
|
||||
{
|
||||
_highscoreManager = highscoreManager;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("/v1/highscore")]
|
||||
public IActionResult GetHighscores([FromBody] HighscoreControllerPostBodyDto body)
|
||||
{
|
||||
if (!ModelState.IsValid || body == null)
|
||||
{
|
||||
var error = new SerializableError(ModelState);
|
||||
return BadRequest(error);
|
||||
}
|
||||
|
||||
Dictionary<HighscoreUserDto, int> list;
|
||||
try
|
||||
{
|
||||
list = _highscoreManager.GetHighscoresWithUserData(body.Type, body.GuildId, body.Amount);
|
||||
}
|
||||
catch (HighscoreListEmptyException)
|
||||
{
|
||||
return NotFound(new ApiError
|
||||
{
|
||||
Message = $"No {body.Type} found on this server"
|
||||
});
|
||||
}
|
||||
|
||||
var response = new List<HighscoreControllerReponseBody>();
|
||||
var counter = 1;
|
||||
foreach (var item in list)
|
||||
{
|
||||
response.Add(new HighscoreControllerReponseBody
|
||||
{
|
||||
count = item.Value,
|
||||
rank = counter,
|
||||
user = item.Key
|
||||
});
|
||||
counter++;
|
||||
}
|
||||
return Ok(response);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
using Geekbot.net.Lib.Highscores;
|
||||
|
||||
namespace Geekbot.net.WebApi.Controllers.Highscores
|
||||
{
|
||||
public class HighscoreControllerPostBodyDto
|
||||
{
|
||||
[Required]
|
||||
public ulong GuildId { get; set; }
|
||||
|
||||
public HighscoreTypes Type { get; set; } = HighscoreTypes.messages;
|
||||
|
||||
[Range(1, 150)]
|
||||
public int Amount { get; set; } = 50;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
using Geekbot.net.Lib.Highscores;
|
||||
|
||||
namespace Geekbot.net.WebApi.Controllers.Highscores
|
||||
{
|
||||
public class HighscoreControllerReponseBody
|
||||
{
|
||||
public int rank { get; set; }
|
||||
public HighscoreUserDto user { get; set; }
|
||||
public int count { get; set; }
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ using Discord.WebSocket;
|
|||
using Geekbot.net.Database;
|
||||
using Geekbot.net.Lib;
|
||||
using Geekbot.net.Lib.GlobalSettings;
|
||||
using Geekbot.net.Lib.Highscores;
|
||||
using Geekbot.net.Lib.Logger;
|
||||
using Geekbot.net.WebApi.Logging;
|
||||
using Microsoft.AspNetCore;
|
||||
|
@ -18,7 +19,7 @@ namespace Geekbot.net.WebApi
|
|||
public static class WebApiStartup
|
||||
{
|
||||
public static void StartWebApi(IGeekbotLogger logger, RunParameters runParameters, CommandService commandService,
|
||||
DatabaseContext databaseContext, DiscordSocketClient client, IGlobalSettings globalSettings)
|
||||
DatabaseContext databaseContext, DiscordSocketClient client, IGlobalSettings globalSettings, IHighscoreManager highscoreManager)
|
||||
{
|
||||
WebHost.CreateDefaultBuilder()
|
||||
.UseKestrel(options =>
|
||||
|
@ -32,6 +33,7 @@ namespace Geekbot.net.WebApi
|
|||
services.AddSingleton<DatabaseContext>(databaseContext);
|
||||
services.AddSingleton<DiscordSocketClient>(client);
|
||||
services.AddSingleton<IGlobalSettings>(globalSettings);
|
||||
services.AddSingleton<IHighscoreManager>(highscoreManager);
|
||||
services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowSpecificOrigin",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue