Skip to content
This repository has been archived by the owner on Jul 2, 2019. It is now read-only.

Commit

Permalink
Drop query context, use IQueryable extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Jun 5, 2019
1 parent 466041d commit bb531bf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/BioEngine.Core.API/Entities/ContentItemVersionInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using BioEngine.Core.Users;
using BioEngine.Core.Abstractions;

namespace BioEngine.Core.API.Entities
{
Expand Down
49 changes: 24 additions & 25 deletions src/BioEngine.Core.API/ResponseRestController.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using BioEngine.Core.Abstractions;
using BioEngine.Core.API.Interfaces;
using BioEngine.Core.API.Models;
using BioEngine.Core.API.Response;
using BioEngine.Core.DB.Queries;
using BioEngine.Core.Extensions;
using BioEngine.Core.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;

namespace BioEngine.Core.API
{
Expand All @@ -39,7 +39,7 @@ protected virtual async Task<TResponse> MapRestModelAsync(TEntity domainModel)
public virtual async Task<ActionResult<ListResponse<TResponse>>> GetAsync([FromQuery] int limit = 20,
[FromQuery] int offset = 0, [FromQuery] string order = null, [FromQuery] string filter = null)
{
var result = await Repository.GetAllAsync(GetQueryContext(limit, offset, order, filter));
var result = await Repository.GetAllAsync(q => ConfigureQuery(q, limit, offset, order, filter));
return await ListAsync(result);
}

Expand All @@ -66,28 +66,13 @@ public virtual async Task<ActionResult<TResponse>> NewAsync()
public virtual async Task<ActionResult<int>> CountAsync([FromQuery] int limit = 20,
[FromQuery] int offset = 0, [FromQuery] string order = null, [FromQuery] string filter = null)
{
var result = await Repository.CountAsync(GetQueryContext(limit, offset, order, filter));
var result = await Repository.CountAsync(query => ConfigureQuery(query, limit, offset, order, filter));
return Ok(result);
}

protected QueryContext<TEntity> GetQueryContext(int limit, int offset, string order, string filter)
protected IQueryable<TEntity> ConfigureQuery(IQueryable<TEntity> query, int limit, int offset, string order,
string filter)
{
var context = new QueryContext<TEntity>();
if (limit > 0)
{
context.Limit = limit;
}

if (offset > 0)
{
context.Offset = offset;
}

if (!string.IsNullOrEmpty(order))
{
context.SetOrderByString(order);
}

if (!string.IsNullOrEmpty(filter) &&
filter != "null")
{
Expand All @@ -99,14 +84,28 @@ protected QueryContext<TEntity> GetQueryContext(int limit, int offset, string or

var data = Convert.FromBase64String(filter);
var decodedString = HttpUtility.UrlDecode(Encoding.UTF8.GetString(data));
var where = JsonConvert.DeserializeObject<List<QueryContextConditionsGroup>>(decodedString);
if (where != null)
if (!string.IsNullOrEmpty(decodedString))
{
context.SetWhere(where);
query = query.WhereByString(decodedString);
}
}

return context;
if (!string.IsNullOrEmpty(order))
{
query = query.OrderByString(order);
}

if (limit > 0)
{
query = query.Take(limit);
}

if (offset > 0)
{
query = query.Skip(offset);
}

return query;
}

protected async Task<ActionResult<ListResponse<TResponse>>> ListAsync(
Expand Down

0 comments on commit bb531bf

Please sign in to comment.