Example #1
0
 public async Task HandleAsync(CancelRemark command)
 {
     await _handler
     .Validate(async() =>
     {
         var remark = await _remarkService.GetAsync(command.RemarkId);
         if (remark.Value.Group == null)
         {
             return;
         }
         await _groupService.ValidateIfRemarkCanBeCanceledOrFailAsync(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.CancelAsync(command.RemarkId, command.UserId, command.Description, location);
     })
     .OnSuccess(async() =>
     {
         var remark   = await _remarkService.GetAsync(command.RemarkId);
         var state    = remark.Value.GetLatestStateOf(RemarkState.Names.Canceled).Value;
         var resource = _resourceFactory.Resolve <RemarkCanceled>(command.RemarkId);
         await _bus.PublishAsync(new RemarkCanceled(command.Request.Id, resource,
                                                    command.UserId, command.RemarkId));
     })
     .OnCustomError(async ex => await _bus.PublishAsync(new CancelRemarkRejected(command.Request.Id,
                                                                                 command.UserId, command.RemarkId, ex.Code, ex.Message)))
     .OnError(async(ex, logger) =>
     {
         logger.Error(ex, "Error occured while cancelling a remark.");
         await _bus.PublishAsync(new CancelRemarkRejected(command.Request.Id,
                                                          command.UserId, command.RemarkId, OperationCodes.Error, ex.Message));
     })
     .ExecuteAsync();
 }