Пример #1
0
        public async Task UpdateMemberAsync(Guid userId, Guid targetUserId, Stream requestBody, string?contentType, byte[] rowVersion, CancellationToken cancellationToken)
        {
            if (Guid.Empty == userId)
            {
                throw new ArgumentOutOfRangeException(nameof(userId));
            }
            if (Guid.Empty == targetUserId)
            {
                throw new ArgumentOutOfRangeException(nameof(targetUserId));
            }

            var userCanPerformAction = await _permissionsService.UserCanPerformActionAsync(userId, EditMembersRole, cancellationToken);

            var userCanUpdateSelf = userId == targetUserId;

            if (!userCanPerformAction && !userCanUpdateSelf)
            {
                _logger.LogError($"Error: UpdateMemberAsync- User:{0} does not have access to perform edit actions", userId);
                throw new SecurityException($"Error: User does not have access");
            }

            var(user, image) = await UploadUserImageMultipartContent(targetUserId, requestBody, rowVersion, contentType, cancellationToken);

            if (user is not null)
            {
                var userValidator        = new UserValidator();
                var userValidationResult = await userValidator.ValidateAsync(user, cancellationToken);

                if (userValidationResult.Errors.Count > 0)
                {
                    throw new ValidationException(userValidationResult);
                }
            }

            try
            {
                if (image is not null)
                {
                    var imageId = await _imageService.CreateImageAsync(image);

                    user = user with {
                        ImageId = imageId
                    };
                }
            }
            catch (DBConcurrencyException ex)
            {
                _logger.LogError(ex, $"Error: CreateImageAsync - Error adding image to user {0}");
                if (image is not null)
                {
                    await _blobStorageProvider.DeleteFileAsync(image.FileName);

                    await _imageService.DeleteImageAsync(image.Id);
                }
            }

            try
            {
                await _userCommand.UpdateUserAsync(user, rowVersion, cancellationToken);
            }
            catch (DBConcurrencyException ex)
            {
                _logger.LogError(ex, $"Error: UpdateUserAsync - Error updating user {0}");
            }
        }