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

Add argument for safety checks to CopyTournamentArgs #174

Merged
merged 1 commit into from
May 31, 2024
Merged
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
24 changes: 23 additions & 1 deletion TournamentManager/TournamentManager/TournamentCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class TournamentCreator
/// <param name="TargetName">The name for the target tournament.</param>
/// <param name="TargetDescription">The description for the target tournament.</param>
/// <param name="TargetLegDates">The leg dates to use for the target tournament round legs. The list cover maximum number of legs across akk rounds.</param>
/// <param name="RoundsToExclude">The round IDs to exclude from copy.</param>
/// <param name="DisableChecks">Disable safety checks.</param>
/// <param name="ModifiedOn">The <see cref="DateTime"/> to use for CreatedOn / ModifiedOn of entities.</param>
public record CopyTournamentArgs(
long SourceTournamentId,
Expand All @@ -28,6 +30,7 @@ public record CopyTournamentArgs(
string? TargetDescription,
IList<(DateTime Start, DateTime End)> TargetLegDates,
IList<long> RoundsToExclude,
bool DisableChecks,
DateTime ModifiedOn);

private readonly ILogger<TournamentCreator> _logger;
Expand Down Expand Up @@ -64,13 +67,14 @@ public static TournamentCreator Instance(IAppDb appDb, ILogger<TournamentCreator
/// new List&lt;(DateTime Start, DateTime End)&gt; {
/// (new DateTime(2024, 9, 23), new DateTime(2025, 2, 1)), // 1st leg
/// (new DateTime(2025, 2, 3), new DateTime(2025, 5, 30)) // 2nd leg
/// }, Array.Empty&lt;long&gt;(), DateTime.UtcNow);
/// }, Array.Empty&lt;long&gt;(), false, DateTime.UtcNow);
///
/// var success = await TournamentCreator
/// .Instance(AppDb, AppLogging.CreateLogger&lt;TournamentCreator&gt;())
/// .CreateNewFromSourceTournament(copyArgs, CancellationToken.None);
/// </code>
/// <returns><see langword="true"/>, if the new tournament was created successfully.</returns>
/// <exception cref="InvalidOperationException"></exception>
public async Task<bool> CreateNewFromSourceTournament(CopyTournamentArgs copyArgs, CancellationToken cancellationToken)
{
if (copyArgs.SetSourceTournamentCompleted) await SetTournamentCompleted(copyArgs.SourceTournamentId, cancellationToken);
Expand Down Expand Up @@ -101,6 +105,7 @@ public async Task<bool> CreateNewFromSourceTournament(CopyTournamentArgs copyArg
/// <param name="copyArgs">The <see cref="CopyTournamentArgs"/> to be used.</param>
/// <param name="cancellationToken"></param>
/// <returns>A <see cref="ValueTuple"/> with the Source (unchanged) and the new Target <see cref="TournamentEntity"/>.</returns>
/// <exception cref="InvalidOperationException"></exception>
internal async Task<(TournamentEntity Source, TournamentEntity Target)> CopyTournament (CopyTournamentArgs copyArgs, CancellationToken cancellationToken)
{
_modifiedOn = copyArgs.ModifiedOn;
Expand All @@ -109,6 +114,23 @@ await _appDb.TournamentRepository.GetTournamentAsync(
new PredicateExpression(TournamentFields.Id == copyArgs.SourceTournamentId), CancellationToken.None)
?? throw new InvalidOperationException($"'{copyArgs.SourceTournamentId}' not found.");

if(copyArgs.DisableChecks)
{
_logger.LogWarning("Safety checks disabled.");
}
else
{
if (sourceTournament.IsPlanningMode)
{
throw new InvalidOperationException($"Source tournament {sourceTournament.Id} is in planning mode.");
}

if (sourceTournament.NextTournamentId.HasValue)
{
throw new InvalidOperationException($"Source tournament {sourceTournament.Id} has already a next tournament.");
}
}

// Create the target tournament
var targetTournament = new TournamentEntity
{
Expand Down