Ejemplo n.º 1
0
 public async Task HandleAsync(EditRemark command)
 => await _handler
 .Validate(async() =>
           await _remarkService.ValidateEditorAccessOrFailAsync(command.RemarkId, command.UserId))
 .Run(async() =>
 {
     Location location = null;
     if (command.Latitude.HasValue && command.Latitude != 0 &&
         command.Longitude.HasValue && command.Longitude != 0)
     {
         var locations = await _locationService.GetAsync(command.Latitude.Value, command.Longitude.Value);
         if (locations.HasNoValue || locations.Value.Results == null || !locations.Value.Results.Any())
         {
             throw new ServiceException(OperationCodes.AddressNotFound,
                                        $"Address was not found for remark with id: '{command.RemarkId}' " +
                                        $"latitude: {command.Latitude}, longitude:  {command.Longitude}.");
         }
         var address = locations.Value.Results.First().FormattedAddress;
         location    = Domain.Location.Create(command.Latitude.Value, command.Longitude.Value, address);
     }
     await _remarkService.EditAsync(command.RemarkId, command.UserId,
                                    command.GroupId, command.Category, command.Description, location);
 })
 .OnSuccess(async() =>
 {
     var remark   = await _remarkService.GetAsync(command.RemarkId);
     var resource = _resourceFactory.Resolve <RemarkEdited>(command.RemarkId);
     await _bus.PublishAsync(new RemarkEdited(command.Request.Id, resource,
                                              command.UserId, command.RemarkId));
 })
 .OnCustomError(async ex => await _bus.PublishAsync(new EditRemarkRejected(command.Request.Id,
                                                                           command.UserId, command.RemarkId, ex.Code, ex.Message)))
 .OnError(async(ex, logger) =>
 {
     logger.Error(ex, "Error occured while editing a remark.");
     await _bus.PublishAsync(new EditRemarkRejected(command.Request.Id,
                                                    command.UserId, command.RemarkId, OperationCodes.Error, ex.Message));
 })
 .ExecuteAsync();