Skip to content

Commit

Permalink
Rewrite class TournamentCreator (#173)
Browse files Browse the repository at this point in the history
Rewrite class TournamentCreator

* breaking: Rewrite class TournamentCreator
   * Move all DAL operations to TournamentRepository
   * All operations for adding a new Tournament are inside a Transaction
* chore: Add logging
* feat: Add new methods to TournamentRepository
* breaking: Rename TournamentEntity.Tournament to TournamentEntity.NextTournament
* chore: Move NuGetizer settings to Directory.Build.props
  • Loading branch information
axunonb authored May 31, 2024
1 parent d47f7c7 commit 6d10b7c
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 173 deletions.
2 changes: 2 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<AnalysisLevel>latest</AnalysisLevel>
<Features>strict</Features>
<NoWarn>$(NoWarn);1591</NoWarn>
<!-- If true (default), NuGetizer will apply the shortened revision ID to the package version during the packing process. -->
<UseShortSourceRevisionId>false</UseShortSourceRevisionId>
</PropertyGroup>

</Project>
2 changes: 0 additions & 2 deletions League/League.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ Localizations for English and German are included. The library is in operation o
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
<!-- If true (default), NuGetizer will apply the shortened revision ID to the package version during the packing process. -->
<UseShortSourceRevisionId>false</UseShortSourceRevisionId>
<!-- With dotnet, add parameter: -p:SatelliteResourceLanguages="""en;de""" -->
<SatelliteResourceLanguages>en;de</SatelliteResourceLanguages>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Related Entity | Full description
[Ranking](../../_DefaultGroup/Entities/Ranking.htm) | Ranking.Tournament - Tournament.Rankings (m:1)
[Registration](../../_DefaultGroup/Entities/Registration.htm) | Registration.Tournament - Tournament.Registrations (m:1)
[Round](../../_DefaultGroup/Entities/Round.htm) | Round.Tournament - Tournament.Rounds (m:1)
[Tournament](../../_DefaultGroup/Entities/Tournament.htm) | Tournament.Tournament - Tournament.Tournaments (m:1)
[Tournament](../../_DefaultGroup/Entities/Tournament.htm) | Tournament.NextTournament - Tournament.Tournaments (m:1)
[TournamentType](../../_DefaultGroup/Entities/TournamentType.htm) | Tournament.TournamentType - TournamentType.Tournaments (m:1)

## Fields
Expand Down Expand Up @@ -139,22 +139,22 @@ Setting name | Value
--|--
Navigator property is public | True

#### Rankings (NavigatorCollection)
#### NextTournament (NavigatorSingleValue)
Setting name | Value
--|--
Navigator property is public | True

#### Registrations (NavigatorCollection)
#### Rankings (NavigatorCollection)
Setting name | Value
--|--
Navigator property is public | True

#### Rounds (NavigatorCollection)
#### Registrations (NavigatorCollection)
Setting name | Value
--|--
Navigator property is public | True

#### Tournament (NavigatorSingleValue)
#### Rounds (NavigatorCollection)
Setting name | Value
--|--
Navigator property is public | True
Expand All @@ -171,7 +171,7 @@ Navigator property is public | True

### Attribute definitions per element

#### Tournament (NavigatorSingleValue)
#### NextTournament (NavigatorSingleValue)

* `Browsable($false)`

Expand Down
2 changes: 1 addition & 1 deletion TournamentManager/DAL/Code/Markdown/generalinformation.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
This information is generated from the project `TournamentManager`, using the file `D:\Internet\Websites\League\Src\TournamentManager\DAL\TournamentManager.llblgenproj`.

--|--
Generated on | 20-Apr-2024
Generated on | 31-Mai-2024
Using LLBLGen Pro version | LLBLGen Pro v5.11 RTM (v5.11.1)
Project creator | axuno gGmbH
Project name | TournamentManager
Expand Down
2 changes: 1 addition & 1 deletion TournamentManager/DAL/Code/Markdown/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<h1 class="text-center">TournamentManager Model Documentation</h1>
<p class="text-center">
Generated by <a href="https://www.llblgen.com" target="_blank">LLBLGen Pro v5.11</a><br>
Generated on 20-Apr-2024<br>
Generated on 31-Mai-2024<br>
</p>
<br><br><br><br><br><br><br><br>
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public partial class TournamentEntity : CommonEntityBase
private EntityCollection<RegistrationEntity> _registrations;
private EntityCollection<RoundEntity> _rounds;
private EntityCollection<TournamentEntity> _tournaments;
private TournamentEntity _tournament;
private TournamentEntity _nextTournament;
private TournamentTypeEntity _tournamentType;

// __LLBLGENPRO_USER_CODE_REGION_START PrivateMembers
Expand All @@ -42,8 +42,8 @@ public partial class TournamentEntity : CommonEntityBase
/// <summary>All names of fields mapped onto a relation. Usable for in-memory filtering</summary>
public static partial class MemberNames
{
/// <summary>Member name Tournament</summary>
public static readonly string Tournament = "Tournament";
/// <summary>Member name NextTournament</summary>
public static readonly string NextTournament = "NextTournament";
/// <summary>Member name TournamentType</summary>
public static readonly string TournamentType = "TournamentType";
/// <summary>Member name ExcludeMatchDates</summary>
Expand All @@ -69,7 +69,7 @@ public TournamentEntityStaticMetaData()
AddNavigatorMetaData<TournamentEntity, EntityCollection<RegistrationEntity>>("Registrations", a => a._registrations, (a, b) => a._registrations = b, a => a.Registrations, () => new TournamentRelations().RegistrationEntityUsingTournamentId, typeof(RegistrationEntity), (int)TournamentManager.DAL.EntityType.RegistrationEntity);
AddNavigatorMetaData<TournamentEntity, EntityCollection<RoundEntity>>("Rounds", a => a._rounds, (a, b) => a._rounds = b, a => a.Rounds, () => new TournamentRelations().RoundEntityUsingTournamentId, typeof(RoundEntity), (int)TournamentManager.DAL.EntityType.RoundEntity);
AddNavigatorMetaData<TournamentEntity, EntityCollection<TournamentEntity>>("Tournaments", a => a._tournaments, (a, b) => a._tournaments = b, a => a.Tournaments, () => new TournamentRelations().TournamentEntityUsingNextTournamentId, typeof(TournamentEntity), (int)TournamentManager.DAL.EntityType.TournamentEntity);
AddNavigatorMetaData<TournamentEntity, TournamentEntity>("Tournament", "Tournaments", (a, b) => a._tournament = b, a => a._tournament, (a, b) => a.Tournament = b, TournamentManager.DAL.RelationClasses.StaticTournamentRelations.TournamentEntityUsingIdNextTournamentIdStatic, ()=>new TournamentRelations().TournamentEntityUsingIdNextTournamentId, null, new int[] { (int)TournamentFieldIndex.NextTournamentId }, null, true, (int)TournamentManager.DAL.EntityType.TournamentEntity);
AddNavigatorMetaData<TournamentEntity, TournamentEntity>("NextTournament", "Tournaments", (a, b) => a._nextTournament = b, a => a._nextTournament, (a, b) => a.NextTournament = b, TournamentManager.DAL.RelationClasses.StaticTournamentRelations.TournamentEntityUsingIdNextTournamentIdStatic, ()=>new TournamentRelations().TournamentEntityUsingIdNextTournamentId, null, new int[] { (int)TournamentFieldIndex.NextTournamentId }, null, true, (int)TournamentManager.DAL.EntityType.TournamentEntity);
AddNavigatorMetaData<TournamentEntity, TournamentTypeEntity>("TournamentType", "Tournaments", (a, b) => a._tournamentType = b, a => a._tournamentType, (a, b) => a.TournamentType = b, TournamentManager.DAL.RelationClasses.StaticTournamentRelations.TournamentTypeEntityUsingTypeIdStatic, ()=>new TournamentRelations().TournamentTypeEntityUsingTypeId, null, new int[] { (int)TournamentFieldIndex.TypeId }, null, true, (int)TournamentManager.DAL.EntityType.TournamentTypeEntity);
}
}
Expand Down Expand Up @@ -145,7 +145,7 @@ protected TournamentEntity(SerializationInfo info, StreamingContext context) : b

