| | | 1 | | using EHonda.KicktippAi.Core; |
| | | 2 | | using FirebaseAdapter.Models; |
| | | 3 | | using Google.Cloud.Firestore; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using NodaTime; |
| | | 6 | | |
| | | 7 | | namespace FirebaseAdapter; |
| | | 8 | | |
| | | 9 | | public class FirebaseMatchOutcomeRepository : IMatchOutcomeRepository |
| | | 10 | | { |
| | | 11 | | private const int ExpectedMatchesPerMatchday = 9; |
| | | 12 | | |
| | | 13 | | private readonly FirestoreDb _firestoreDb; |
| | | 14 | | private readonly ILogger<FirebaseMatchOutcomeRepository> _logger; |
| | | 15 | | private readonly string _matchOutcomesCollection; |
| | | 16 | | private readonly string _competition; |
| | | 17 | | |
| | 0 | 18 | | public FirebaseMatchOutcomeRepository(FirestoreDb firestoreDb, ILogger<FirebaseMatchOutcomeRepository> logger) |
| | | 19 | | { |
| | 0 | 20 | | _firestoreDb = firestoreDb ?? throw new ArgumentNullException(nameof(firestoreDb)); |
| | 0 | 21 | | _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| | 0 | 22 | | _matchOutcomesCollection = "match-outcomes"; |
| | 0 | 23 | | _competition = "bundesliga-2025-26"; |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | public async Task<MatchOutcomeUpsertResult> UpsertMatchOutcomeAsync( |
| | | 27 | | CollectedMatchOutcome outcome, |
| | | 28 | | string communityContext, |
| | | 29 | | CancellationToken cancellationToken = default) |
| | | 30 | | { |
| | 0 | 31 | | var documentId = BuildDocumentId(outcome); |
| | 0 | 32 | | var docRef = _firestoreDb.Collection(_matchOutcomesCollection).Document(documentId); |
| | 0 | 33 | | var snapshot = await docRef.GetSnapshotAsync(cancellationToken); |
| | 0 | 34 | | var now = Timestamp.GetCurrentTimestamp(); |
| | | 35 | | |
| | 0 | 36 | | if (!snapshot.Exists) |
| | | 37 | | { |
| | 0 | 38 | | var firestoreOutcome = ToFirestoreMatchOutcome(outcome, communityContext, documentId, now, now); |
| | 0 | 39 | | await docRef.SetAsync(firestoreOutcome, cancellationToken: cancellationToken); |
| | | 40 | | |
| | 0 | 41 | | return new MatchOutcomeUpsertResult( |
| | 0 | 42 | | MatchOutcomeWriteDisposition.Created, |
| | 0 | 43 | | ConvertToPersistedMatchOutcome(firestoreOutcome)); |
| | | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | var existing = snapshot.ConvertTo<FirestoreMatchOutcome>(); |
| | 0 | 47 | | if (!NeedsUpdate(existing, outcome)) |
| | | 48 | | { |
| | 0 | 49 | | return new MatchOutcomeUpsertResult( |
| | 0 | 50 | | MatchOutcomeWriteDisposition.Unchanged, |
| | 0 | 51 | | ConvertToPersistedMatchOutcome(existing)); |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | var updated = ToFirestoreMatchOutcome(outcome, communityContext, documentId, existing.CreatedAt, now); |
| | 0 | 55 | | await docRef.SetAsync(updated, cancellationToken: cancellationToken); |
| | | 56 | | |
| | 0 | 57 | | return new MatchOutcomeUpsertResult( |
| | 0 | 58 | | MatchOutcomeWriteDisposition.Updated, |
| | 0 | 59 | | ConvertToPersistedMatchOutcome(updated)); |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | public async Task<IReadOnlyList<int>> GetIncompleteMatchdaysAsync( |
| | | 63 | | string communityContext, |
| | | 64 | | int currentMatchday, |
| | | 65 | | CancellationToken cancellationToken = default) |
| | | 66 | | { |
| | 0 | 67 | | var query = _firestoreDb.Collection(_matchOutcomesCollection) |
| | 0 | 68 | | .WhereEqualTo("communityContext", communityContext); |
| | | 69 | | |
| | 0 | 70 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | 0 | 71 | | var groupedOutcomes = snapshot.Documents |
| | 0 | 72 | | .Select(doc => doc.ConvertTo<FirestoreMatchOutcome>()) |
| | 0 | 73 | | .Where(outcome => outcome.Competition == _competition && outcome.Matchday <= currentMatchday) |
| | 0 | 74 | | .GroupBy(outcome => outcome.Matchday) |
| | 0 | 75 | | .ToDictionary(group => group.Key, group => group.ToList()); |
| | | 76 | | |
| | 0 | 77 | | var incompleteMatchdays = new List<int>(); |
| | 0 | 78 | | for (var matchday = 1; matchday <= currentMatchday; matchday++) |
| | | 79 | | { |
| | 0 | 80 | | if (!groupedOutcomes.TryGetValue(matchday, out var outcomes)) |
| | | 81 | | { |
| | 0 | 82 | | incompleteMatchdays.Add(matchday); |
| | 0 | 83 | | continue; |
| | | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | var isComplete = outcomes.Count >= ExpectedMatchesPerMatchday && |
| | 0 | 87 | | outcomes.All(outcome => string.Equals(outcome.Availability, nameof(MatchOutcomeAvailability |
| | | 88 | | |
| | 0 | 89 | | if (!isComplete) |
| | | 90 | | { |
| | 0 | 91 | | incompleteMatchdays.Add(matchday); |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | return incompleteMatchdays.AsReadOnly(); |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | public async Task<IReadOnlyList<PersistedMatchOutcome>> GetMatchdayOutcomesAsync( |
| | | 99 | | int matchday, |
| | | 100 | | string communityContext, |
| | | 101 | | CancellationToken cancellationToken = default) |
| | | 102 | | { |
| | 0 | 103 | | var query = _firestoreDb.Collection(_matchOutcomesCollection) |
| | 0 | 104 | | .WhereEqualTo("communityContext", communityContext); |
| | | 105 | | |
| | 0 | 106 | | var snapshot = await query.GetSnapshotAsync(cancellationToken); |
| | 0 | 107 | | return snapshot.Documents |
| | 0 | 108 | | .Select(doc => doc.ConvertTo<FirestoreMatchOutcome>()) |
| | 0 | 109 | | .Where(outcome => outcome.Competition == _competition && outcome.Matchday == matchday) |
| | 0 | 110 | | .Select(ConvertToPersistedMatchOutcome) |
| | 0 | 111 | | .OrderBy(outcome => outcome.HomeTeam) |
| | 0 | 112 | | .ToList() |
| | 0 | 113 | | .AsReadOnly(); |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | private static bool NeedsUpdate(FirestoreMatchOutcome existing, CollectedMatchOutcome outcome) |
| | | 117 | | { |
| | 0 | 118 | | return existing.HomeGoals != outcome.HomeGoals || |
| | 0 | 119 | | existing.AwayGoals != outcome.AwayGoals || |
| | 0 | 120 | | !string.Equals(existing.Availability, outcome.Availability.ToString(), StringComparison.Ordinal) || |
| | 0 | 121 | | existing.TippSpielId != outcome.TippSpielId || |
| | 0 | 122 | | existing.StartsAt.ToDateTimeOffset() != outcome.StartsAt.ToInstant().ToDateTimeOffset(); |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | private FirestoreMatchOutcome ToFirestoreMatchOutcome( |
| | | 126 | | CollectedMatchOutcome outcome, |
| | | 127 | | string communityContext, |
| | | 128 | | string documentId, |
| | | 129 | | Timestamp createdAt, |
| | | 130 | | Timestamp updatedAt) |
| | | 131 | | { |
| | 0 | 132 | | return new FirestoreMatchOutcome |
| | 0 | 133 | | { |
| | 0 | 134 | | Id = documentId, |
| | 0 | 135 | | HomeTeam = outcome.HomeTeam, |
| | 0 | 136 | | AwayTeam = outcome.AwayTeam, |
| | 0 | 137 | | StartsAt = ConvertToTimestamp(outcome.StartsAt), |
| | 0 | 138 | | Matchday = outcome.Matchday, |
| | 0 | 139 | | HomeGoals = outcome.HomeGoals, |
| | 0 | 140 | | AwayGoals = outcome.AwayGoals, |
| | 0 | 141 | | Availability = outcome.Availability.ToString(), |
| | 0 | 142 | | TippSpielId = outcome.TippSpielId, |
| | 0 | 143 | | CreatedAt = createdAt, |
| | 0 | 144 | | UpdatedAt = updatedAt, |
| | 0 | 145 | | Competition = _competition, |
| | 0 | 146 | | CommunityContext = communityContext |
| | 0 | 147 | | }; |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | private PersistedMatchOutcome ConvertToPersistedMatchOutcome(FirestoreMatchOutcome firestoreOutcome) |
| | | 151 | | { |
| | 0 | 152 | | return new PersistedMatchOutcome( |
| | 0 | 153 | | firestoreOutcome.CommunityContext, |
| | 0 | 154 | | firestoreOutcome.Competition, |
| | 0 | 155 | | firestoreOutcome.HomeTeam, |
| | 0 | 156 | | firestoreOutcome.AwayTeam, |
| | 0 | 157 | | ConvertFromTimestamp(firestoreOutcome.StartsAt), |
| | 0 | 158 | | firestoreOutcome.Matchday, |
| | 0 | 159 | | firestoreOutcome.HomeGoals, |
| | 0 | 160 | | firestoreOutcome.AwayGoals, |
| | 0 | 161 | | Enum.Parse<MatchOutcomeAvailability>(firestoreOutcome.Availability, ignoreCase: false), |
| | 0 | 162 | | firestoreOutcome.TippSpielId, |
| | 0 | 163 | | firestoreOutcome.CreatedAt.ToDateTimeOffset(), |
| | 0 | 164 | | firestoreOutcome.UpdatedAt.ToDateTimeOffset()); |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | private static string BuildDocumentId(CollectedMatchOutcome outcome) |
| | | 168 | | { |
| | 0 | 169 | | return outcome.TippSpielId ?? throw new InvalidOperationException( |
| | 0 | 170 | | $"Cannot persist match outcome for {outcome.HomeTeam} vs {outcome.AwayTeam} on matchday {outcome.Matchday} b |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | private static Timestamp ConvertToTimestamp(ZonedDateTime zonedDateTime) |
| | | 174 | | { |
| | 0 | 175 | | var instant = zonedDateTime.ToInstant(); |
| | 0 | 176 | | return Timestamp.FromDateTimeOffset(instant.ToDateTimeOffset()); |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | private static ZonedDateTime ConvertFromTimestamp(Timestamp timestamp) |
| | | 180 | | { |
| | 0 | 181 | | var instant = Instant.FromDateTimeOffset(timestamp.ToDateTimeOffset()); |
| | 0 | 182 | | return instant.InUtc(); |
| | | 183 | | } |
| | | 184 | | } |