public async Task HandleAsync(RemovePhotosFromRemark command) { var groupIds = new Guid[] {}; var removedPhotos = new string[] {}; await _handler .Validate(async() => await _remarkService.ValidateEditorAccessOrFailAsync(command.RemarkId, command.UserId)) .Run(async() => { groupIds = command.Photos? .Where(x => x.GroupId != Guid.Empty) .Select(x => x.GroupId) .ToArray() ?? new Guid[] {}; var names = command.Photos? .Where(x => x.Name.NotEmpty()) .Select(x => x.Name) .ToArray() ?? new string[] {}; var namesForGroups = await _remarkPhotoService.GetPhotosForGroupsAsync(command.RemarkId, groupIds); removedPhotos = names .Union(namesForGroups.HasValue ? namesForGroups.Value : Enumerable.Empty <string>()) .Distinct() .ToArray(); await _remarkPhotoService.RemovePhotosAsync(command.RemarkId, removedPhotos); }) .OnSuccess(async() => { var resource = _resourceFactory.Resolve <PhotosFromRemarkRemoved>(command.RemarkId); await _bus.PublishAsync(new PhotosFromRemarkRemoved(command.Request.Id, resource, command.UserId, command.RemarkId)); }) .OnCustomError(ex => _bus.PublishAsync(new RemovePhotosFromRemarkRejected(command.Request.Id, command.RemarkId, command.UserId, ex.Code, ex.Message))) .OnError(async(ex, logger) => { logger.Error(ex, $"Error occured while removing photos from the remark with id: '{command.RemarkId}'."); await _bus.PublishAsync(new RemovePhotosFromRemarkRejected(command.Request.Id, command.RemarkId, command.UserId, OperationCodes.Error, ex.Message)); }) .ExecuteAsync(); }
public async Task HandleAsync(DeleteRemark command) { await _handler .Validate(async() => { var remark = await _remarkService.GetAsync(command.RemarkId); try { await _remarkService.ValidateEditorAccessOrFailAsync(command.RemarkId, command.UserId); return; } catch { if (remark.Value.Group == null) { throw; } } await _groupService.ValidateIfRemarkCanBeCanceledOrFailAsync(remark.Value.Group.Id, command.UserId); }) .Run(async() => { await _remarkService.DeleteAsync(command.RemarkId); }) .OnSuccess(async() => { var resource = _resourceFactory.Resolve <RemarkDeleted>(command.RemarkId); await _bus.PublishAsync(new RemarkDeleted(command.Request.Id, resource, command.UserId, command.RemarkId)); }) .OnCustomError(ex => _bus.PublishAsync(new DeleteRemarkRejected(command.Request.Id, command.RemarkId, command.UserId, ex.Code, ex.Message))) .OnError(async(ex, logger) => { logger.Error(ex, "Error occured while deleting a remark."); await _bus.PublishAsync(new DeleteRemarkRejected(command.Request.Id, command.RemarkId, command.UserId, OperationCodes.Error, ex.Message)); }) .ExecuteAsync(); }
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();