Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

Commit

Permalink
Merge branch 'release/0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
patriksvensson committed Aug 18, 2018
2 parents cbe0a33 + 4fd4cac commit 499d0f5
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 61 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### New in 0.5.0 (Released 2018/08/19)

- [__#11__](https://github.com/spectresystems/spectre.query/issues/11) Add way of getting the parsed expression from a query

### New in 0.4.0 (Released 2018/08/18)

- [__#9__](https://github.com/spectresystems/spectre.query/issues/9) Add support for mapping inherited items
Expand Down
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ This project is currently under active development and might not be ready for pr

* Navigation properties are not supported. For more complex queries, use [Query Types](https://github.com/aspnet/EntityFramework.Docs/blob/master/entity-framework/core/modeling/query-types.md).

* There's currently no support for non-integer numbers.

## Usage

### 1. Install the NuGet package
Expand Down Expand Up @@ -111,20 +109,21 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
[Route("api/movies")]
public class MovieController : ControllerBase
{
private readonly IQueryProviderSession<MovieContext> _provider;
private readonly MovieContext _context;
private readonly IQueryProvider<MovieContext> _provider;

public MovieController(IQueryProviderSession<MovieContext> provider)
public MovieController(MovieContext context, IQueryProvider<MovieContext> provider)
{
_context = context;
_provider = provider;
}

[HttpGet]
public IActionResult<List<Movie>> Query([FromHeader]string query = "Rating > 80 AND !Seen")
{
return _provider
.Query<Movie>(query)
.OrderByDescending(x => x.Rating)
.ToList();
return _provider.Query<Movie>(_context, query)
.OrderByDescending(movie => movie.Rating)
.ToList()
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ namespace Spectre.Query.AspNetCore.Example.Controllers
{
public class HomeController : Controller
{
private readonly IQueryProviderSession<MovieContext> _provider;
private readonly MovieContext _context;
private readonly IQueryProvider<MovieContext> _provider;

public HomeController(IQueryProviderSession<MovieContext> provider)
public HomeController(MovieContext context, IQueryProvider<MovieContext> provider)
{
_context = context;
_provider = provider;
}

[HttpGet]
public IActionResult Index(string query = null)
{
var movies = _provider
.Query<Movie>(query)
.OrderByDescending(x => x.Rating);
var movies = _provider.Query<Movie>(_context, query)
.OrderByDescending(movie => movie.Rating)
.ToList();

return View(new SearchResultModel<Movie>
{
Movies = movies.ToList(),
Movies = movies,
Query = query
});
}
Expand All @@ -37,13 +39,13 @@ public IActionResult Search(SearchResultModel<Movie> model)
[HttpGet]
public IActionResult Projection(string query = null)
{
var movies = _provider
.Query<MovieProjection>(query)
.OrderByDescending(x => x.Rating);
var movies = _provider.Query<MovieProjection>(_context, query)
.OrderByDescending(x => x.Rating)
.ToList();

return View(new SearchResultModel<MovieProjection>
{
Movies = movies.ToList(),
Movies = movies,
Query = query
});
}
Expand Down
9 changes: 0 additions & 9 deletions src/Spectre.Query.AspNetCore/IQueryProviderSession.cs

This file was deleted.

24 changes: 0 additions & 24 deletions src/Spectre.Query.AspNetCore/Internal/QueryProviderSession.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using Microsoft.EntityFrameworkCore;
using Spectre.Query;
using Spectre.Query.AspNetCore;

namespace Microsoft.Extensions.DependencyInjection
{
Expand All @@ -17,12 +16,9 @@ public static void AddQueryProvider<TContext>(this IServiceCollection services,
using (var scope = factory.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<TContext>();
return QueryProviderBuilder.Build<TContext>(context, configurator);
return QueryProviderBuilder.Build(context, configurator);
}
});

// Register the query provider session.
services.AddScoped<IQueryProviderSession<TContext>, QueryProviderSession<TContext>>();
}
}
}
5 changes: 4 additions & 1 deletion src/Spectre.Query/IQueryProvider.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System.Linq;
using System;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;

namespace Spectre.Query
{
public interface IQueryProvider<TContext>
where TContext : DbContext
{
Expression<Func<TEntity, bool>> Compile<TEntity>(string query) where TEntity : class;
IQueryable<TEntity> Query<TEntity>(TContext context, string query) where TEntity : class;
}
}
23 changes: 19 additions & 4 deletions src/Spectre.Query/Internal/QueryProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using Spectre.Query.Internal.Configuration;
using Spectre.Query.Internal.Expressions;
Expand All @@ -17,7 +18,7 @@ public QueryProvider(QueryConfiguration<TContext> configuration)
_configuration = configuration;
}

public IQueryable<TEntity> Query<TEntity>(TContext context, string query)
public Expression<Func<TEntity, bool>> Compile<TEntity>(string query)
where TEntity : class
{
if (!_configuration.Mappings.TryGetValue(typeof(TEntity), out var entityConfiguration))
Expand All @@ -31,10 +32,24 @@ public IQueryable<TEntity> Query<TEntity>(TContext context, string query)
expression = ConversionRewriter.Rewrite(expression);

// Translate the expression.
var result = QueryTranslator<TContext, TEntity>.Translate(expression);
return QueryTranslator<TContext, TEntity>.Translate(expression);
}

public IQueryable<TEntity> Query<TEntity>(TContext context, string query)
where TEntity : class
{
if (!_configuration.Mappings.TryGetValue(typeof(TEntity), out var entityConfiguration))
{
throw new InvalidOperationException("Entity has not been configured.");
}

// Compile the query.
var expression = Compile<TEntity>(query);

// Execute the query.
return entityConfiguration.IsQueryType
? context.Query<TEntity>().Where(result)
: context.Set<TEntity>().Where(result);
? context.Query<TEntity>().Where(expression)
: context.Set<TEntity>().Where(expression);
}
}
}

0 comments on commit 499d0f5

Please sign in to comment.