示例#1
0
        public async Task <IActionResult> AddUserToGroup(AddUserToGroupRequest request)
        {
            var result = new AddUserToGroupRequestValidation().Validate(request);

            if (!result.IsValid)
            {
                foreach (var failure in result.Errors)
                {
                    ModelState.AddModelError(failure.PropertyName, failure.ErrorMessage);
                }

                var errors = ModelState.Values.SelectMany(v => v.Errors.Select(b => b.ErrorMessage)).ToList();
                _telemetryClient.TrackTrace(new TraceTelemetry(
                                                $"AddUserToGroupRequest validation failed: {string.Join(separator, errors)}",
                                                SeverityLevel.Error));
                return(BadRequest(ModelState));
            }

            var group = await _userAccountService.GetGroupByNameAsync(request.GroupName);

            if (group == null)
            {
                _telemetryClient.TrackTrace(new TraceTelemetry($"Group not found '{request.GroupName}'",
                                                               SeverityLevel.Error));
                return(NotFound());
            }

            var user = new User
            {
                Id = request.UserId
            };

            try
            {
                await _userAccountService.AddUserToGroupAsync(user, group);
            }
            catch (UserServiceException e)
            {
                ModelState.AddModelError(nameof(user), e.Reason);
                return(NotFound(ModelState));
            }

            return(Accepted());
        }