/// <summary>Creates a new IRelationPredicateBucket object which contains the predicate expression and relation collection to fetch the related entity of type 'Tournament' to this entity.</summary>
/// <returns></returns>
public virtual IRelationPredicateBucket GetRelationInfoTournament() { return CreateRelationInfoForNavigator("Tournament"); }
public virtual IRelationPredicateBucket GetRelationInfoNextTournament() { return CreateRelationInfoForNavigator("NextTournament"); }

/// <summary>Creates a new IRelationPredicateBucket object which contains the predicate expression and relation collection to fetch the related entity of type 'TournamentType' to this entity.</summary>
/// <returns></returns>
Expand Down Expand Up @@ -203,7 +203,7 @@ private void InitClassEmpty(IValidator validator, IEntityFields2 fields)

/// <summary>Creates a new PrefetchPathElement2 object which contains all the information to prefetch the related entities of type 'Tournament' for this entity.</summary>
/// <returns>Ready to use IPrefetchPathElement2 implementation.</returns>
public static IPrefetchPathElement2 PrefetchPathTournament { get { return _staticMetaData.GetPrefetchPathElement("Tournament", CommonEntityBase.CreateEntityCollection<TournamentEntity>()); } }
public static IPrefetchPathElement2 PrefetchPathNextTournament { get { return _staticMetaData.GetPrefetchPathElement("NextTournament", CommonEntityBase.CreateEntityCollection<TournamentEntity>()); } }

