From 7df8a86d36f9d71fd376fde7af5a0d7460e845ce Mon Sep 17 00:00:00 2001 From: enisn Date: Sat, 3 Aug 2019 18:26:32 +0300 Subject: [PATCH 1/5] Seperation different files of FilterBase --- src/AutoFilterer/Types/FilterBase.cs | 15 +---------- src/AutoFilterer/Types/FilterBase{TEntity}.cs | 25 +++++++++++++++++++ src/AutoFilterer/Types/Range.cs | 4 +-- 3 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 src/AutoFilterer/Types/FilterBase{TEntity}.cs diff --git a/src/AutoFilterer/Types/FilterBase.cs b/src/AutoFilterer/Types/FilterBase.cs index 7d8dd62..350e5f3 100644 --- a/src/AutoFilterer/Types/FilterBase.cs +++ b/src/AutoFilterer/Types/FilterBase.cs @@ -11,7 +11,7 @@ namespace AutoFilterer.Types { - public class FilterBase : IFilterableType + public class FilterBase : IFilter { /// /// Automaticly Applies Where() to IQuaryable collection. Please visit project site for more information and customizations. @@ -77,17 +77,4 @@ public virtual Expression> BuildExpression(PropertyIn return Expression.Lambda>(comparison, parameter); } } - - public class FilterBase : FilterBase - { - /// - /// Automaticly Applies Where() to IQuaryable collection. Please visit project site for more information and customizations. - /// - /// - public virtual IQueryable ApplyFilterTo(IQueryable query) - { - return ApplyFilterTo(query); - } - } - } diff --git a/src/AutoFilterer/Types/FilterBase{TEntity}.cs b/src/AutoFilterer/Types/FilterBase{TEntity}.cs new file mode 100644 index 0000000..3586f11 --- /dev/null +++ b/src/AutoFilterer/Types/FilterBase{TEntity}.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutoFilterer.Types +{ + /// + /// 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 . + /// + /// Entity Type. + public class FilterBase : FilterBase + { + /// + /// Automaticly Applies Where() to IQuaryable collection. Please visit project site for more information and customizations. + /// + /// + public virtual IQueryable ApplyFilterTo(IQueryable query) + { + return ApplyFilterTo(query); + } + } +} diff --git a/src/AutoFilterer/Types/Range.cs b/src/AutoFilterer/Types/Range.cs index ffe6492..533f7aa 100644 --- a/src/AutoFilterer/Types/Range.cs +++ b/src/AutoFilterer/Types/Range.cs @@ -12,8 +12,8 @@ public class Range : IRange, IRange, IEquatable, IFormattable { public Range() { - } + public Range(string value) { var parsed = Parse(value); @@ -104,7 +104,7 @@ public static T1 GetMaxValue() maxValue = ulong.MaxValue; break; default: - maxValue = default(T1);//set default value + maxValue = default(T1); // Set as default value. break; } return (T1)maxValue; From 4a560d07866d702891dbb6a11dc4a70cceae2b81 Mon Sep 17 00:00:00 2001 From: enisn Date: Sat, 3 Aug 2019 18:27:19 +0300 Subject: [PATCH 2/5] Add IFilter as a new layer in hierachy to seperate data types and main filter object types --- src/AutoFilterer/Abstractions/IFilter.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/AutoFilterer/Abstractions/IFilter.cs diff --git a/src/AutoFilterer/Abstractions/IFilter.cs b/src/AutoFilterer/Abstractions/IFilter.cs new file mode 100644 index 0000000..ca94384 --- /dev/null +++ b/src/AutoFilterer/Abstractions/IFilter.cs @@ -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 ApplyFilterTo(IQueryable query); + } +} From b5e25c78c14c6bb1f173824523b26767f4ef9285 Mon Sep 17 00:00:00 2001 From: enisn Date: Sat, 3 Aug 2019 18:27:45 +0300 Subject: [PATCH 3/5] Add an extension method as ApplyFilter to apply DbSet types easily --- src/AutoFilterer/Extensions/QueryExtensions.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/AutoFilterer/Extensions/QueryExtensions.cs b/src/AutoFilterer/Extensions/QueryExtensions.cs index bb93a8e..eb16f43 100644 --- a/src/AutoFilterer/Extensions/QueryExtensions.cs +++ b/src/AutoFilterer/Extensions/QueryExtensions.cs @@ -1,4 +1,6 @@ -using System; +using AutoFilterer.Abstractions; +using AutoFilterer.Types; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -27,5 +29,10 @@ public static IQueryable ToPaged(this IQueryable source, int page, int return (source as IOrderedQueryable).Skip((page - 1) * pageSize).Take(pageSize); } - } + + public static IQueryable ApplyFilter(this IQueryable source, IFilter filter) + { + return filter.ApplyFilterTo(source); + } + } } From 1aed0682e9f64aae0392a75ad07579d8b07d18c0 Mon Sep 17 00:00:00 2001 From: enisn Date: Sat, 3 Aug 2019 18:27:57 +0300 Subject: [PATCH 4/5] Update Sample --- .../Controllers/BlogsController.cs | 24 ++++++++++++------ .../WebApplication.API/Dtos/BlogFilterDto.cs | 2 +- .../Dtos/BlogPaginationFilterDto.cs | 25 +++++++++++++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 sandbox/WebApplication.API/Dtos/BlogPaginationFilterDto.cs diff --git a/sandbox/WebApplication.API/Controllers/BlogsController.cs b/sandbox/WebApplication.API/Controllers/BlogsController.cs index 0c5f1de..d7f38e3 100644 --- a/sandbox/WebApplication.API/Controllers/BlogsController.cs +++ b/sandbox/WebApplication.API/Controllers/BlogsController.cs @@ -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 @@ -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); } diff --git a/sandbox/WebApplication.API/Dtos/BlogFilterDto.cs b/sandbox/WebApplication.API/Dtos/BlogFilterDto.cs index 8d58646..4f57466 100644 --- a/sandbox/WebApplication.API/Dtos/BlogFilterDto.cs +++ b/sandbox/WebApplication.API/Dtos/BlogFilterDto.cs @@ -11,7 +11,7 @@ namespace WebApplication.API.Dtos { - public class BlogFilterDto : PaginationFilterBase + public class BlogFilterDto : FilterBase { public int? CategoryId { get; set; } diff --git a/sandbox/WebApplication.API/Dtos/BlogPaginationFilterDto.cs b/sandbox/WebApplication.API/Dtos/BlogPaginationFilterDto.cs new file mode 100644 index 0000000..f182aec --- /dev/null +++ b/sandbox/WebApplication.API/Dtos/BlogPaginationFilterDto.cs @@ -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 Priority { get; set; } + + [StringFilterOptions(StringFilterOption.Contains)] + public string Title { get; set; } + + public bool? IsPublished { get; set; } + + public Range PublishDate { get; set; } + } +} From 96540c35a7602c3840a7d279ad015e660174c4b9 Mon Sep 17 00:00:00 2001 From: enisn Date: Sat, 3 Aug 2019 18:30:43 +0300 Subject: [PATCH 5/5] [Docs] Update Usage Samples --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 487f21f..e274c89 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,8 @@ public class BlogFilterDto : FilterBase - 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] @@ -70,7 +72,7 @@ public class BlogsController : ControllerBase { using(var db = new MyDbContext()) { - var blogList = filter.ApplyFilterTo(db.Blogs).ToList(); + var blogList = db.Blogs.ApplyFilter(filter); return Ok(blogList); } } @@ -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); } }