public async Task <IActionResult> Put([FromBody] User user)
        {
            if (!ProjectId.HasValue)
            {
                return(ErrorResult
                       .BadRequest($"Project Id provided in the url path is invalid.  Must be a valid GUID.", ResultErrorCode.ValidationError)
                       .ActionResult());
            }

            var validation = new UserValidator().Validate(user);

            if (!validation.IsValid)
            {
                return(ErrorResult
                       .BadRequest(validation)
                       .ActionResult());
            }

            var project = await projectsRepository
                          .GetAsync(ProjectId.Value)
                          .ConfigureAwait(false);

            if (project is null)
            {
                return(ErrorResult
                       .NotFound($"A Project with the ID '{ProjectId.Value}' could not be found in this TeamCloud Instance.")
                       .ActionResult());
            }

            var oldUser = project?.Users?.FirstOrDefault(u => u.Id == user.Id);

            if (oldUser is null)
            {
                return(ErrorResult
                       .NotFound($"A User with the ID '{oldUser.Id}' could not be found on this Project.")
                       .ActionResult());
            }

            var command = new OrchestratorProjectUserUpdateCommand(CurrentUser, user, ProjectId.Value);

            var commandResult = await orchestrator
                                .InvokeAsync(command)
                                .ConfigureAwait(false);

            if (commandResult.Links.TryGetValue("status", out var statusUrl))
            {
                return(StatusResult
                       .Accepted(commandResult.CommandId.ToString(), statusUrl, commandResult.RuntimeStatus.ToString(), commandResult.CustomStatus)
                       .ActionResult());
            }

            throw new Exception("This shoudn't happen, but we need to decide to do when it does.");
        }
        public async Task <IActionResult> PutMe([FromBody] User user)
        {
            if (user is null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            if (string.IsNullOrEmpty(ProjectId))
            {
                return(ErrorResult
                       .BadRequest($"Project Id provided in the url path is invalid.  Must be a valid GUID.", ResultErrorCode.ValidationError)
                       .ActionResult());
            }

            var validation = new UserValidator().Validate(user);

            if (!validation.IsValid)
            {
                return(ErrorResult
                       .BadRequest(validation)
                       .ActionResult());
            }

            var me = await userService
                     .CurrentUserAsync()
                     .ConfigureAwait(false);

            if (me is null)
            {
                return(ErrorResult
                       .NotFound($"A User matching the current user was not found in this TeamCloud instance.")
                       .ActionResult());
            }

            if (!me.Id.Equals(user.Id, StringComparison.OrdinalIgnoreCase))
            {
                return(ErrorResult
                       .BadRequest(new ValidationError {
                    Field = "id", Message = $"User's id does match the id of the current user."
                })
                       .ActionResult());
            }

            if (!me.IsMember(ProjectId))
            {
                return(ErrorResult
                       .NotFound($"A User matching the current user was not found in this Project.")
                       .ActionResult());
            }

            if (me.IsOwner(ProjectId) && !user.IsOwner(ProjectId))
            {
                var otherOwners = await usersRepository
                                  .ListOwnersAsync(ProjectId)
                                  .AnyAsync(o => o.Id.Equals(user.Id, StringComparison.OrdinalIgnoreCase))
                                  .ConfigureAwait(false);

                if (!otherOwners)
                {
                    return(ErrorResult
                           .BadRequest($"Projects must have at least one Owner. To change this user's role you must first add another Owner.", ResultErrorCode.ValidationError)
                           .ActionResult());
                }
            }

            var membership = user.ProjectMembership(ProjectId);

            if (me.HasEqualMembership(membership))
            {
                return(ErrorResult
                       .BadRequest(new ValidationError {
                    Field = "projectMemberships", Message = $"User's project memberships did not change."
                })
                       .ActionResult());
            }

            me.UpdateProjectMembership(membership);

            var currentUserForCommand = await userService
                                        .CurrentUserAsync()
                                        .ConfigureAwait(false);

            var command = new OrchestratorProjectUserUpdateCommand(currentUserForCommand, me, ProjectId);

            return(await orchestrator
                   .InvokeAndReturnAccepted(command)
                   .ConfigureAwait(false));
        }
        public async Task <IActionResult> Put([FromRoute] string userNameOrId, [FromBody] User user)
        {
            if (user is null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            if (string.IsNullOrEmpty(ProjectId))
            {
                return(ErrorResult
                       .BadRequest($"Project Id provided in the url path is invalid.  Must be a valid GUID.", ResultErrorCode.ValidationError)
                       .ActionResult());
            }

            if (string.IsNullOrWhiteSpace(userNameOrId))
            {
                return(ErrorResult
                       .BadRequest($"The identifier '{userNameOrId}' provided in the url path is invalid.  Must be a valid email address or GUID.", ResultErrorCode.ValidationError)
                       .ActionResult());
            }

            var userId = await userService
                         .GetUserIdAsync(userNameOrId)
                         .ConfigureAwait(false);

            if (string.IsNullOrEmpty(userId))
            {
                return(ErrorResult
                       .NotFound($"The user '{userNameOrId}' could not be found.")
                       .ActionResult());
            }

            var validation = new UserValidator().Validate(user);

            if (!validation.IsValid)
            {
                return(ErrorResult
                       .BadRequest(validation)
                       .ActionResult());
            }

            if (!userId.Equals(user.Id, StringComparison.OrdinalIgnoreCase))
            {
                return(ErrorResult
                       .BadRequest(new ValidationError {
                    Field = "id", Message = $"User's id does match the identifier provided in the path."
                })
                       .ActionResult());
            }

            var oldUser = await usersRepository
                          .GetAsync(user.Id)
                          .ConfigureAwait(false);

            if (oldUser is null || !oldUser.IsMember(ProjectId))
            {
                return(ErrorResult
                       .NotFound($"The user '{user.Id}' could not be found in this project.")
                       .ActionResult());
            }

            if (oldUser.IsOwner(ProjectId) && !user.IsOwner(ProjectId))
            {
                var otherOwners = await usersRepository
                                  .ListOwnersAsync(ProjectId)
                                  .AnyAsync(o => o.Id.Equals(user.Id, StringComparison.OrdinalIgnoreCase))
                                  .ConfigureAwait(false);

                if (!otherOwners)
                {
                    return(ErrorResult
                           .BadRequest($"Projects must have at least one Owner. To change this user's role you must first add another Owner.", ResultErrorCode.ValidationError)
                           .ActionResult());
                }
            }

            var membership = user.ProjectMembership(ProjectId);

            if (oldUser.HasEqualMembership(membership))
            {
                return(ErrorResult
                       .BadRequest(new ValidationError {
                    Field = "projectMemberships", Message = $"User's project memberships did not change."
                })
                       .ActionResult());
            }

            oldUser.UpdateProjectMembership(membership);

            var currentUserForCommand = await userService
                                        .CurrentUserAsync()
                                        .ConfigureAwait(false);

            var command = new OrchestratorProjectUserUpdateCommand(currentUserForCommand, oldUser, ProjectId);

            return(await orchestrator
                   .InvokeAndReturnAccepted(command)
                   .ConfigureAwait(false));
        }