Skip to content

Commit fcd33b8

Browse files
committed
better sorting api
1 parent 65e99cc commit fcd33b8

File tree

5 files changed

+42
-10
lines changed

5 files changed

+42
-10
lines changed

BugLab.Business/Extensions/IQueryableExtensions.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using BugLab.Data;
2-
using BugLab.Data.Entities;
1+
using BugLab.Shared.Enums;
32
using Microsoft.EntityFrameworkCore;
3+
using System;
44
using System.Linq;
5+
using System.Linq.Expressions;
56
using System.Threading;
67
using System.Threading.Tasks;
78

@@ -27,5 +28,17 @@ public static class IQueryableExtensions
2728

2829
return (query, totalItems);
2930
}
31+
32+
public static IOrderedQueryable<T> SortBy<T, TKey>(this IQueryable<T> source,
33+
Expression<Func<T, TKey>> sortOn, SortDirection sortDirection = SortDirection.Asc)
34+
{
35+
return sortDirection == SortDirection.Asc ? source.OrderBy(sortOn) : source.OrderByDescending(sortOn);
36+
}
37+
38+
public static IOrderedQueryable<T> ThenSortBy<T, TKey>(this IOrderedQueryable<T> source,
39+
Expression<Func<T, TKey>> sortOn, SortDirection sortDirection = SortDirection.Asc)
40+
{
41+
return sortDirection == SortDirection.Asc ? source.ThenBy(sortOn) : source.ThenByDescending(sortOn);
42+
}
3043
}
3144
}

BugLab.Business/QueryHandlers/Bugs/GetBugsHandler.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using BugLab.Business.Helpers;
1+
using BugLab.Business.Extensions;
2+
using BugLab.Business.Helpers;
23
using BugLab.Data;
4+
using BugLab.Shared.Enums;
35
using BugLab.Shared.Responses;
46
using Mapster;
57
using MediatR;
@@ -29,10 +31,12 @@ public async Task<PagedList<BugResponse>> Handle(GetBugsQuery request, Cancellat
2931

3032
if (!string.IsNullOrWhiteSpace(request.Title)) query = query.Where(b => b.Title.Contains(request.Title));
3133

32-
query = request.OrderBy switch
34+
var defaultOrder = query.OrderBy(b => b.Status);
35+
36+
query = request.SortBy switch
3337
{
34-
"title" => query.OrderBy(b => b.Status).ThenBy(b => b.Title),
35-
_ => query.OrderBy(b => b.Status).ThenBy(b => b.Priority)
38+
BugSortBy.Title => defaultOrder.ThenSortBy(b => b.Title, request.Sort),
39+
_ => defaultOrder.ThenSortBy(b => b.Priority, request.Sort)
3640
};
3741

3842
return await PagedList<BugResponse>.CreateAsync(query.ProjectToType<BugResponse>(),

BugLab.Shared/Enums/BugEnums.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ public enum BugStatus
1414
InProgress,
1515
Resolved
1616
}
17+
18+
public enum BugSortBy
19+
{
20+
Priority,
21+
Title
22+
}
1723
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace BugLab.Shared.Enums
2+
{
3+
public enum SortDirection
4+
{
5+
Asc,
6+
Desc
7+
}
8+
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
namespace BugLab.Shared.QueryParams
1+
using BugLab.Shared.Enums;
2+
3+
namespace BugLab.Shared.QueryParams
24
{
35
public class BugParams : PaginationParams
46
{
5-
private string _orderBy = "priority";
6-
77
public int? ProjectId { get; set; }
8-
public string OrderBy { get => _orderBy; set => _orderBy = value.ToLower(); }
8+
public BugSortBy SortBy { get; set; }
9+
public SortDirection Sort { get; set; }
910
public string Title { get; set; }
1011
}
1112
}

0 commit comments

Comments
 (0)