Skip to content

Commit

Permalink
Use a new ID for DeletedFile docs
Browse files Browse the repository at this point in the history
- fixes #112
  • Loading branch information
ddaspit committed Sep 6, 2023
1 parent 2fe198a commit b62df4b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 51 deletions.
28 changes: 5 additions & 23 deletions src/Serval.DataFiles/Services/DataFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand All @@ -108,23 +103,10 @@ public override async Task<bool> 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);
Expand Down
28 changes: 0 additions & 28 deletions tests/Serval.DataFiles.Tests/Services/DataFileServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b62df4b

Please sign in to comment.