Esempio n. 1
0
    public async ValueTask <ActionResult> StartTypingSession()
    {
        var typingSessionId = await _typingSessionRepository.NextIdAsync();

        var typingSession = new TypingSession(typingSessionId, ProfileId, DateTime.UtcNow, new TypingSessionConfiguration());

        await _typingSessionRepository.SaveAsync(typingSession);

        var result = new { typingSessionId };

        return(CreatedAtAction(nameof(GetById), result, result));
    }
    public async ValueTask <TypingResultSubmitData> AddTypingResultAsync(TypedText typedText, string userId)
    {
        var text = await _textRepository.FindAsync(typedText.TextId)
                   .ConfigureAwait(false);

        if (text == null)
        {
            throw new InvalidOperationException("Text was not found.");
        }

        var typingSessionId = await _typingSessionRepository.NextIdAsync()
                              .ConfigureAwait(false);

        var typingSession          = new TypingSession(typingSessionId, userId, DateTime.UtcNow, new TypingSessionConfiguration());
        var typingSessionTextIndex = typingSession.AddText(new TypingSessionText(typedText.TextId, text.Value));
        await _typingSessionRepository.SaveAsync(typingSession)
        .ConfigureAwait(false);

        var userSessionId = await _userSessionRepository.NextIdAsync()
                            .ConfigureAwait(false);

        var userSession = new UserSession(userSessionId, userId, typingSessionId, DateTime.UtcNow, typedText.UserTimeZoneOffsetMinutes);
        await _userSessionRepository.SaveAsync(userSession)
        .ConfigureAwait(false);

        var typingResultId = Guid.NewGuid().ToString();

        await AddTypingResultAsync(userSessionId, new TextTypingResult(
                                       typingResultId,
                                       typingSessionTextIndex,
                                       typedText.StartedTypingUtc,
                                       DateTime.UtcNow,
                                       typedText.Events))
        .ConfigureAwait(false);

        return(new TypingResultSubmitData(typedText.TextId, typingSessionId, userSessionId, typingResultId));
    }