/// <summary>Creates a new PrefetchPathElement2 object which contains all the information to prefetch the related entities of type 'TournamentType' for this entity.</summary>
/// <returns>Ready to use IPrefetchPathElement2 implementation.</returns>
Expand Down Expand Up @@ -299,14 +299,14 @@ public virtual System.DateTime ModifiedOn

/// <summary>Gets the EntityCollection with the related entities of type 'TournamentEntity' which are related to this entity via a relation of type '1:n'. If the EntityCollection hasn't been fetched yet, the collection returned will be empty.<br/><br/></summary>
[TypeContainedAttribute(typeof(TournamentEntity))]
public virtual EntityCollection<TournamentEntity> Tournaments { get { return GetOrCreateEntityCollection<TournamentEntity, TournamentEntityFactory>("Tournament", true, false, ref _tournaments); } }
public virtual EntityCollection<TournamentEntity> Tournaments { get { return GetOrCreateEntityCollection<TournamentEntity, TournamentEntityFactory>("NextTournament", true, false, ref _tournaments); } }

/// <summary>Gets / sets related entity of type 'TournamentEntity' which has to be set using a fetch action earlier. If no related entity is set for this property, null is returned..<br/><br/></summary>
[Browsable(false)]
public virtual TournamentEntity Tournament
public virtual TournamentEntity NextTournament
{
get { return _tournament; }
set { SetSingleRelatedEntityNavigator(value, "Tournament"); }
get { return _nextTournament; }
set { SetSingleRelatedEntityNavigator(value, "NextTournament"); }
}

/// <summary>Gets / sets related entity of type 'TournamentTypeEntity' which has to be set using a fetch action earlier. If no related entity is set for this property, null is returned..<br/><br/></summary>
Expand Down Expand Up @@ -388,7 +388,7 @@ public virtual IEntityRelation TournamentEntityUsingNextTournamentId
/// <summary>Returns a new IEntityRelation object, between TournamentEntity and TournamentEntity over the m:1 relation they have, using the relation between the fields: Tournament.NextTournamentId - Tournament.Id</summary>
public virtual IEntityRelation TournamentEntityUsingIdNextTournamentId
{
get { return ModelInfoProviderSingleton.GetInstance().CreateRelation(RelationType.ManyToOne, "Tournament", false, new[] { TournamentFields.Id, TournamentFields.NextTournamentId }); }
get { return ModelInfoProviderSingleton.GetInstance().CreateRelation(RelationType.ManyToOne, "NextTournament", false, new[] { TournamentFields.Id, TournamentFields.NextTournamentId }); }
}

