示例#1
0
 public async Task HandleAsync(UploadAvatar command)
 {
     var avatarUrl = string.Empty;
     await _handler
     .Run(async() =>
     {
         var avatar = _fileResolver.FromBase64(command.Avatar.Base64, command.Avatar.Name, command.Avatar.ContentType);
         if (avatar.HasNoValue)
         {
             throw new ServiceException(OperationCodes.InvalidAvatar);
         }
         await _avatarService.AddOrUpdateAsync(command.UserId, avatar.Value);
         avatarUrl = await _avatarService.GetUrlAsync(command.UserId);
     })
     .OnSuccess(async() => await _bus.PublishAsync(new AvatarUploaded(command.Request.Id,
                                                                      command.UserId, avatarUrl)))
     .OnCustomError(async ex => await _bus.PublishAsync(new UploadAvatarRejected(command.Request.Id,
                                                                                 command.UserId, ex.Code, ex.Message)))
     .OnError(async(ex, logger) =>
     {
         logger.Error(ex, "Error occured while uploading avatar.");
         await _bus.PublishAsync(new UploadAvatarRejected(command.Request.Id,
                                                          command.UserId, OperationCodes.Error, ex.Message));
     })
     .ExecuteAsync();
 }
        protected override async Task Handle(UploadAvatarCommand command, CancellationToken cancellationToken)
        {
            await _handler
            .Run(async() =>
            {
                var avatar = _fileResolver.FromBase64(command.FileBase64, command.Filename,
                                                      command.FileContentType);

                if (avatar.HasNoValue())
                {
                    throw new ServiceException(Codes.AvatarIsInvalid,
                                               $"Avatar with filename: {command.Filename} is invalid.");
                }

                await _avatarService.AddOrUpdateAsync(command.UserId, avatar);
            })
            .OnSuccess(async() =>
            {
                var avatarUrl = await _avatarService.GetUrlAsync(command.UserId);
                await _mediatRBus.PublishAsync(new AvatarUploadedDomainEvent(command.Request.Id, command.UserId,
                                                                             avatarUrl), cancellationToken);
            })
            .OnCustomError(async customException =>
            {
                await _mediatRBus.PublishAsync(new UploadAvatarRejectedDomainEvent(command.Request.Id,
                                                                                   command.UserId, customException.Code, customException.Message), cancellationToken);
            })
            .OnError(async(exception, logger) =>
            {
                logger.Error($"Error occured when uploading avatar for user with id: {command.UserId}", exception);
                await _mediatRBus.PublishAsync(new UploadAvatarRejectedDomainEvent(command.Request.Id,
                                                                                   command.UserId, Codes.Error, exception.Message), cancellationToken);
            })
            .ExecuteAsync();
        }
        public async Task HandleAsync(ResolveRemark command)
        {
            File file = null;

            await _handler.Validate(async() =>
            {
                if (command.ValidatePhoto)
                {
                    var resolvedFile = _fileResolver.FromBase64(command.Photo.Base64, command.Photo.Name, command.Photo.ContentType);
                    if (resolvedFile.HasNoValue)
                    {
                        Logger.Error($"File cannot be resolved from base64, photoName:{command.Photo.Name}, " +
                                     $"contentType:{command.Photo.ContentType}, userId:{command.UserId}");
                        throw new ServiceException(OperationCodes.CannotConvertFile);
                    }
                    file        = resolvedFile.Value;
                    var isImage = _fileValidator.IsImage(file);
                    if (isImage == false)
                    {
                        Logger.Warning($"File is not an image! name:{file.Name}, contentType:{file.ContentType}, " +
                                       $"userId:{command.UserId}");
                        throw new ServiceException(OperationCodes.InvalidFile);
                    }
                }
                var remark = await _remarkService.GetAsync(command.RemarkId);
                if (remark.Value.Group == null)
                {
                    return;
                }
                await _groupService.ValidateIfRemarkCanBeResolvedOrFailAsync(remark.Value.Group.Id, command.UserId);
            })
            .Run(async() =>
            {
                Location location = null;
                if (command.Latitude != 0 && command.Longitude != 0)
                {
                    location = Location.Create(command.Latitude, command.Longitude, command.Address);
                }
                await _remarkStateService.ResolveAsync(command.RemarkId, command.UserId, command.Description,
                                                       location, file, command.ValidateLocation);
            })
            .OnSuccess(async() =>
            {
                var remark   = await _remarkService.GetAsync(command.RemarkId);
                var state    = remark.Value.GetLatestStateOf(RemarkState.Names.Resolved).Value;
                var resource = _resourceFactory.Resolve <RemarkResolved>(command.RemarkId);
                await _bus.PublishAsync(new RemarkResolved(command.Request.Id, resource,
                                                           command.UserId, command.RemarkId));
            })
            .OnCustomError(async ex => await _bus.PublishAsync(new ResolveRemarkRejected(command.Request.Id,
                                                                                         command.UserId, command.RemarkId, ex.Code, ex.Message)))
            .OnError(async(ex, logger) =>
            {
                logger.Error(ex, "Error occured while resolving a remark.");
                await _bus.PublishAsync(new ResolveRemarkRejected(command.Request.Id,
                                                                  command.UserId, command.RemarkId, OperationCodes.Error, ex.Message));
            })
            .ExecuteAsync();
        }
        public async Task HandleAsync(AddPhotosToRemark command)
        {
            await _handler
            .Validate(() =>
            {
                if (command.Photos == null || !command.Photos.Any())
                {
                    throw new ServiceException(OperationCodes.NoFiles,
                                               $"There are no photos to be added to the remark with id: '{command.RemarkId}'.");
                }
                if (command.Photos.Count() > _generalSettings.PhotosLimit)
                {
                    throw new ServiceException(OperationCodes.TooManyFiles);
                }
            })
            .Run(async() =>
            {
                var photos = new List <File>();
                foreach (var file in command.Photos)
                {
                    var resolvedFile = _fileResolver.FromBase64(file.Base64, file.Name, file.ContentType);
                    if (resolvedFile.HasNoValue)
                    {
                        throw new ServiceException(OperationCodes.CannotConvertFile);
                    }
                    var photo = resolvedFile.Value;
                    // var isImage = _fileValidator.IsImage(photo);
                    // if (!isImage)
                    // {

                    //     throw new ServiceException(OperationCodes.InvalidFile);
                    // }
                    photos.Add(photo);
                }
                await _remarkPhotoService.AddPhotosAsync(command.RemarkId, command.UserId, photos.ToArray());
            })
            .OnSuccess(async() =>
            {
                var remark   = await _remarkService.GetAsync(command.RemarkId);
                var resource = _resourceFactory.Resolve <PhotosToRemarkAdded>(command.RemarkId);
                await _bus.PublishAsync(new PhotosToRemarkAdded(command.Request.Id, resource,
                                                                command.UserId, command.RemarkId));
            })
            .OnCustomError(ex => _bus.PublishAsync(new AddPhotosToRemarkRejected(command.Request.Id,
                                                                                 command.RemarkId, command.UserId, ex.Code, ex.Message)))
            .OnError(async(ex, logger) =>
            {
                logger.Error(ex, "Error occured while adding photos to remark.");
                await _bus.PublishAsync(new AddPhotosToRemarkRejected(command.Request.Id,
                                                                      command.RemarkId, command.UserId, OperationCodes.Error, ex.Message));
            })
            .ExecuteAsync();
        }