diff --git a/src/SIL.Harmony.Tests/ResourceTests/RemoteResourcesTests.cs b/src/SIL.Harmony.Tests/ResourceTests/RemoteResourcesTests.cs index c871f29..544c5e9 100644 --- a/src/SIL.Harmony.Tests/ResourceTests/RemoteResourcesTests.cs +++ b/src/SIL.Harmony.Tests/ResourceTests/RemoteResourcesTests.cs @@ -32,8 +32,8 @@ private string CreateFile(string contents, [CallerMemberName] string fileName = { var file = CreateFile(contents, fileName); //because resource service is null the file is not uploaded - var resourceId = await _resourceService.AddLocalResource(file, _localClientId, resourceService: null); - return (resourceId, file); + var crdtResource = await _resourceService.AddLocalResource(file, _localClientId, resourceService: null); + return (crdtResource.Id, file); } [Fact] @@ -69,11 +69,10 @@ public async Task CanUploadFileToRemote() var localFile = CreateFile(fileContents); //act - var resourceId = - await _resourceService.AddLocalResource(localFile, _localClientId, resourceService: _remoteServiceMock); + var crdtResource = await _resourceService.AddLocalResource(localFile, _localClientId, resourceService: _remoteServiceMock); - var resource = await DataModel.GetLatest(resourceId); + var resource = await DataModel.GetLatest(crdtResource.Id); ArgumentNullException.ThrowIfNull(resource); ArgumentNullException.ThrowIfNull(resource.RemoteId); _remoteServiceMock.ReadFile(resource.RemoteId).Should().Be(fileContents); @@ -119,10 +118,10 @@ public async Task CanGetALocalResourceGivenAnId() { var file = CreateFile("resource"); //because resource service is null the file is not uploaded - var resourceId = await _resourceService.AddLocalResource(file, _localClientId, resourceService: null); + var crdtResource = await _resourceService.AddLocalResource(file, _localClientId, resourceService: null); //act - var localResource = await _resourceService.GetLocalResource(resourceId); + var localResource = await _resourceService.GetLocalResource(crdtResource.Id); localResource.Should().NotBeNull(); @@ -136,4 +135,30 @@ public async Task LocalResourceIsNullIfNotDownloaded() var localResource = await _resourceService.GetLocalResource(resourceId); localResource.Should().BeNull(); } + + [Fact] + public async Task CanListAllResources() + { + var (localResourceId, localResourcePath) = await SetupLocalFile("localOnly", "localOnly.txt"); + var (remoteResourceId, remoteId) = await SetupRemoteResource("remoteOnly"); + var localAndRemoteResource = await _resourceService.AddLocalResource(CreateFile("localAndRemove"), _localClientId, resourceService: _remoteServiceMock); + + var crdtResources = await _resourceService.AllResources(); + crdtResources.Should().BeEquivalentTo( + [ + new CrdtResource + { + Id = localResourceId, + LocalPath = localResourcePath, + RemoteId = null + }, + new CrdtResource + { + Id = remoteResourceId, + LocalPath = null, + RemoteId = remoteId + }, + localAndRemoteResource + ]); + } } diff --git a/src/SIL.Harmony.Tests/ResourceTests/WordResourceTests.cs b/src/SIL.Harmony.Tests/ResourceTests/WordResourceTests.cs index 67a79bb..e7fc702 100644 --- a/src/SIL.Harmony.Tests/ResourceTests/WordResourceTests.cs +++ b/src/SIL.Harmony.Tests/ResourceTests/WordResourceTests.cs @@ -25,12 +25,12 @@ public async Task CanReferenceAResourceFromAWord() var imageFile = CreateFile("not image data"); //set commit date for add local resource MockTimeProvider.SetNextDateTime(NextDate()); - var resourceId = await _resourceService.AddLocalResource(imageFile, Guid.NewGuid(), resourceService: _remoteServiceMock); - await WriteNextChange(new AddWordImageChange(_entity1Id, resourceId)); + var resource = await _resourceService.AddLocalResource(imageFile, Guid.NewGuid(), resourceService: _remoteServiceMock); + await WriteNextChange(new AddWordImageChange(_entity1Id, resource.Id)); var word = await DataModel.GetLatest(_entity1Id); word.Should().NotBeNull(); - word!.ImageResourceId.Should().Be(resourceId); + word!.ImageResourceId.Should().Be(resource.Id); var localResource = await _resourceService.GetLocalResource(word.ImageResourceId!.Value); diff --git a/src/SIL.Harmony/ResourceService.cs b/src/SIL.Harmony/ResourceService.cs index 8bdc01b..fec818b 100644 --- a/src/SIL.Harmony/ResourceService.cs +++ b/src/SIL.Harmony/ResourceService.cs @@ -26,7 +26,10 @@ private void ValidateResourcesSetup() if (!_crdtConfig.Value.RemoteResourcesEnabled) throw new RemoteResourceNotEnabledException(); } - public async Task AddLocalResource(string resourcePath, Guid clientId, Guid id = default, IRemoteResourceService? resourceService = null) + public async Task AddLocalResource(string resourcePath, + Guid clientId, + Guid id = default, + IRemoteResourceService? resourceService = null) { ValidateResourcesSetup(); var localResource = new LocalResource @@ -41,13 +44,23 @@ public async Task AddLocalResource(string resourcePath, Guid clientId, Gui { var uploadResult = await resourceService.UploadResource(localResource.LocalPath); await _dataModel.AddChange(clientId, new CreateRemoteResourceChange(localResource.Id, uploadResult.RemoteId)); + await transaction.CommitAsync(); + return new CrdtResource + { + Id = localResource.Id, + RemoteId = uploadResult.RemoteId, + LocalPath = localResource.LocalPath + }; } - else - { - await _dataModel.AddChange(clientId, new CreateRemoteResourcePendingUploadChange(localResource.Id)); - } + + await _dataModel.AddChange(clientId, new CreateRemoteResourcePendingUploadChange(localResource.Id)); await transaction.CommitAsync(); - return localResource.Id; + return new CrdtResource + { + Id = localResource.Id, + RemoteId = null, + LocalPath = localResource.LocalPath + }; } public async Task ListResourcesPendingUpload() @@ -106,7 +119,11 @@ public async Task ListResourcesPendingDownload() public async Task DownloadResource(Guid resourceId, IRemoteResourceService remoteResourceService) { ValidateResourcesSetup(); - return await DownloadResource(await _dataModel.GetLatest(resourceId) ?? throw new EntityNotFoundException("Unable to find remote resource"), remoteResourceService); + return await DownloadResource( + await _dataModel.GetLatest(resourceId) ?? + throw new EntityNotFoundException("Unable to find remote resource"), + remoteResourceService + ); } public async Task DownloadResource(RemoteResource remoteResource, IRemoteResourceService remoteResourceService) @@ -141,6 +158,5 @@ public async Task AllResources() RemoteId = r?.RemoteId, LocalPath = l?.LocalPath }).ToArray(); - } } \ No newline at end of file