From b62df4ba8c5dfbbc7d6e8ced63ecab8b29055736 Mon Sep 17 00:00:00 2001 From: Damien Daspit Date: Wed, 6 Sep 2023 15:22:18 -0500 Subject: [PATCH] Use a new ID for DeletedFile docs - fixes #112 --- .../Services/DataFileService.cs | 28 ++++--------------- .../Services/DataFileServiceTests.cs | 28 ------------------- 2 files changed, 5 insertions(+), 51 deletions(-) diff --git a/src/Serval.DataFiles/Services/DataFileService.cs b/src/Serval.DataFiles/Services/DataFileService.cs index 701c0c91..2a522488 100644 --- a/src/Serval.DataFiles/Services/DataFileService.cs +++ b/src/Serval.DataFiles/Services/DataFileService.cs @@ -77,12 +77,7 @@ public async Task CreateAsync(DataFile dataFile, Stream stream, CancellationToke else { await _deletedFiles.InsertAsync( - new DeletedFile - { - Id = originalDataFile.Id, - Filename = originalDataFile.Filename, - DeletedAt = DateTime.UtcNow - }, + new DeletedFile { Filename = originalDataFile.Filename, DeletedAt = DateTime.UtcNow }, cancellationToken ); } @@ -108,23 +103,10 @@ public override async Task DeleteAsync(string id, CancellationToken cancel DataFile? dataFile = await Entities.DeleteAsync(id, cancellationToken); if (dataFile is not null) { - try - { - await _deletedFiles.InsertAsync( - new DeletedFile - { - Id = id, - Filename = dataFile.Filename, - DeletedAt = DateTime.UtcNow - }, - cancellationToken - ); - } - catch (DuplicateKeyException) - { - // if it was already deleted, return false - return false; - } + await _deletedFiles.InsertAsync( + new DeletedFile { Filename = dataFile.Filename, DeletedAt = DateTime.UtcNow }, + cancellationToken + ); } await _mediator.Publish(new DataFileDeleted { DataFileId = id }, cancellationToken); await _dataAccessContext.CommitTransactionAsync(CancellationToken.None); diff --git a/tests/Serval.DataFiles.Tests/Services/DataFileServiceTests.cs b/tests/Serval.DataFiles.Tests/Services/DataFileServiceTests.cs index 3257ca49..81cbf6e6 100644 --- a/tests/Serval.DataFiles.Tests/Services/DataFileServiceTests.cs +++ b/tests/Serval.DataFiles.Tests/Services/DataFileServiceTests.cs @@ -115,34 +115,6 @@ public async Task DeleteAsync_DoesNotExist() Assert.That(deleted, Is.False); } - [Test] - public async Task DeleteAsync_Twice() - { - var env = new TestEnvironment(); - env.DataFiles.Add( - new DataFile - { - Id = DATA_FILE_ID, - Name = "file1", - Filename = "file1.txt" - } - ); - bool deleted = await env.Service.DeleteAsync(DATA_FILE_ID); - Assert.That(deleted, Is.True); - // The same file would not be added twice, but this is to check - env.DataFiles.Add( - new DataFile - { - Id = DATA_FILE_ID, - Name = "file1", - Filename = "file1.txt" - } - ); - deleted = await env.Service.DeleteAsync(DATA_FILE_ID); - // The real condition is a race condition - trying to delete the same file twice (at the same time). One should fail. - Assert.That(deleted, Is.False); - } - private class TestEnvironment { public TestEnvironment()