Refactor !rank, add HighscoreManager and add /v1/highscore to the api

This commit is contained in:
runebaas 2018-09-05 22:55:45 +02:00
parent a5c70859a4
commit 99245b9ead
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
15 changed files with 261 additions and 109 deletions

View file

@ -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);
}
}
}

View file

@ -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;
}
}

View file

@ -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; }
}
}