Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various Enhancements #35

Merged
merged 4 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion API.Client/Clients/PersonClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Hesketh.MecatolArchives.API.Client.Clients;

public sealed class PersonClient : EntityClient<Person, Person, Post.Person>
public sealed class PersonClient : EntityClient<Person, Put.Person, Post.Person>
{
public PersonClient(HttpClient client, IAuthHeaderProvider authHeaderProvider) : base(client, authHeaderProvider,
"people")
Expand Down
2 changes: 2 additions & 0 deletions API.Data/Faction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
public sealed class Faction : IEntity
{
public string Name { get; set; } = null!;
public bool HideFromStatistics { get; set; } = false;
public string? Link { get; set; } = null;
public Guid Identifier { get; set; }
}
4 changes: 3 additions & 1 deletion API.Data/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ 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 bool HideFromStatistics { get; set; } = false;

public string Initials
{
get
Expand Down
1 change: 1 addition & 0 deletions API.Data/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand Down
2 changes: 2 additions & 0 deletions API.Data/Post/Faction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
public sealed class Faction
{
public string Name { get; set; } = null!;
public bool HideFromStatistics { get; set; } = false;
public string? Link { get; set; } = null;
}
2 changes: 2 additions & 0 deletions API.Data/Post/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
public sealed class Person
{
public string Name { get; set; } = null!;
public Guid? DefaultColourId { get; set; } = null;
public bool HideFromStatistics { get; set; } = false;
}
4 changes: 3 additions & 1 deletion API.Data/Post/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ public Player(Data.Player model)
Points = model.Points;
Winner = model.Winner;
Eliminated = model.Eliminated;

DraftOrder = model.DraftOrder;

PersonIdentifier = model.Person.Identifier;
FactionIdentifier = model.Faction.Identifier;
ColourIdentifier = model.Colour.Identifier;
}

public uint DraftOrder { get; set; }
public uint Points { get; set; }
public bool Winner { get; set; }
public bool Eliminated { get; set; }
Expand Down
19 changes: 19 additions & 0 deletions API.Data/Put/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Hesketh.MecatolArchives.API.Data.Put;

public sealed class Person : IEntity
{
public Person() {}

public Person(Data.Person person)
{
Identifier = person.Identifier;
Name = person.Name;
DefaultColourId = person.DefaultColour?.Identifier;
HideFromStatistics = person.HideFromStatistics;
}

public Guid Identifier { get; set; }
public string Name { get; set; } = null!;
public bool HideFromStatistics { get; set; } = false;
public Guid? DefaultColourId { get; set; } = null;
}
61 changes: 56 additions & 5 deletions API/Controllers/PersonController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -12,37 +13,87 @@ namespace Hesketh.MecatolArchives.API.Controllers;
[Authorize]
public sealed class PersonController : ControllerBase
{
private readonly MecatolArchivesDbContext _db;
private readonly IMapper _mapper;
private readonly CrudControllerHelper<Person, Data.Person, Data.Person, Data.Post.Person> _crud;

public PersonController(MecatolArchivesDbContext db, IMapper mapper)
{
_db = db;
_mapper = mapper;
_crud = new CrudControllerHelper<Person, Data.Person, Data.Person, Data.Post.Person>(db, mapper);
}

[HttpGet("{identifier}")]
[AllowAnonymous]
public async Task<ActionResult<Data.Person>> 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<DTM.Person>(person);
}

[HttpGet]
[AllowAnonymous]
public async Task<ActionResult<ICollection<Data.Person>>> Get()
{
return await _crud.GetAsync();
var res = await _db.GetDbSet<Person>()
.Include(x => x.DefaultColour)
.OrderBy(x => x.Name)
.ToListAsync();

var mapped = _mapper.Map<ICollection<DTM.Person>>(res);
return Ok(mapped);
}

[HttpPost]
public async Task<ActionResult<Data.Person>> Post(Data.Post.Person model)
{
return await _crud.PostAsync(model);
var person = _mapper.Map<DBM.Person>(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<Data.Person>(person);
return Ok(mapped);
}

[HttpPut]
public async Task<ActionResult<Data.Person>> Put(Data.Person model)
public async Task<ActionResult<Data.Person>> 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<Data.Person>(person);
return Ok(mapped);
}

[HttpDelete("{identifier}")]
Expand Down
46 changes: 20 additions & 26 deletions API/Controllers/StatsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public StatsController(MecatolArchivesDbContext db)
public ActionResult<Statistics> GetPlayersFactions(Guid personIdentifier)
{
var overall = new Statistics();

var statsLookup = new Dictionary<Guid, Statistic>();
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);
}
Expand All @@ -34,21 +34,18 @@ public ActionResult<Statistics> 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;
}
}

Expand All @@ -66,7 +63,7 @@ public ActionResult<Statistics> GetFactionStats()
var overall = new Statistics();

var statsLookup = new Dictionary<Guid, Statistic>();
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);
}
Expand All @@ -77,21 +74,18 @@ public ActionResult<Statistics> 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;
}
}

Expand All @@ -113,7 +107,7 @@ public ActionResult<Statistics> 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))
{
Expand Down
1 change: 1 addition & 0 deletions API/Mapping/Put.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public class Put : Profile
public Put()
{
CreateMap<Play, DB.Models.Play>();
CreateMap<Person, DB.Models.Person>();
}
}
Loading