Example #1
0
        public async Task <ActionResult <List <Builder> > > GetBuildersForCoach(string coachId)
        {
            var            currentUserId = User.Identity.Name;
            List <Builder> builders;

            try
            {
                if (User.IsInRole(Role.Admin))
                {
                    builders = await _coachService.GetBuildersFromAdminAsync(coachId);
                }
                else if (User.IsInRole(Role.Coach))
                {
                    builders = await _coachService.GetBuildersFromCoachAsync(currentUserId, coachId);
                }
                else
                {
                    return(Forbid("You must be part of the Buildup program"));
                }
            }
            catch (UnauthorizedAccessException e)
            {
                return(Forbid($"You are not allowed to get the builders: {e.Message}"));
            }
            catch (Exception e)
            {
                return(BadRequest($"Can't get the builders: {e.Message}"));
            }

            return(Ok(builders));
        }