Esempio n. 1
0
        public async Task <ActionResult <Builder> > GetBuilder(string id)
        {
            var     currentUserId = User.Identity.Name;
            Builder builder;

            try
            {
                if (User.IsInRole(Role.Admin))
                {
                    builder = await _buildersService.GetBuilderFromAdminAsync(id);
                }
                else if (User.IsInRole(Role.Coach))
                {
                    builder = await _buildersService.GetBuilderFromCoachAsync(currentUserId, id);
                }
                else if (User.IsInRole(Role.Builder))
                {
                    builder = await _buildersService.GetBuilderFromBuilderAsync(currentUserId, id);
                }
                else
                {
                    return(Forbid("You must be part of the Buildup program"));
                }
            }
            catch (UnauthorizedAccessException e)
            {
                return(Forbid($"You are not allowed to view this builder: {e.Message}"));
            }
            catch (Exception e)
            {
                return(BadRequest($"Can't get the builder: {e.Message}"));
            }

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

            return(Ok(builder));
        }