From b122b974d700e6da001f074e7ff1a9dd119de43f Mon Sep 17 00:00:00 2001 From: Alex Hesketh Date: Thu, 22 Feb 2024 14:36:39 +0000 Subject: [PATCH] Adjust Stats output to hide from overall lists if checked Person now has a default colour --- API.Data/Faction.cs | 1 + API.Data/Person.cs | 1 + API.Data/Player.cs | 1 + API.Data/Post/Person.cs | 1 + API.Data/Put/Person.cs | 8 ++++ API/Controllers/PersonController.cs | 61 ++++++++++++++++++++++++++--- API/Controllers/StatsController.cs | 46 ++++++++++------------ API/Mapping/Put.cs | 1 + 8 files changed, 89 insertions(+), 31 deletions(-) create mode 100644 API.Data/Put/Person.cs diff --git a/API.Data/Faction.cs b/API.Data/Faction.cs index fb02971..d592a14 100644 --- a/API.Data/Faction.cs +++ b/API.Data/Faction.cs @@ -3,5 +3,6 @@ public sealed class Faction : IEntity { public string Name { get; set; } = null!; + public string? Link { get; set; } = null; public Guid Identifier { get; set; } } \ No newline at end of file diff --git a/API.Data/Person.cs b/API.Data/Person.cs index 15d1ca9..0f0dad6 100644 --- a/API.Data/Person.cs +++ b/API.Data/Person.cs @@ -5,6 +5,7 @@ namespace Hesketh.MecatolArchives.API.Data; public sealed class Person : IEntity { public string Name { get; set; } = null!; + public Colour? DefaultColour { get; set; } = null; public Guid Identifier { get; set; } public string Initials diff --git a/API.Data/Player.cs b/API.Data/Player.cs index 78075e5..bc5e94c 100644 --- a/API.Data/Player.cs +++ b/API.Data/Player.cs @@ -5,6 +5,7 @@ public sealed class Player : IEntity public uint Points { get; set; } = 0; public bool Winner { get; set; } = false; public bool Eliminated { get; set; } = false; + public uint DraftOrder { get; set; } = 0; public Person Person { get; set; } = null!; public Faction Faction { get; set; } = null!; diff --git a/API.Data/Post/Person.cs b/API.Data/Post/Person.cs index d79a92f..fa2bc92 100644 --- a/API.Data/Post/Person.cs +++ b/API.Data/Post/Person.cs @@ -3,4 +3,5 @@ public sealed class Person { public string Name { get; set; } = null!; + public Guid? DefaultColourId { get; set; } = null; } \ No newline at end of file diff --git a/API.Data/Put/Person.cs b/API.Data/Put/Person.cs new file mode 100644 index 0000000..fc11274 --- /dev/null +++ b/API.Data/Put/Person.cs @@ -0,0 +1,8 @@ +namespace Hesketh.MecatolArchives.API.Data.Put; + +public sealed class Person +{ + public Guid Identifier { get; set; } + public string Name { get; set; } = null!; + public Guid? DefaultColourId { get; set; } = null; +} \ No newline at end of file diff --git a/API/Controllers/PersonController.cs b/API/Controllers/PersonController.cs index c328d6d..d61154d 100644 --- a/API/Controllers/PersonController.cs +++ b/API/Controllers/PersonController.cs @@ -4,6 +4,7 @@ using Hesketh.MecatolArchives.DB.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; namespace Hesketh.MecatolArchives.API.Controllers; @@ -12,10 +13,14 @@ namespace Hesketh.MecatolArchives.API.Controllers; [Authorize] public sealed class PersonController : ControllerBase { + private readonly MecatolArchivesDbContext _db; + private readonly IMapper _mapper; private readonly CrudControllerHelper _crud; public PersonController(MecatolArchivesDbContext db, IMapper mapper) { + _db = db; + _mapper = mapper; _crud = new CrudControllerHelper(db, mapper); } @@ -23,26 +28,72 @@ public PersonController(MecatolArchivesDbContext db, IMapper mapper) [AllowAnonymous] public async Task> Get(Guid identifier) { - return await _crud.GetAsync(identifier); + var person = await _db.People.Include(x => x.DefaultColour) + .FirstOrDefaultAsync(x => x.Identifier == identifier); + if (person == null) + return NotFound(); + + return _mapper.Map(person); } [HttpGet] [AllowAnonymous] public async Task>> Get() { - return await _crud.GetAsync(); + var res = await _db.GetDbSet() + .Include(x => x.DefaultColour) + .OrderBy(x => x.Name) + .ToListAsync(); + + var mapped = _mapper.Map>(res); + return Ok(mapped); } [HttpPost] public async Task> Post(Data.Post.Person model) { - return await _crud.PostAsync(model); + var person = _mapper.Map(model); + if (model.DefaultColourId != null) + { + var colour = await _db.Colours.FirstOrDefaultAsync(x => x.Identifier == person.Identifier); + if (colour == null) + return NotFound("The colour could not be found"); + + person.DefaultColour = colour; + } + + await _db.SaveChangesAsync(); + + var mapped = _mapper.Map(person); + return Ok(mapped); } [HttpPut] - public async Task> Put(Data.Person model) + public async Task> Put(DTPut.Person model) { - return await _crud.PutAsync(model); + var person = await _db.People.Include(x => x.DefaultColour) + .FirstOrDefaultAsync(x => x.Identifier == model.Identifier); + if (person == null) + return NotFound(); + + _mapper.Map(model, person); + if (model.DefaultColourId != null) + { + var colour = await _db.Colours.FirstOrDefaultAsync(x => x.Identifier == model.DefaultColourId); + if (colour == null) + return NotFound("The colour could not be found"); + + person.DefaultColour = colour; + } + else + { + person.DefaultColour = null; + } + + await _db.SaveChangesAsync(); + + var mapped = _mapper.Map(person); + return Ok(mapped); } [HttpDelete("{identifier}")] diff --git a/API/Controllers/StatsController.cs b/API/Controllers/StatsController.cs index 165237b..d4e35c1 100644 --- a/API/Controllers/StatsController.cs +++ b/API/Controllers/StatsController.cs @@ -20,9 +20,9 @@ public StatsController(MecatolArchivesDbContext db) public ActionResult GetPlayersFactions(Guid personIdentifier) { var overall = new Statistics(); - + var statsLookup = new Dictionary(); - foreach (var faction in _db.Factions.Where(x => x.Name != MecatolArchivesDbContext.UnknownName)) + foreach (var faction in _db.Factions) { statsLookup[faction.Identifier] = new Statistic(faction.Name, faction.Identifier); } @@ -34,21 +34,18 @@ public ActionResult GetPlayersFactions(Guid personIdentifier) { foreach (var player in play.Players.Where(x => x.Person.Identifier == personIdentifier)) { - if (!statsLookup.TryGetValue(player.Faction.Identifier, out var stats)) + if (statsLookup.TryGetValue(player.Faction.Identifier, out var stats)) { - stats = new Statistic(player.Faction.Name, player.Faction.Identifier); - statsLookup[player.Faction.Identifier] = stats; - } + stats.Plays += 1; - stats.Plays += 1; + if (player.Winner) + { + stats.Wins += 1; + } - if (player.Winner) - { - stats.Wins += 1; + var pointPercentage = (double)player.Points / play.PointGoal; + stats.PointPercentSum += pointPercentage; } - - var pointPercentage = (double)player.Points / play.PointGoal; - stats.PointPercentSum += pointPercentage; } } @@ -66,7 +63,7 @@ public ActionResult GetFactionStats() var overall = new Statistics(); var statsLookup = new Dictionary(); - foreach (var faction in _db.Factions.Where(x => x.Name != MecatolArchivesDbContext.UnknownName)) + foreach (var faction in _db.Factions.Where(x => !x.HideFromStatistics)) { statsLookup[faction.Identifier] = new Statistic(faction.Name, faction.Identifier); } @@ -77,21 +74,18 @@ public ActionResult GetFactionStats() { foreach (var player in play.Players) { - if (!statsLookup.TryGetValue(player.Faction.Identifier, out var stats)) + if (statsLookup.TryGetValue(player.Faction.Identifier, out var stats)) { - stats = new Statistic(player.Faction.Name, player.Faction.Identifier); - statsLookup[player.Faction.Identifier] = stats; - } + stats.Plays += 1; - stats.Plays += 1; + if (player.Winner) + { + stats.Wins += 1; + } - if (player.Winner) - { - stats.Wins += 1; + var pointPercentage = (double)player.Points / play.PointGoal; + stats.PointPercentSum += pointPercentage; } - - var pointPercentage = (double)player.Points / play.PointGoal; - stats.PointPercentSum += pointPercentage; } } @@ -113,7 +107,7 @@ public ActionResult GetPeopleStats() .ThenInclude(x => x.Person) .Where(x => x.Players.Any(y => y.Winner))) { - foreach (var player in play.Players) + foreach (var player in play.Players.Where(x => !x.Person.HideFromStatistics)) { if (!statsLookup.TryGetValue(player.Person.Identifier, out var stats)) { diff --git a/API/Mapping/Put.cs b/API/Mapping/Put.cs index 10653a0..f624c4d 100644 --- a/API/Mapping/Put.cs +++ b/API/Mapping/Put.cs @@ -8,5 +8,6 @@ public class Put : Profile public Put() { CreateMap(); + CreateMap(); } } \ No newline at end of file