Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetCollection Method #99

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ManagedCode.Database.AzureTables/AzureTablesDatabase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Azure.Data.Tables;
using Humanizer;
using ManagedCode.Database.Core;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public override ValueTask DisposeAsync()

#region Get

protected override async Task<List<TItem>> GetCollectionInternalAsync(CancellationToken cancellationToken = default)
{
return await _tableClient.QueryAsync<TItem>().ToListAsync(cancellationToken);
}

protected override async Task<TItem?> GetInternalAsync(TableId id, CancellationToken cancellationToken = default)
{
try
Expand Down
7 changes: 7 additions & 0 deletions ManagedCode.Database.Core/BaseDatabaseCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public abstract class BaseDatabaseCollection<TId, TItem> : IDatabaseCollection<T

public abstract ValueTask DisposeAsync();

public Task<List<TItem>> GetCollection(CancellationToken cancellationToken = default)
{
return ExecuteAsync(GetCollectionInternalAsync(cancellationToken));
}

public Task<TItem> InsertAsync(TItem item, CancellationToken cancellationToken = default)
{
return ExecuteAsync(InsertInternalAsync(item, cancellationToken));
Expand Down Expand Up @@ -107,6 +112,8 @@ protected abstract Task<int> DeleteInternalAsync(IEnumerable<TItem> items,
CancellationToken cancellationToken = default);

protected abstract Task<bool> DeleteCollectionInternalAsync(CancellationToken cancellationToken = default);

protected abstract Task<List<TItem>> GetCollectionInternalAsync(CancellationToken cancellationToken = default);

protected abstract Task<TItem?> GetInternalAsync(TId id, CancellationToken cancellationToken = default);

Expand Down
1 change: 1 addition & 0 deletions ManagedCode.Database.Core/IDatabase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

Expand Down
3 changes: 2 additions & 1 deletion ManagedCode.Database.Core/IDatabaseCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace ManagedCode.Database.Core;
public interface IDatabaseCollection<in TId, TItem> : IDisposable, IAsyncDisposable where TItem : IItem<TId>
{
ICollectionQueryable<TItem> Query { get; }


Task<List<TItem>> GetCollection(CancellationToken cancellationToken = default);
Task<TItem> InsertAsync(TItem item, CancellationToken cancellationToken = default);
Task<int> InsertAsync(IEnumerable<TItem> items, CancellationToken cancellationToken = default);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,25 @@
return Task.FromResult(_storage.Count == 0);
}



#endregion

#region Get

protected override Task<List<TItem>> GetCollectionInternalAsync(CancellationToken cancellationToken = default)
{
return Task.FromResult(_storage.Values.ToList());
}

protected override Task<TItem> GetInternalAsync(TId id, CancellationToken cancellationToken = default)

Check warning on line 162 in ManagedCode.Database.Core/InMemory/InMemoryDatabaseCollection.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Nullability of reference types in return type doesn't match overridden member.
{
if (_storage.TryGetValue(id, out var item))
{
return Task.FromResult(item);
}

return Task.FromResult<TItem>(default);

Check warning on line 169 in ManagedCode.Database.Core/InMemory/InMemoryDatabaseCollection.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference argument for parameter 'result' in 'Task<TItem> Task.FromResult<TItem>(TItem result)'.
}

#endregion
Expand Down
5 changes: 5 additions & 0 deletions ManagedCode.Database.Cosmos/CosmosCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public override ValueTask DisposeAsync()

#region Get

protected override Task<List<TItem>> GetCollectionInternalAsync(CancellationToken cancellationToken = default)
{
return Task.FromResult(_container.GetItemLinqQueryable<TItem>().ToList());
}

protected override async Task<TItem?> GetInternalAsync(string id, CancellationToken cancellationToken = default)
{
using var queryIterator = _container.GetItemLinqQueryable<TItem>().Where(w => w.Id == id).ToFeedIterator<TItem>();
Expand Down
5 changes: 5 additions & 0 deletions ManagedCode.Database.DynamoDB/DynamoDBCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public override ValueTask DisposeAsync()

#region Get

protected override async Task<List<TItem>> GetCollectionInternalAsync(CancellationToken cancellationToken = default)
{
return await _dynamoDBContext.ScanAsync<TItem>(null, _config).GetRemainingAsync(cancellationToken);
}

protected override async Task<TItem?> GetInternalAsync(string hashKey, CancellationToken cancellationToken = default)
{
var data = await _dynamoDBContext.ScanAsync<TItem>(GetScanConditions(hashKey), _config).GetRemainingAsync(cancellationToken);
Expand Down
5 changes: 5 additions & 0 deletions ManagedCode.Database.LiteDB/LiteDBCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public override void Dispose()

#region Get

protected override Task<List<TItem>> GetCollectionInternalAsync(CancellationToken cancellationToken = default)
{
return Task.FromResult(_collection.FindAll().ToList());
}

protected override async Task<TItem?> GetInternalAsync(TId id, CancellationToken cancellationToken = default)
{
await Task.Yield();
Expand Down
9 changes: 9 additions & 0 deletions ManagedCode.Database.MongoDB/MongoDBCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public override ValueTask DisposeAsync()

#region Get

protected override async Task<List<TItem>> GetCollectionInternalAsync(CancellationToken cancellationToken = default)
{
var collectionName = _collection.CollectionNamespace.CollectionName;

var collection = await _collection.Database.GetCollection<TItem>(collectionName).Find<TItem>(_ => true).ToListAsync();

return collection;
}

protected override async Task<TItem?> GetInternalAsync(ObjectId id, CancellationToken cancellationToken = default)
{
var cursor = await _collection.FindAsync(w => w.Id == id, cancellationToken: cancellationToken);
Expand Down
5 changes: 5 additions & 0 deletions ManagedCode.Database.SQLite/SQLiteDatabaseCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public override void Dispose()

#region Get

protected override Task<List<TItem>> GetCollectionInternalAsync(CancellationToken cancellationToken = default)
{
return Task.FromResult(_database.Query<TItem>(null));
}

protected override async Task<TItem?> GetInternalAsync(TId id, CancellationToken cancellationToken = default)
{
await Task.Yield();
Expand Down
17 changes: 17 additions & 0 deletions ManagedCode.Database.Tests/BaseTests/BaseCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Threading.Tasks;
using ManagedCode.Database.Tests.TestContainers;
using Microsoft.Azure.Cosmos;
using Xunit;

namespace ManagedCode.Database.Tests.BaseTests;
Expand Down Expand Up @@ -368,6 +369,22 @@ public virtual async Task Count()

#region Get

[Fact]
public virtual async Task GetAll_ReturnOk()
{
// Arrange
await Collection.InsertAsync(CreateNewItem());
await Collection.InsertAsync(CreateNewItem());
await Collection.InsertAsync(CreateNewItem());

// Act
var getItemResult = await Collection.GetCollection();

// Assert
getItemResult.Count.Should().Be(3);
}


[Fact]
public virtual async Task GetById_ReturnOk()
{
Expand Down
6 changes: 6 additions & 0 deletions ManagedCode.Database.ZoneTree/ZoneTreeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ protected override Task<bool> DeleteCollectionInternalAsync(CancellationToken ca
return Task.FromResult(true);
}

protected override Task<List<TItem>> GetCollectionInternalAsync(CancellationToken cancellationToken = default)
{
var collection = _zoneTree.Enumerate().ToList();
return Task.FromResult(collection.Count == 0 ? null : collection)!;
}

protected override async Task<TItem> InsertOrUpdateInternalAsync(TItem item,
CancellationToken cancellationToken = default)
{
Expand Down
Loading