Ejemplo n.º 1
0
        public async Task <CreateSessionResponse> CreateSessionAsync(CreateSessionRequest createSessionRequest, CancellationToken cancellationToken)
        {
            var session = new SessionModel
            {
                SessionId    = ObjectId.GenerateNewId().ToString(),
                SessionCode  = GetRandomCode(SessionCodeLength),
                Expires      = DateTime.UtcNow.AddDays(ExpiryTimeInDays),
                HostCode     = GetRandomCode(HostCodeLength),
                PointChoices = createSessionRequest.PointChoices
            };

            await _sessionRepo.InsertSessionAsync(session, cancellationToken);

            var user = new UserModel
            {
                SessionId = session.SessionId,
                UserId    = ObjectId.GenerateNewId().ToString(),
                AuthCode  = GetRandomCode(AuthCodeLength),
                Nickname  = createSessionRequest.Nickname,
                IsHost    = true
            };

            await _userRepo.InsertUserAsync(user, cancellationToken);

            var i = 0;

            foreach (var story in createSessionRequest.Stories)
            {
                story.SessionId  = session.SessionId;
                story.StoryIndex = i++;
            }

            await _storyRepo.InsertStoriesAsync(createSessionRequest.Stories, cancellationToken);

            ScheduleDeleteSession(session.SessionId);

            return(new CreateSessionResponse
            {
                SessionId = session.SessionId,
                SessionCode = session.SessionCode,
                HostCode = session.HostCode,
                PointChoices = session.PointChoices,
                UserId = user.UserId,
                AuthCode = user.AuthCode
            });
        }