コード例 #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> > GetCoachForBuilder(string builderId)
        {
            var   currentUserId = User.Identity.Name;
            Coach coach;

            try
            {
                if (User.IsInRole(Role.Admin))
                {
                    coach = await _buildersService.GetCoachForBuilderFromAdminAsync(builderId);
                }
                else if (User.IsInRole(Role.Builder))
                {
                    coach = await _buildersService.GetCoachForBuilderFromBuilderAsync(currentUserId, builderId);
                }
                else
                {
                    return(Forbid("You must be part of the Buildup program"));
                }
            }
            catch (UnauthorizedAccessException e)
            {
                return(Forbid($"You are not allowed to get the builder's coach: {e.Message}"));
            }
            catch (Exception e)
            {
                return(BadRequest($"Can't get the coach: {e.Message}"));
            }

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

            return(Ok(coach));
        }