Skip to content

Commit 10ff9fb

Browse files
committed
Moved sort getting into GriddlyResult base class.
1 parent 2a5b2af commit 10ff9fb

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

Griddly.Mvc/GriddlyResult.cs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,38 @@
55
using System.Linq;
66
using System.Linq.Expressions;
77
using System.Reflection;
8+
using System.Web;
89
using System.Web.Helpers;
910
using System.Web.Mvc;
1011

1112
namespace Griddly.Mvc
1213
{
1314
public abstract class GriddlyResult : ActionResult
1415
{
16+
public SortField[] GetSortFields(NameValueCollection items)
17+
{
18+
return items.AllKeys
19+
.Where(x => x.StartsWith("sortFields["))
20+
.Select(x =>
21+
{
22+
int pos = x.IndexOf(']', "sortFields[".Length);
1523

24+
return new
25+
{
26+
Index = int.Parse(x.Substring("sortFields[".Length, pos - "sortFields[".Length)),
27+
Field = x.Substring(pos + 2, x.Length - pos - 2 - 1),
28+
Direction = (SortDirection)Enum.Parse(typeof(SortDirection), items[x])
29+
};
30+
31+
})
32+
.OrderBy(x => x.Index)
33+
.Select(x => new SortField()
34+
{
35+
Field = x.Field,
36+
Direction = x.Direction
37+
})
38+
.ToArray();
39+
}
1640
}
1741

1842
public class GriddlyResult<T> : GriddlyResult
@@ -41,34 +65,16 @@ public override void ExecuteResult(ControllerContext context)
4165

4266
if (!int.TryParse(items["pageNumber"], out pageNumber))
4367
pageNumber = 0;
68+
4469
if (!int.TryParse(items["pageSize"], out pageSize))
4570
pageSize = 20;
71+
4672
if (Enum.TryParse(items["exportFormat"], true, out exportFormatValue))
4773
exportFormat = exportFormatValue;
4874
else
4975
exportFormat = null;
5076

51-
sortFields = items.AllKeys
52-
.Where(x => x.StartsWith("sortFields["))
53-
.Select(x =>
54-
{
55-
int pos = x.IndexOf(']', "sortFields[".Length);
56-
57-
return new
58-
{
59-
Index = int.Parse(x.Substring("sortFields[".Length, pos - "sortFields[".Length)),
60-
Field = x.Substring(pos + 2, x.Length - pos - 2 - 1),
61-
Direction = (SortDirection)Enum.Parse(typeof(SortDirection), items[x])
62-
};
63-
64-
})
65-
.OrderBy(x => x.Index)
66-
.Select(x => new SortField()
67-
{
68-
Field = x.Field,
69-
Direction = x.Direction
70-
})
71-
.ToArray();
77+
sortFields = GetSortFields(items);
7278

7379
GriddlySettings settings = null;
7480

0 commit comments

Comments
 (0)