Skip to content
Merged
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
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ public class BlogFilterDto : FilterBase<Blog>
- Let's create a sample Controller and get the DTO from querystring

```csharp
using AutoFilterer.Extensions; // <-- To call extension methods
//...
public class BlogsController : ControllerBase
{
[HttpGet]
public IActionResult Get([FromQuery]BlogFilterDto filter)
{
using(var db = new MyDbContext())
{
var blogList = filter.ApplyFilterTo(db.Blogs).ToList();
var blogList = db.Blogs.ApplyFilter(filter);
return Ok(blogList);
}
}
Expand Down Expand Up @@ -206,9 +208,8 @@ public class BlogsController : ControllerBase
{
using(var db = new MyDbContext())
{
// You just need to apply an ordering before calling ApplyFilterTo() method
var query = db.Blogs.OrderByDescending(o => o.PublishDate);
var blogList = filter.ApplyFilterTo(query).ToList();
// You just need to apply an ordering before calling ApplyFilter() method
var blogList = db.Blogs.OrderBy(o => o.PublishDate).ApplyFilter(filter);
return Ok(blogList);
}
}
Expand Down
24 changes: 17 additions & 7 deletions sandbox/WebApplication.API/Controllers/BlogsController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoFilterer.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebApplication.API.Dtos;
using WebApplication.API.Models;
using WebApplication.API.Repository;

namespace WebApplication.API.Controllers
Expand All @@ -14,21 +14,31 @@ namespace WebApplication.API.Controllers
public class BlogsController : ControllerBase
{
// api/Blogs
// api/Blogs?page=1
// api/Blogs?page=2&perPage=4
// api/Blogs?title=hello
// api/Blogs?title=hello
// api/Blogs?isPublished=False
// api/Blogs?priority.min=2

[HttpGet]
public IActionResult Get([FromQuery]BlogFilterDto filter)
{
var db = new BlogDummyData();

var query = db.Blogs.OrderByDescending(o => o.PublishDate);
var list = db.Blogs.ApplyFilter(filter);

return Ok(list);
}

// api/Blogs
// api/Blogs?page=1
// api/Blogs?page=2&perPage=4
// api/Blogs?title=hello
// api/Blogs?priority.min=5
[HttpGet("paged")]
public IActionResult GetWithPages(BlogPaginationFilterDto filter)
{
var db = new BlogDummyData();

var list = filter.ApplyFilterTo(query).ToList();
var list = db.Blogs.OrderBy(o => o.PublishDate).ApplyFilter(filter);

return Ok(list);
}
Expand Down
2 changes: 1 addition & 1 deletion sandbox/WebApplication.API/Dtos/BlogFilterDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace WebApplication.API.Dtos
{
public class BlogFilterDto : PaginationFilterBase<Blog>
public class BlogFilterDto : FilterBase<Blog>
{
public int? CategoryId { get; set; }

Expand Down
25 changes: 25 additions & 0 deletions sandbox/WebApplication.API/Dtos/BlogPaginationFilterDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using AutoFilterer.Attributes;
using AutoFilterer.Enums;
using AutoFilterer.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebApplication.API.Dtos
{
public class BlogPaginationFilterDto : PaginationFilterBase
{
public int? CategoryId { get; set; }

public Range<int> Priority { get; set; }

[StringFilterOptions(StringFilterOption.Contains)]
public string Title { get; set; }

public bool? IsPublished { get; set; }

public Range<DateTime> PublishDate { get; set; }
}
}
13 changes: 13 additions & 0 deletions src/AutoFilterer/Abstractions/IFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoFilterer.Abstractions
{
public interface IFilter : IFilterableType
{
IQueryable<TEntity> ApplyFilterTo<TEntity>(IQueryable<TEntity> query);
}
}
11 changes: 9 additions & 2 deletions src/AutoFilterer/Extensions/QueryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using AutoFilterer.Abstractions;
using AutoFilterer.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -27,5 +29,10 @@ public static IQueryable<T> ToPaged<T>(this IQueryable<T> source, int page, int

return (source as IOrderedQueryable<T>).Skip((page - 1) * pageSize).Take(pageSize);
}
}

public static IQueryable<T> ApplyFilter<T>(this IQueryable<T> source, IFilter filter)
{
return filter.ApplyFilterTo(source);
}
}
}
15 changes: 1 addition & 14 deletions src/AutoFilterer/Types/FilterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace AutoFilterer.Types
{
public class FilterBase : IFilterableType
public class FilterBase : IFilter
{
/// <summary>
/// Automaticly Applies Where() to IQuaryable collection. Please visit project site for more information and customizations.
Expand Down Expand Up @@ -77,17 +77,4 @@ public virtual Expression<Func<TModel, bool>> BuildExpression<TModel>(PropertyIn
return Expression.Lambda<Func<TModel, bool>>(comparison, parameter);
}
}

public class FilterBase<TEntity> : FilterBase
{
/// <summary>
/// Automaticly Applies Where() to IQuaryable collection. Please visit project site for more information and customizations.
/// </summary>
/// <param name="query"></param>
public virtual IQueryable<TEntity> ApplyFilterTo(IQueryable<TEntity> query)
{
return ApplyFilterTo<TEntity>(query);
}
}

}
25 changes: 25 additions & 0 deletions src/AutoFilterer/Types/FilterBase{TEntity}.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoFilterer.Types
{
/// <summary>
/// Base class of filter Data Transfer Objects (DTO).
/// If you don't have access to your Entity models from dto (if they're in seperated libraries), just inherit non-generic type of FilterBase which is <see cref="FilterBase"/>.
/// </summary>
/// <typeparam name="TEntity">Entity Type.</typeparam>
public class FilterBase<TEntity> : FilterBase
{
/// <summary>
/// Automaticly Applies Where() to IQuaryable collection. Please visit project site for more information and customizations.
/// </summary>
/// <param name="query"></param>
public virtual IQueryable<TEntity> ApplyFilterTo(IQueryable<TEntity> query)
{
return ApplyFilterTo<TEntity>(query);
}
}
}
4 changes: 2 additions & 2 deletions src/AutoFilterer/Types/Range.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class Range<T> : IRange<T>, IRange, IEquatable<string>, IFormattable
{
public Range()
{

}

public Range(string value)
{
var parsed = Parse<T>(value);
Expand Down Expand Up @@ -104,7 +104,7 @@ public static T1 GetMaxValue<T1>()
maxValue = ulong.MaxValue;
break;
default:
maxValue = default(T1);//set default value
maxValue = default(T1); // Set as default value.
break;
}
return (T1)maxValue;
Expand Down