public async Task <TeamMemberDTO> GetTeamMemberOrNullAsync(int userId)
        {
            TeamMember member = await teamMembers.GetAsync(userId);

            if (member == null)
            {
                return(null);
            }
            return(new TeamMemberDTO
            {
                Id = userId,
                TeamId = member.TeamId,
                YouTubeId = member.YouTubeId
            });
        }
        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);
        }
Exemple #3
0
        public async Task <int> CreateTeamAsync(CreateTeamDTO request)
        {
            TeamMember userAsTeamMember = await members.GetAsync(request.CreatorId);

            if (userAsTeamMember != null)
            {
                throw new BadRequestException("User is already in a team!");
            }

            var team = new Team
            {
                Name = request.Name
            };
            int teamId = await teams.CreateAsync(team);

            await AddTeamMemberAsync(teamId, request.CreatorId);

            return(teamId);
        }