Skip to content

Commit

Permalink
refactor: Renamed FileAggregate to StoredFile
Browse files Browse the repository at this point in the history
  • Loading branch information
steffenskov committed Mar 4, 2024
1 parent a38a74e commit 6c23962
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>1.3.0</Version>
<Version>1.3.1</Version>
<Authors>Steffen Skov</Authors>
<Company>Steffen Skov</Company>
<Description>Extension to SegregatedStorage adding support for Azure Blob storage</Description>
Expand Down
12 changes: 6 additions & 6 deletions src/SegregatedStorage.MongoFileRepository/MongoFileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ namespace SegregatedStorage;

internal class MongoFileRepository : IFileRepository
{
private readonly IMongoCollection<FileAggregate> _collection;
private readonly IMongoCollection<StoredFile> _collection;

public MongoFileRepository(IMongoDatabase db, string collectionName)
{
_collection = db.GetCollection<FileAggregate>(collectionName);
_collection = db.GetCollection<StoredFile>(collectionName);
}

public async ValueTask PersistAsync(FileAggregate file, CancellationToken cancellationToken = default)
public async ValueTask PersistAsync(StoredFile storedFile, CancellationToken cancellationToken = default)
{
await _collection.ReplaceOneAsync(f => f.Id == file.Id, file, new ReplaceOptions
await _collection.ReplaceOneAsync(f => f.Id == storedFile.Id, storedFile, new ReplaceOptions
{
IsUpsert = true
}, cancellationToken: cancellationToken);
}

public async ValueTask<FileAggregate> GetAsync(Guid id, CancellationToken cancellationToken = default)
public async ValueTask<StoredFile> GetAsync(Guid id, CancellationToken cancellationToken = default)
{
var cursor = await _collection.FindAsync(f => f.Id == id, cancellationToken: cancellationToken);
var result = await cursor.FirstOrDefaultAsync(cancellationToken);
Expand All @@ -37,7 +37,7 @@ public async ValueTask DeleteAsync(Guid id, CancellationToken cancellationToken
throw new FileNotFoundException($"File not found with id {id}");
}

public async ValueTask<IEnumerable<FileAggregate>> GetForDeletionAsync(CancellationToken cancellationToken = default)
public async ValueTask<IEnumerable<StoredFile>> GetForDeletionAsync(CancellationToken cancellationToken = default)
{
var cursor = await _collection.FindAsync(f => f.State == FileState.Deleting, cancellationToken: cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>1.3.0</Version>
<Version>1.3.1</Version>
<Authors>Steffen Skov</Authors>
<Company>Steffen Skov</Company>
<Description>Extension to SegregatedStorage adding support for MongoDB for storing metadata</Description>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
namespace SegregatedStorage.Aggregates;

public record FileAggregate
public record StoredFile
{
public Guid Id { get; private init; }
public string FileName { get; private init; } = default!;
public string MimeType { get; private init; } = default!;
public FileState State { get; private init; }

public static FileAggregate Create(Guid id, string filename, string mimeType)
public static StoredFile Create(Guid id, string filename, string mimeType)
{
return new FileAggregate
return new StoredFile
{
Id = id,
FileName = filename,
Expand All @@ -18,15 +18,15 @@ public static FileAggregate Create(Guid id, string filename, string mimeType)
};
}

public FileAggregate Delete()
public StoredFile Delete()
{
return this with
{
State = FileState.Deleting
};
}

public FileAggregate Uploaded()
public StoredFile Uploaded()
{
return this with
{
Expand Down
6 changes: 3 additions & 3 deletions src/SegregatedStorage/Repositories/IFileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ namespace SegregatedStorage.Repositories;

public interface IFileRepository
{
ValueTask PersistAsync(FileAggregate file, CancellationToken cancellationToken = default);
ValueTask<FileAggregate> GetAsync(Guid id, CancellationToken cancellationToken = default);
ValueTask PersistAsync(StoredFile storedFile, CancellationToken cancellationToken = default);
ValueTask<StoredFile> GetAsync(Guid id, CancellationToken cancellationToken = default);
ValueTask DeleteAsync(Guid id, CancellationToken cancellationToken = default);
ValueTask<IEnumerable<FileAggregate>> GetForDeletionAsync(CancellationToken cancellationToken = default);
ValueTask<IEnumerable<StoredFile>> GetForDeletionAsync(CancellationToken cancellationToken = default);
}
10 changes: 5 additions & 5 deletions src/SegregatedStorage/Repositories/InMemoryFileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ namespace SegregatedStorage.Repositories;

internal class InMemoryFileRepository : IFileRepository
{
private readonly ConcurrentDictionary<Guid, FileAggregate> _files = new();
private readonly ConcurrentDictionary<Guid, StoredFile> _files = new();

public ValueTask PersistAsync(FileAggregate file, CancellationToken cancellationToken = default)
public ValueTask PersistAsync(StoredFile storedFile, CancellationToken cancellationToken = default)
{
_files[file.Id] = file;
_files[storedFile.Id] = storedFile;
return ValueTask.CompletedTask;
}

public ValueTask<FileAggregate> GetAsync(Guid id, CancellationToken cancellationToken = default)
public ValueTask<StoredFile> GetAsync(Guid id, CancellationToken cancellationToken = default)
{
if (_files.TryGetValue(id, out var result))
return ValueTask.FromResult(result);
Expand All @@ -28,7 +28,7 @@ public ValueTask DeleteAsync(Guid id, CancellationToken cancellationToken = defa
return ValueTask.CompletedTask;
}

public ValueTask<IEnumerable<FileAggregate>> GetForDeletionAsync(CancellationToken cancellationToken = default)
public ValueTask<IEnumerable<StoredFile>> GetForDeletionAsync(CancellationToken cancellationToken = default)
{
var result = _files.Values.Where(file => file.State == FileState.Deleting);

Expand Down
2 changes: 1 addition & 1 deletion src/SegregatedStorage/SegregatedStorage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>1.3.0</Version>
<Version>1.3.1</Version>
<Authors>Steffen Skov</Authors>
<Company>Steffen Skov</Company>
<Description>A small library for providing segregated storage abstraction</Description>
Expand Down
10 changes: 5 additions & 5 deletions src/SegregatedStorage/Services/DeletionBackgroundService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ protected async Task DeleteFromRepositoriesAsync(CancellationToken cancellationT
}
}

private async Task DeleteFileAsync(TKey key, IFileRepository repository, FileAggregate file, CancellationToken cancellationToken)
private async Task DeleteFileAsync(TKey key, IFileRepository repository, StoredFile storedFile, CancellationToken cancellationToken)
{
var deleted = await DeleteFromStorageProviderAsync(key, file, cancellationToken);
var deleted = await DeleteFromStorageProviderAsync(key, storedFile, cancellationToken);

if (deleted) await repository.DeleteAsync(file.Id, cancellationToken);
if (deleted) await repository.DeleteAsync(storedFile.Id, cancellationToken);
}

private async Task<bool> DeleteFromStorageProviderAsync(TKey key, FileAggregate file, CancellationToken cancellationToken)
private async Task<bool> DeleteFromStorageProviderAsync(TKey key, StoredFile storedFile, CancellationToken cancellationToken)
{
var storageProvider = _storageProviderLocator.GetService(key);
try
{
await storageProvider.DeleteAsync(FilePathGenerator.GenerateFilePath(file.Id), cancellationToken);
await storageProvider.DeleteAsync(FilePathGenerator.GenerateFilePath(storedFile.Id), cancellationToken);
return true;
}
catch (FileNotFoundException)
Expand Down
6 changes: 3 additions & 3 deletions src/SegregatedStorage/Services/IStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IStorageService<in TKey>
/// <param name="data">The actual data contents of the file.</param>
/// <param name="cancellationToken">CancellationToken, can be omitted</param>
/// <returns>Id of the file uploaded.</returns>
ValueTask<FileAggregate> UploadAsync(TKey key, string filename, string mimeType, Stream data, CancellationToken cancellationToken = default);
ValueTask<StoredFile> UploadAsync(TKey key, string filename, string mimeType, Stream data, CancellationToken cancellationToken = default);

/// <summary>
/// Uploads a file to the storage service using the given id.
Expand All @@ -24,7 +24,7 @@ public interface IStorageService<in TKey>
/// <param name="data">The actual data contents of the file.</param>
/// <param name="cancellationToken">CancellationToken, can be omitted</param>
/// <returns>Id of the file uploaded.</returns>
ValueTask<FileAggregate> UploadAsync(TKey key, Guid id, string filename, string mimeType, Stream data, CancellationToken cancellationToken = default);
ValueTask<StoredFile> UploadAsync(TKey key, Guid id, string filename, string mimeType, Stream data, CancellationToken cancellationToken = default);

/// <summary>
/// Downloads a file from the storage service.
Expand All @@ -33,7 +33,7 @@ public interface IStorageService<in TKey>
/// <param name="id">Id of the file to download, will throw FileNotFoundException if no such file exists.</param>
/// <param name="cancellationToken">CancellationToken, can be omitted</param>
/// <returns>Stream with the actual data contents of the file.</returns>
ValueTask<(FileAggregate File, Stream Data)> DownloadAsync(TKey key, Guid id, CancellationToken cancellationToken = default);
ValueTask<(StoredFile File, Stream Data)> DownloadAsync(TKey key, Guid id, CancellationToken cancellationToken = default);

/// <summary>
/// Deletes a file from the storage service.
Expand Down
8 changes: 4 additions & 4 deletions src/SegregatedStorage/Services/StorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ public StorageService(IKeyServiceLocator<TKey, IFileRepository> repositoryLocato
_repositoryLocator = repositoryLocator;
}

public async ValueTask<FileAggregate> UploadAsync(TKey key, string filename, string mimeType, Stream data, CancellationToken cancellationToken = default)
public async ValueTask<StoredFile> UploadAsync(TKey key, string filename, string mimeType, Stream data, CancellationToken cancellationToken = default)
{
var id = Guid.NewGuid();
return await UploadAsync(key, id, filename, mimeType, data, cancellationToken);
}

public async ValueTask<FileAggregate> UploadAsync(TKey key, Guid id, string filename, string mimeType, Stream data, CancellationToken cancellationToken = default)
public async ValueTask<StoredFile> UploadAsync(TKey key, Guid id, string filename, string mimeType, Stream data, CancellationToken cancellationToken = default)
{
var repository = _repositoryLocator.GetService(key);
var storageProvider = _storageProviderLocator.GetService(key);
var file = FileAggregate.Create(id, filename, mimeType);
var file = StoredFile.Create(id, filename, mimeType);
await repository.PersistAsync(file, cancellationToken);

try
Expand All @@ -39,7 +39,7 @@ public async ValueTask<FileAggregate> UploadAsync(TKey key, Guid id, string file
}
}

public async ValueTask<(FileAggregate File, Stream Data)> DownloadAsync(TKey key, Guid id, CancellationToken cancellationToken = default)
public async ValueTask<(StoredFile File, Stream Data)> DownloadAsync(TKey key, Guid id, CancellationToken cancellationToken = default)
{
var repository = _repositoryLocator.GetService(key);
var storageProvider = _storageProviderLocator.GetService(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async Task PersistAsync_Valid_IsPersisted()
{
// Arrange
var repository = _repositoryLocator.GetService(42);
var file = FileAggregate.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
var file = StoredFile.Create(Guid.NewGuid(), "image.jpg", "image/jpg");

// Act
await repository.PersistAsync(file);
Expand All @@ -31,7 +31,7 @@ public async Task PersistAsync_IdAlreadyExists_IsOverwritten()
{
// Arrange
var repository = _repositoryLocator.GetService(42);
var file = FileAggregate.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
var file = StoredFile.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
await repository.PersistAsync(file);

var updatedFile = file.Uploaded();
Expand Down Expand Up @@ -61,7 +61,7 @@ public async Task GetAsync_ExistOnOtherKey_Throws()
// Arrange
var repository = _repositoryLocator.GetService(42);
var otherRepository = _repositoryLocator.GetService(43);
var file = FileAggregate.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
var file = StoredFile.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
await repository.PersistAsync(file);

// Act && Assert
Expand All @@ -73,7 +73,7 @@ public async Task GetAsync_Exist_IsRetrieved()
{
// Arrange
var repository = _repositoryLocator.GetService(42);
var file = FileAggregate.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
var file = StoredFile.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
await repository.PersistAsync(file);

// Act
Expand All @@ -88,7 +88,7 @@ public async Task DeleteAsync_Exist_IsDeleted()
{
// Arrange
var repository = _repositoryLocator.GetService(42);
var file = FileAggregate.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
var file = StoredFile.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
await repository.PersistAsync(file);

// Act
Expand Down Expand Up @@ -127,9 +127,9 @@ public async Task GetForDeletionAsync_SomeExist_ReturnsOnlyDeleted()
{
// Arrange
var repository = _repositoryLocator.GetService(42);
var file1 = FileAggregate.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
var file2 = FileAggregate.Create(Guid.NewGuid(), "image.jpg", "image/jpg").Delete();
var file3 = FileAggregate.Create(Guid.NewGuid(), "image.jpg", "image/jpg").Delete();
var file1 = StoredFile.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
var file2 = StoredFile.Create(Guid.NewGuid(), "image.jpg", "image/jpg").Delete();
var file3 = StoredFile.Create(Guid.NewGuid(), "image.jpg", "image/jpg").Delete();
await repository.PersistAsync(file1);
await repository.PersistAsync(file2);
await repository.PersistAsync(file3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public async Task PersistAsync_Valid_IsPersisted()
{
// Arrange
var repository = new InMemoryFileRepository();
var file = FileAggregate.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
var file = StoredFile.Create(Guid.NewGuid(), "image.jpg", "image/jpg");

// Act
await repository.PersistAsync(file);
Expand All @@ -23,7 +23,7 @@ public async Task PersistAsync_IdAlreadyExists_IsOverwritten()
{
// Arrange
var repository = new InMemoryFileRepository();
var file = FileAggregate.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
var file = StoredFile.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
await repository.PersistAsync(file);

var updatedFile = file.Uploaded();
Expand Down Expand Up @@ -53,7 +53,7 @@ public async Task GetAsync_ExistOnOtherKey_Throws()
// Arrange
var repository = new InMemoryFileRepository();
var otherRepository = new InMemoryFileRepository();
var file = FileAggregate.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
var file = StoredFile.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
await repository.PersistAsync(file);

// Act && Assert
Expand All @@ -65,7 +65,7 @@ public async Task GetAsync_Exist_IsRetrieved()
{
// Arrange
var repository = new InMemoryFileRepository();
var file = FileAggregate.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
var file = StoredFile.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
await repository.PersistAsync(file);

// Act
Expand All @@ -80,7 +80,7 @@ public async Task DeleteAsync_Exist_IsDeleted()
{
// Arrange
var repository = new InMemoryFileRepository();
var file = FileAggregate.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
var file = StoredFile.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
await repository.PersistAsync(file);

// Act
Expand Down Expand Up @@ -119,9 +119,9 @@ public async Task GetForDeletionAsync_SomeExist_ReturnsOnlyDeleted()
{
// Arrange
var repository = new InMemoryFileRepository();
var file1 = FileAggregate.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
var file2 = FileAggregate.Create(Guid.NewGuid(), "image.jpg", "image/jpg").Delete();
var file3 = FileAggregate.Create(Guid.NewGuid(), "image.jpg", "image/jpg").Delete();
var file1 = StoredFile.Create(Guid.NewGuid(), "image.jpg", "image/jpg");
var file2 = StoredFile.Create(Guid.NewGuid(), "image.jpg", "image/jpg").Delete();
var file3 = StoredFile.Create(Guid.NewGuid(), "image.jpg", "image/jpg").Delete();
await repository.PersistAsync(file1);
await repository.PersistAsync(file2);
await repository.PersistAsync(file3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task DeleteFromRepositoriesAsync_NoFiles_DoesNothing()
var getForDeletionCalled = false;
var repository = Substitute.For<IFileRepository>();
repository.When(rep => rep.GetForDeletionAsync()).Do(_ => { getForDeletionCalled = true; });
repository.GetForDeletionAsync().Returns(ValueTask.FromResult(Enumerable.Empty<FileAggregate>()));
repository.GetForDeletionAsync().Returns(ValueTask.FromResult(Enumerable.Empty<StoredFile>()));

var repositoryLocator = Substitute.For<IKeyServiceLocator<int, IFileRepository>>();
repositoryLocator.GetServices().Returns([(42, repository)]);
Expand All @@ -50,7 +50,7 @@ public async Task DeleteFromRepositoriesAsync_Files_DeletesThem()
// Arrange
const int key1 = 42;
const int key2 = 1337;
var file = FileAggregate.Create(Guid.NewGuid(), "hello world", "plain/text");
var file = StoredFile.Create(Guid.NewGuid(), "hello world", "plain/text");
var storageProviderDeleteCalled = new Dictionary<int, bool>();
var repositoryDeleteCalled = new Dictionary<int, bool>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task DownloadAsync_DoesNotExist_Throws()
public async Task DownloadAsync_StateIsDeleting_Throws()
{
// Arrange
var file = FileAggregate.Create(Guid.NewGuid(), "hello.txt", "text/plain").Delete();
var file = StoredFile.Create(Guid.NewGuid(), "hello.txt", "text/plain").Delete();
await _repositoryLocator.GetService(42).PersistAsync(file);

// Act && Assert
Expand All @@ -84,7 +84,7 @@ public async Task DownloadAsync_StateIsDeleting_Throws()
public async Task DownloadAsync_StateIsAwaiting_Throws()
{
// Arrange
var file = FileAggregate.Create(Guid.NewGuid(), "hello.txt", "text/plain");
var file = StoredFile.Create(Guid.NewGuid(), "hello.txt", "text/plain");
await _repositoryLocator.GetService(42).PersistAsync(file);

// Act && Assert
Expand Down Expand Up @@ -123,7 +123,7 @@ public async Task DeleteAsync_DoesNotExist_Throws()
public async Task DeleteAsync_StateIsDeleting_Throws()
{
// Arrange
var file = FileAggregate.Create(Guid.NewGuid(), "hello.txt", "text/plain").Delete();
var file = StoredFile.Create(Guid.NewGuid(), "hello.txt", "text/plain").Delete();
await _repositoryLocator.GetService(42).PersistAsync(file);

// Act && Assert
Expand Down

0 comments on commit 6c23962

Please sign in to comment.