示例#1
0
        public async Task <List <BuildOnReturning> > GetReturningFromCoach(string currentUserId, string projectId)
        {
            Coach coach = await _coachsService.GetCoachFromAdminAsync(currentUserId);

            Project project = await _projectsService.GetProjectFromIdAsync(projectId);

            if (coach == null)
            {
                throw new UnauthorizedAccessException("You are not a coach");
            }
            if (project == null)
            {
                throw new Exception("The project doesn't exist");
            }

            Coach builderCoach = await _buildersService.GetCoachForBuilderFromAdminAsync(project.BuilderId);

            if (coach.Id != builderCoach.Id)
            {
                throw new UnauthorizedAccessException("You are not the coach of this builder");
            }

            return(await(await _buildOnReturnings.FindAsync(databaseReturning =>
                                                            databaseReturning.ProjectId == projectId
                                                            )).ToListAsync());
        }
示例#2
0
        public async Task <ActionResult <Coach> > GetCoach(string userId)
        {
            var   currentUserId = User.Identity.Name;
            Coach coach;

            try
            {
                if (User.IsInRole(Role.Admin))
                {
                    coach = await _coachService.GetCoachFromAdminAsync(userId);
                }
                else if (User.IsInRole(Role.Coach))
                {
                    coach = await _coachService.GetCoachFromCoachAsync(currentUserId, userId);
                }
                else
                {
                    return(Forbid("You must be part of the Buildup program"));
                }
            }
            catch (UnauthorizedAccessException e)
            {
                return(Forbid($"You are not authorized to view this coach info: {e.Message}"));
            }
            catch (Exception e)
            {
                return(BadRequest($"Can't get the coach: {e.Message}"));
            }

            if (coach == null)
            {
                return(NotFound());
            }

            return(Ok(coach));
        }