Skip to content

Commit

Permalink
Feat: apply find by filter definition
Browse files Browse the repository at this point in the history
- to apply issue #2
  • Loading branch information
cpa-coder committed Nov 6, 2022
1 parent 2c818b5 commit eb7dda4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/MongoSharpen/DbContext/DbContext.Find.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ internal sealed partial class DbContext
public Find<T> Find<T>(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> expression)
where T : IEntity => new(this, expression);

public Find<T> Find<T>(FilterDefinition<T> definition)
where T : IEntity => new(this, _ => definition);

public Find<T, TProjection> Find<T, TProjection>() where T : IEntity => new(this);

public Find<T, TProjection> Find<T, TProjection>(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> expression)
where T : IEntity => new(this, expression);

public Find<T, TProjection> Find<T, TProjection>(FilterDefinition<T> definition)
where T : IEntity => new(this, _ => definition);
}
19 changes: 13 additions & 6 deletions src/MongoSharpen/DbContext/IDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,27 @@ Task SaveAsync<T>(IEnumerable<T> entities, CancellationToken token = default)
/// <summary>
/// Implements MongoDB's find operation.
/// </summary>
Find<T> Find<T>(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> expression)
where T : IEntity;
Find<T> Find<T>(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> expression) where T : IEntity;

/// <summary>
/// Implements MongoDB's find operation.
/// </summary>
Find<T, TProjection> Find<T, TProjection>()
where T : IEntity;
public Find<T> Find<T>(FilterDefinition<T> definition) where T : IEntity;

/// <summary>
/// Implements MongoDB's find operation.
/// </summary>
Find<T, TProjection> Find<T, TProjection>(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> expression)
where T : IEntity;
Find<T, TProjection> Find<T, TProjection>() where T : IEntity;

/// <summary>
/// Implements MongoDB's find operation.
/// </summary>
Find<T, TProjection> Find<T, TProjection>(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> expression) where T : IEntity;

/// <summary>
/// Implements MongoDB's find operation.
/// </summary>
public Find<T, TProjection> Find<T, TProjection>(FilterDefinition<T> definition) where T : IEntity;

/// <summary>
/// Returns an estimate of the number of documents in the collection ignoring global filter.
Expand Down
9 changes: 6 additions & 3 deletions tests/MongoSharpen.Test/DbContexts/DbContextTests.Find.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Bogus;
using FluentAssertions;
using MongoDB.Driver;
using MongoSharpen.Builders;
using MongoSharpen.Test.Dtos;
using MongoSharpen.Test.Entities;
Expand Down Expand Up @@ -43,7 +44,7 @@ public async Task find__with_no_projection_and_with_match_id__should_return_refe
var ctx = DbFactory.Get("library");
var reference = _bookFixture.Books.First();

var actual = await ctx.Find<Book>(x => x.Match(t => t.Id == reference.Id)).ExecuteAsync();
var actual = await ctx.Find(Builders<Book>.Filter.Eq(x => x.Id, reference.Id)).ExecuteAsync();

actual.Count.Should().Be(1);
}
Expand Down Expand Up @@ -134,7 +135,8 @@ public async Task find__with_no_projection_and_on_execute_many__should_return_it
}

[Fact]
public async Task find__with_no_projection_and_on_execute_many_using_filter_expression__should_return_items_from_specified_filter()
public async Task
find__with_no_projection_and_on_execute_many_using_filter_expression__should_return_items_from_specified_filter()
{
var first = _bookFixture.Books.First();

Expand Down Expand Up @@ -186,7 +188,7 @@ public async Task find__with_projection_and_with_match_id__should_return_referen
var ctx = DbFactory.Get("library");
var reference = _bookFixture.Books.First();

var actual = await ctx.Find<Book, BookDto>(x => x.Match(t => t.Id == reference.Id))
var actual = await ctx.Find<Book, BookDto>(Builders<Book>.Filter.Eq(i => i.Id, reference.Id))
.Project(x => new BookDto
{
Id = x.Id,
Expand Down Expand Up @@ -373,6 +375,7 @@ private async Task find__with_projection__when_set_projection_more_than_once__sh
.Project(x => new BookDto { Id = x.Id })
.ExecuteSingleAsync());
}

[Fact]
private async Task find__with_projection__when_no_projection_is_set__should_throw_exception()
{
Expand Down

0 comments on commit eb7dda4

Please sign in to comment.