Exemple #1
0
        public async Task <IActionResult> CreateUserRole(UserRoleCreateRequest request)
        {
            //Recupero l'elemento dal business layer
            var user = BasicLayer.GetShooter(request.UserId);

            if (user == null)
            {
                return(NotFound($"User with {request.UserId} not found"));
            }

            //Recupero l'elemento dal business layer
            var role = AuthorizationLayer.GetRole(request.RoleId);

            if (role == null)
            {
                return(NotFound($"Role with {request.RoleId} not found"));
            }

            var entity = new UserRole()
            {
                UserId   = user.Id,
                RoleId   = role.Id,
                EntityId = request.EntityId
            };
            //Invocazione del service layer
            var validations = await AuthorizationLayer.CreateUserRole(entity, PlatformUtils.GetIdentityUserId(User));

            if (validations.Count > 0)
            {
                return(BadRequest(validations));
            }

            var userRoles = AuthorizationLayer.FetchUserRole(role.Id);

            var userIds = userRoles.Select(x => x.UserId).ToList();

            var users = BasicLayer.FetchShootersByIds(userIds);

            //Return contract
            return(Ok(userRoles.As(x => ContractUtils.GenerateContract(x, users?.FirstOrDefault(s => s.Id == x.UserId)))));
        }