public RevisionSession StartRevisionSession(DateTimeOffset?now = null) { if (CurrentRevisionSessionId != null) { throw new InvalidOperationException("An uncompleted revision session exists"); } now ??= DateTimeOffset.UtcNow; var cardsReadyForSession = _cardBoxes .SelectMany(b => b.Cards.Select(c => new { box = b, card = c }).Where(bc => bc.card.CardBoxChangedDate != null && bc.card.CardBoxChangedDate.Value.AddDays(bc.box.RevisionDelay) <= now)) .Select(bc => bc.card) .ToList(); // instances of owned types cannot be shared between multiple owners // See: https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities var sessionId = Guid.NewGuid(); var sessionCards = cardsReadyForSession .Select(c => new SessionCard(sessionId, c)) .ToList(); var session = new RevisionSession(sessionId, Id, sessionCards); CurrentRevisionSessionId = session.Id; return(session); }
public void ProcessCardsFromRevisionSession(RevisionSession revisionSession, DateTimeOffset?dateTime = null) { if (Id != revisionSession.CardBoxSetId) { throw new InvalidOperationException("The revision session was created from other card box set"); } if (CurrentRevisionSessionId != revisionSession.Id) { throw new InvalidOperationException( "The revision session doesn't match with the current revision session of the set"); } if (_completedRevisionSessionIds.Any(id => id.Value == revisionSession.Id)) { return; } if (revisionSession.Status != RevisionSessionStatus.Completed || revisionSession.SessionCards.Any(c => c.Status == SessionCardStatus.NotAnswered)) { throw new DomainException.RevisionSessionNotCompletedException(); } var splittedCardIds = revisionSession.SessionCards .GroupBy(c => c.Status) .ToDictionary(c => c.Key, x => x.Select(c => c.CardId).ToArray()); if (splittedCardIds.TryGetValue(SessionCardStatus.AnsweredCorrectly, out var answeredCorrectlyCardIds)) { foreach (var cardId in answeredCorrectlyCardIds) { PromoteCard(cardId, dateTime); } } if (splittedCardIds.TryGetValue(SessionCardStatus.AnsweredWrong, out var answeredWrongCardIds)) { foreach (var cardId in answeredWrongCardIds) { DemoteCard(cardId, dateTime); } } _completedRevisionSessionIds.Add(new CompletedRevisionSessionId(revisionSession.Id)); CurrentRevisionSessionId = null; }
public void AddNew(RevisionSession revisionSession) { _db.RevisionSessions.Add(revisionSession); }