Beispiel #1
0
        public async Task <int> InitiateTrainingAsync(int userId)
        {
            int        trainingId;
            TeamMember teamMember;

            try
            {
                trainingId = await trainings.GiveNewTrainingIdAsync(userId, idlenessMinutesForNewTraining);

                teamMember = await members.GetAsync(userId);

                if (teamMember == null)
                {
                    throw new BadRequestException("User doesn't belong to a team!");
                }
            }
            catch (SqlException ex)
            {
                if (ex.Number == SqlErrorCodes.USER_DOES_NOT_EXIST)
                {
                    throw new BadRequestException("Invalid user!");
                }
                throw ex;
            }

            if (!string.IsNullOrEmpty(teamMember.YouTubeId))
            {
                LiveStreamResponseDTO streamResponse = await youTubeService.GetCurrentLiveStreamAsync(new LiveStreamRequestDTO
                {
                    LiveStreamingServiceUserId = teamMember.YouTubeId
                });

                if (streamResponse.HasLivestream)
                {
                    await videoStreams.CreateAsync(new VideoStream
                    {
                        TeamMemberId = userId,
                        TrainingId   = trainingId,
                        YouTubeId    = streamResponse.LiveStreamId,
                        StartTime    = streamResponse.StartTime,
                        EndTime      = streamResponse.EndTime
                    });
                }
            }

            return(trainingId);
        }