Example #1
0
        public async Task HandleAsync(ProcessRemark command)
        {
            var remarkProcessed = false;
            await _handler
            .Validate(async() =>
            {
                await _policy.ValidateAsync(command.RemarkId, command.UserId);
                var remark = await _remarkService.GetAsync(command.RemarkId);
                if (remark.Value.Group == null)
                {
                    return;
                }
                await _groupService.ValidateIfRemarkCanBeProcessedOrFailAsync(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.ProcessAsync(command.RemarkId, command.UserId, command.Description, location);
            })
            .OnSuccess(async() =>
            {
                remarkProcessed = true;
                var remark      = await _remarkService.GetAsync(command.RemarkId);
                var state       = remark.Value.GetLatestStateOf(RemarkState.Names.Processing).Value;
                var resource    = _resourceFactory.Resolve <RemarkProcessed>(command.RemarkId);
                await _bus.PublishAsync(new RemarkProcessed(command.Request.Id, resource,
                                                            command.UserId, command.RemarkId));
            })
            .OnCustomError(async ex => await _bus.PublishAsync(new ProcessRemarkRejected(command.Request.Id,
                                                                                         command.UserId, command.RemarkId, ex.Code, ex.Message)))
            .OnError(async(ex, logger) =>
            {
                logger.Error(ex, "Error occured while processing a remark.");
                await _bus.PublishAsync(new ProcessRemarkRejected(command.Request.Id,
                                                                  command.UserId, command.RemarkId, OperationCodes.Error, ex.Message));
            })
            .Next()
            .Run(async() =>
            {
                if (!remarkProcessed)
                {
                    return;
                }

                var participant = await _remarkActionService.GetParticipantAsync(command.RemarkId, command.UserId);
                if (participant.HasValue)
                {
                    return;
                }
                var takeRemarkAction = new TakeRemarkAction
                {
                    Request     = Messages.Commands.Request.From <TakeRemarkAction>(command.Request),
                    UserId      = command.UserId,
                    RemarkId    = command.RemarkId,
                    Description = command.Description
                };
                await _bus.PublishAsync(takeRemarkAction);
            })
            .Next()
            .ExecuteAllAsync();
        }