/// <summary>Returns a new IEntityRelation object, between TournamentEntity and TournamentTypeEntity over the m:1 relation they have, using the relation between the fields: Tournament.TypeId - TournamentType.Id</summary>
Expand Down
2 changes: 1 addition & 1 deletion TournamentManager/DAL/TournamentManager.llblgenproj
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@
<FieldFieldRelationship FkField="TeamId" PkField="Id" />
</FieldRelationships>
</NormalRelationship>
<NormalRelationship Name="Tournament.Tournament-Tournament.Tournaments" Start=":Tournament" End=":Tournament" StartNavigator="Tournament" EndNavigator="Tournaments" Type="4" EndIsOptional="true">
<NormalRelationship Name="Tournament.NextTournament-Tournament.Tournaments" Start=":Tournament" End=":Tournament" StartNavigator="NextTournament" EndNavigator="Tournaments" Type="4" EndIsOptional="true">
<FieldRelationships>
<FieldFieldRelationship FkField="NextTournamentId" PkField="Id" />
</FieldRelationships>
Expand Down
52 changes: 51 additions & 1 deletion TournamentManager/TournamentManager/Data/TournamentRepository.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Data;
using Microsoft.Extensions.Logging;
using SD.LLBLGen.Pro.LinqSupportClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
Expand Down Expand Up @@ -52,7 +53,6 @@ public TournamentRepository(MultiTenancy.IDbContext dbContext)
public virtual async Task<EntityCollection<RoundEntity>> GetTournamentRoundsAsync(long tournamentId, CancellationToken cancellationToken)
{
using var da = _dbContext.GetNewAdapter();
//var selectedRounds = new EntityCollection<RoundEntity>();
var metaData = new LinqMetaData(da);

var q = await (from r in metaData.Round
Expand All @@ -63,6 +63,18 @@ public virtual async Task<EntityCollection<RoundEntity>> GetTournamentRoundsAsyn
return result;
}

public virtual async Task<List<long>> GetTournamentRoundIdsAsync(long tournamentId, CancellationToken cancellationToken)
{
using var da = _dbContext.GetNewAdapter();
var metaData = new LinqMetaData(da);

var result = await (from r in metaData.Round
where r.TournamentId == tournamentId
select r.Id).ToListAsync(cancellationToken);

return result;
}

public virtual async Task<TournamentEntity?> GetTournamentEntityForMatchSchedulerAsync(long tournamentId, CancellationToken cancellationToken)
{
var bucket = new RelationPredicateBucket(TournamentFields.Id == tournamentId);
Expand All @@ -87,4 +99,42 @@ public virtual async Task<EntityCollection<RoundEntity>> GetTournamentRoundsAsyn
await da.FetchEntityCollectionAsync(qp, cancellationToken);
return t.FirstOrDefault();
}

internal virtual async Task<bool> SaveTournamentsAsync(TournamentEntity sourceTournament, TournamentEntity targetTournament, CancellationToken cancellationToken)
{
using var da = _dbContext.GetNewAdapter();

try
{
await da.StartTransactionAsync(IsolationLevel.ReadCommitted,
string.Concat(nameof(SaveTournamentsAsync), Guid.NewGuid().ToString("N")), cancellationToken);

await da.SaveEntityAsync(targetTournament, true, true, cancellationToken);

if (sourceTournament.NextTournamentId is null)
{
sourceTournament.NextTournamentId = targetTournament.Id;
sourceTournament.ModifiedOn = targetTournament.ModifiedOn;
await da.SaveEntityAsync(sourceTournament, true, false, cancellationToken);
_logger.LogDebug("{Property} set to {NextTournamentId}", nameof(TournamentEntity.NextTournamentId), sourceTournament.NextTournamentId);
}
else
{
_logger.LogDebug("{Property} was already set to {NextTournamentId}", nameof(TournamentEntity.NextTournamentId), sourceTournament.NextTournamentId);
}

await da.CommitAsync(cancellationToken);
return true;
}
catch (Exception e)
{
_logger.LogCritical(e, "Error saving transaction for tournament IDs {TargetId} and {SourceId}",
targetTournament.Id, sourceTournament.Id);

if (da.IsTransactionInProgress)
da.Rollback();

return false;
}
}
}
Loading

0 comments on commit 6d10b7c

Please sign in to comment.