-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from FIRSTinMI/event-teams
Event teams
- Loading branch information
Showing
10 changed files
with
202 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace FiMAdminApi.Data.Models; | ||
|
||
public class EventTeam | ||
{ | ||
public int Id { get; set; } | ||
public required Guid EventId { get; set; } | ||
public virtual Event? Event { get; set; } | ||
public required int TeamNumber { get; set; } | ||
public required int LevelId { get; set; } | ||
public virtual Level? Level { get; set; } | ||
public string? Notes { get; set; } | ||
public required string StatusId { get; set; } | ||
public virtual EventTeamStatus? Status { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace FiMAdminApi.Data.Models; | ||
|
||
public class EventTeamStatus | ||
{ | ||
/// <summary> | ||
/// Unique identifier for the status, but still understandable by humans | ||
/// </summary> | ||
[Key] | ||
public required string Id { get; set; } | ||
|
||
public required string Name { get; set; } | ||
|
||
/// <summary> | ||
/// Higher numbers are teams closer to ready for matches | ||
/// </summary> | ||
public required int Ordinal { get; set; } | ||
} | ||
|
||
public static class KnownEventTeamStatuses | ||
{ | ||
public const string Dropped = "Dropped"; | ||
public const string NotArrived = "NotArrived"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using FiMAdminApi.Clients; | ||
using FiMAdminApi.Data.Models; | ||
using FiMAdminApi.Services; | ||
|
||
namespace FiMAdminApi.EventSync.Steps; | ||
|
||
public class PopulateEventTeams(EventTeamsService teamsService) : EventSyncStep([EventStatus.NotStarted]) | ||
{ | ||
public override async Task RunStep(Event evt, IDataClient eventDataClient) | ||
{ | ||
await teamsService.UpsertEventTeams(evt); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using FiMAdminApi.Clients; | ||
using FiMAdminApi.Data; | ||
using FiMAdminApi.Data.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace FiMAdminApi.Services; | ||
|
||
public class EventTeamsService(DataContext dbContext, IServiceProvider serviceProvider) | ||
{ | ||
public async Task UpsertEventTeams(Event evt) | ||
{ | ||
var dataClient = serviceProvider.GetKeyedService<IDataClient>(evt.SyncSource); | ||
|
||
if (evt.Code is null || dataClient is null) | ||
{ | ||
throw new ApplicationException("Unable to get data client for event"); | ||
} | ||
|
||
if (evt.Season is null) | ||
{ | ||
throw new ApplicationException("Season must be included to populate teams"); | ||
} | ||
|
||
var existingTeams = await dbContext.EventTeams.Where(t => t.EventId == evt.Id).ToListAsync(); | ||
var apiTeams = await dataClient.GetTeamsForEvent(evt.Season, evt.Code); | ||
|
||
// Delete removed teams | ||
var removedTeamNumbers = existingTeams.Select(et => et.TeamNumber) | ||
.Except(apiTeams.Select(at => at.TeamNumber)); | ||
await dbContext.EventTeams.Where(t => t.EventId == evt.Id && removedTeamNumbers.Contains(t.TeamNumber)) | ||
.ExecuteUpdateAsync(b => b.SetProperty(t => t.StatusId, KnownEventTeamStatuses.Dropped)); | ||
|
||
// Insert new teams | ||
var addedTeamNumbers = apiTeams.ExceptBy(existingTeams.Select(et => et.TeamNumber), at => at.TeamNumber); | ||
await dbContext.EventTeams.AddRangeAsync(addedTeamNumbers.Select(at => new EventTeam | ||
{ | ||
EventId = evt.Id, | ||
TeamNumber = at.TeamNumber, | ||
LevelId = evt.Season.LevelId, | ||
Notes = null, | ||
StatusId = KnownEventTeamStatuses.NotArrived | ||
})); | ||
await dbContext.SaveChangesAsync(); | ||
} | ||
} |