Skip to content

Commit 923b3ec

Browse files
committed
Enhance cookie stuff
1 parent b6522e3 commit 923b3ec

11 files changed

+367
-141
lines changed

Build/CommonAssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
//
1616
// You can specify all the values or you can default the Revision and Build Numbers
1717
// by using the '*' as shown below:
18-
[assembly: AssemblyVersion("2.6.0")]
19-
[assembly: AssemblyFileVersion("2.6.0")]
18+
[assembly: AssemblyVersion("2.7.0")]
19+
[assembly: AssemblyFileVersion("2.7.0")]
2020
//[assembly: AssemblyInformationalVersion("2.5-filters")]

Griddly.Mvc/GriddlyContext.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ namespace Griddly.Mvc
99
public class GriddlyContext
1010
{
1111
public string Name { get; set; }
12-
public string ParentPath { get; set; }
1312
public bool IsDefaultSkipped { get; set; }
1413

1514
public GriddlyFilterCookieData CookieData { get; set; }
@@ -18,10 +17,16 @@ public class GriddlyContext
1817

1918
public Dictionary<string, object> Defaults { get; set; } = new Dictionary<string, object>();
2019
public Dictionary<string, object> Parameters { get; set; } = new Dictionary<string, object>();
20+
21+
public int PageNumber { get; set; }
22+
public int PageSize { get; set; }
23+
public GriddlyExportFormat? ExportFormat { get; set; }
24+
public SortField[] SortFields { get; set; }
2125
}
2226

2327
public class GriddlyFilterCookieData
2428
{
2529
public Dictionary<string, string[]> Values { get; set; }
30+
public SortField[] SortFields { get; set; }
2631
}
2732
}

Griddly.Mvc/GriddlyCookieFilterValueProvider.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,26 @@ namespace Griddly.Mvc
1111
{
1212
public class GriddlyCookieFilterValueProvider : IValueProvider
1313
{
14-
GriddlyFilterCookieData _data;
14+
GriddlyContext _context;
1515

16-
public GriddlyCookieFilterValueProvider(GriddlyFilterCookieData data)
16+
public GriddlyCookieFilterValueProvider(GriddlyContext context)
1717
{
18-
_data = data;
18+
_context = context;
19+
20+
if (_context.CookieData?.SortFields?.Length > 0)
21+
_context.SortFields = _context.CookieData?.SortFields;
1922
}
2023

2124
public bool ContainsPrefix(string prefix)
2225
{
23-
return _data.Values?.ContainsKey(prefix) == true;
26+
return _context.CookieData.Values?.ContainsKey(prefix) == true;
2427
}
2528

2629
public ValueProviderResult GetValue(string key)
2730
{
2831
string[] value = null;
2932

30-
if (_data.Values?.TryGetValue(key, out value) != true)
33+
if (_context.CookieData.Values?.TryGetValue(key, out value) != true)
3134
value = null;
3235

3336
string attemptedValue = null;
@@ -57,7 +60,7 @@ public override IValueProvider GetValueProvider(ControllerContext controllerCont
5760
context.CookieData = data;
5861
context.IsDefaultSkipped = true;
5962

60-
return new GriddlyCookieFilterValueProvider(data);
63+
return new GriddlyCookieFilterValueProvider(context);
6164
}
6265
catch
6366
{

Griddly.Mvc/GriddlyExtensions.cs

Lines changed: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections;
44
using System.Collections.Generic;
5+
using System.Collections.Specialized;
56
using System.Linq;
67
using System.Reflection;
78
using System.Text;
@@ -162,55 +163,50 @@ public static IHtmlString ToHtmlAttributes(this IDictionary<string, object> dict
162163

163164
public static void SetGriddlyDefault<T>(this ControllerBase controller, ref T parameter, string field, T value)
164165
{
165-
var context = controller.ViewData[_contextKey] as GriddlyContext;
166+
var context = controller.GetOrCreateGriddlyContext();
166167

167-
if (context != null)
168-
{
169-
context.Defaults[field] = value;
168+
context.Defaults[field] = value;
170169

171-
if (controller.ControllerContext.IsChildAction
172-
&& !context.IsDefaultSkipped
173-
&& EqualityComparer<T>.Default.Equals(parameter, default(T)))
174-
{
175-
parameter = value;
170+
if (controller.ControllerContext.IsChildAction
171+
&& !context.IsDefaultSkipped
172+
&& EqualityComparer<T>.Default.Equals(parameter, default(T)))
173+
{
174+
parameter = value;
176175

177-
context.Parameters[field] = value;
178-
}
176+
context.Parameters[field] = parameter;
179177
}
180178
}
181179

182180
public static void SetGriddlyDefault<T>(this ControllerBase controller, ref T[] parameter, string field, IEnumerable<T> value)
183181
{
184-
var context = controller.ViewData[_contextKey] as GriddlyContext;
182+
var context = controller.GetOrCreateGriddlyContext();
185183

186-
if (context != null)
184+
context.Defaults[field] = value;
185+
186+
if (controller.ControllerContext.IsChildAction
187+
&& !context.IsDefaultSkipped
188+
&& parameter == null)
187189
{
188-
context.Defaults[field] = value;
190+
parameter = value.ToArray();
189191

190-
if (controller.ControllerContext.IsChildAction
191-
&& !context.IsDefaultSkipped
192-
&& parameter == null)
193-
{
194-
parameter = value.ToArray();
195-
}
192+
context.Parameters[field] = parameter;
196193
}
197194
}
198195

199196
public static void SetGriddlyDefault<T>(this ControllerBase controller, ref T?[] parameter, string field, IEnumerable<T> value)
200197
where T : struct
201198
{
202-
var context = controller.ViewData[_contextKey] as GriddlyContext;
199+
var context = controller.GetOrCreateGriddlyContext();
203200

204-
if (context != null)
201+
context.Defaults[field] = value;
202+
203+
if (controller.ControllerContext.IsChildAction
204+
&& !context.IsDefaultSkipped
205+
&& parameter == null)
205206
{
206-
context.Defaults[field] = value;
207+
parameter = value.Cast<T?>().ToArray();
207208

208-
if (controller.ControllerContext.IsChildAction
209-
&& !context.IsDefaultSkipped
210-
&& parameter == null)
211-
{
212-
parameter = value.Cast<T?>().ToArray();
213-
}
209+
context.Parameters[field] = parameter;
214210
}
215211
}
216212

@@ -262,9 +258,31 @@ public static GriddlyContext GetOrCreateGriddlyContext(this ControllerBase contr
262258

263259
if (context == null)
264260
{
261+
SortField[] sortFields = null;
262+
GriddlyExportFormat? exportFormat;
263+
264+
NameValueCollection items = new NameValueCollection(controller.ControllerContext.HttpContext.Request.Params);
265+
266+
if (!int.TryParse(items["pageNumber"], out int pageNumber))
267+
pageNumber = 0;
268+
269+
if (!int.TryParse(items["pageSize"], out int pageSize))
270+
pageSize = 20;
271+
272+
if (Enum.TryParse(items["exportFormat"], true, out GriddlyExportFormat exportFormatValue))
273+
exportFormat = exportFormatValue;
274+
else
275+
exportFormat = null;
276+
277+
sortFields = GriddlyResult.GetSortFields(items);
278+
265279
context = new GriddlyContext()
266280
{
267-
Name = (controller.GetType().Name + "_" + controller.ControllerContext.RouteData.GetRequiredString("action")).ToLower()
281+
Name = (controller.GetType().Name + "_" + controller.ControllerContext.RouteData.GetRequiredString("action")).ToLower(),
282+
PageNumber = pageNumber,
283+
PageSize = pageSize,
284+
ExportFormat = exportFormat,
285+
SortFields = sortFields
268286
};
269287

270288
// NOTE: for 2020 Chris... yes, this is unique for multiple griddlies on a page as it is in the grid action context of each one
@@ -274,6 +292,37 @@ public static GriddlyContext GetOrCreateGriddlyContext(this ControllerBase contr
274292
return context;
275293
}
276294

295+
// TODO: keep in sync with Extensions.GetFormattedValue
296+
public static string[] GetFormattedValueByType(object value)
297+
{
298+
if (value != null)
299+
{
300+
var type = value.GetType();
301+
302+
if (value is IEnumerable enumerable && type != typeof(string))
303+
return enumerable.Cast<object>().Select(x => x.ToString()).ToArray();
304+
305+
string stringValue;
306+
307+
if (type == typeof(float) ||
308+
type == typeof(double) ||
309+
type == typeof(decimal))
310+
stringValue = string.Format("{0:n2}", value);
311+
else if (type == typeof(DateTime) || type.HasCastOperator<DateTime>())
312+
stringValue = string.Format("{0:d}", value);
313+
else if (type == typeof(bool))
314+
stringValue = value.ToString().ToLower();
315+
else
316+
stringValue = value.ToString();
317+
318+
if (!string.IsNullOrWhiteSpace(stringValue))
319+
return new[] { stringValue };
320+
}
321+
322+
return null;
323+
}
324+
325+
277326
static IDictionary<string, object> ObjectToDictionary(object value)
278327
{
279328
if (value == null)

Griddly.Mvc/GriddlyParameterAttribute.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
3636
if (context.IsDefaultSkipped && !(context.CookieData?.Values?.ContainsKey(param.Key) == true || parentKeys.Contains(param.Key)))
3737
filterContext.ActionParameters[param.Key] = null;
3838
else
39+
{
40+
context.Defaults[param.Key] = param.Value;
3941
context.Parameters[param.Key] = param.Value;
42+
}
4043
}
4144
}
4245
}
@@ -55,18 +58,21 @@ public override void OnActionExecuted(ActionExecutedContext filterContext)
5558
Uri parentPath = filterContext.IsChildAction ? request.Url : request.UrlReferrer;
5659
string parentPathString = parentPath?.PathAndQuery.Split('?')[0]; // TODO: less allocations than split
5760

58-
if (parentPathString?.Length > 1)
61+
if (parentPathString?.Length > 0)
5962
{
6063
HttpCookie cookie = new HttpCookie("gf_" + context.Name)
6164
{
62-
Path = context.ParentPath
65+
Path = parentPathString
6366
};
6467

6568
GriddlyFilterCookieData data = new GriddlyFilterCookieData()
6669
{
6770
Values = new Dictionary<string, string[]>()
6871
};
6972

73+
if (context.SortFields?.Length > 0)
74+
data.SortFields = context.SortFields;
75+
7076
// now, we could use the context.Parameters... but the raw string values seems more like what we want here...
7177
foreach (var param in filterContext.ActionDescriptor.GetParameters())
7278
{
@@ -88,7 +94,7 @@ public override void OnActionExecuted(ActionExecutedContext filterContext)
8894
{
8995
if (param.Value != null)
9096
{
91-
var value = Extensions.GetFormattedValueByType(param.Value);
97+
var value = GriddlyExtensions.GetFormattedValueByType(param.Value);
9298

9399
if (value != null)
94100
data.Values[param.Key] = value;

Griddly.Mvc/GriddlyResult.cs

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Griddly.Mvc
1313
{
1414
public abstract class GriddlyResult : ActionResult
1515
{
16-
public SortField[] GetSortFields(NameValueCollection items)
16+
public static SortField[] GetSortFields(NameValueCollection items)
1717
{
1818
return items.AllKeys
1919
.Where(x => x != null && x.StartsWith("sortFields["))
@@ -67,59 +67,38 @@ public override void ExecuteResult(ControllerContext context)
6767
context.RequestContext.HttpContext.Response.Cache.SetNoStore();
6868
}
6969

70-
int pageNumber;
71-
int pageSize;
72-
SortField[] sortFields = null;
73-
GriddlyExportFormat exportFormatValue;
74-
GriddlyExportFormat? exportFormat;
75-
76-
NameValueCollection items = new NameValueCollection(context.RequestContext.HttpContext.Request.Params);
77-
78-
if (!int.TryParse(items["pageNumber"], out pageNumber))
79-
pageNumber = 0;
80-
81-
if (!int.TryParse(items["pageSize"], out pageSize))
82-
pageSize = 20;
83-
84-
if (Enum.TryParse(items["exportFormat"], true, out exportFormatValue))
85-
exportFormat = exportFormatValue;
86-
else
87-
exportFormat = null;
88-
89-
sortFields = GetSortFields(items);
90-
70+
var griddlyContext = context.Controller.GetOrCreateGriddlyContext();
9171
GriddlySettings settings = null;
9272

9373
if (context.IsChildAction)
9474
{
9575
settings = GriddlySettingsResult.GetSettings(context, ViewName);
9676

97-
if (GriddlySettings.OnGriddlyResultExecuting != null)
98-
GriddlySettings.OnGriddlyResultExecuting(settings, context);
77+
GriddlySettings.OnGriddlyResultExecuting?.Invoke(settings, context);
9978

10079
// TODO: should we always pull sort fields?
101-
if (!sortFields.Any())
102-
sortFields = settings.DefaultSort;
80+
if (griddlyContext.SortFields?.Any() != true)
81+
griddlyContext.SortFields = settings.DefaultSort;
10382

10483
if (settings.PageSize > settings.MaxPageSize)
10584
settings.PageSize = settings.MaxPageSize;
10685

10786
if (settings.PageSize != null)
108-
pageSize = settings.PageSize.Value;
87+
griddlyContext.PageSize = settings.PageSize.Value;
10988
}
11089

111-
if (exportFormat == null)
90+
if (griddlyContext.ExportFormat == null)
11291
{
113-
IList<T> page = GetPage(pageNumber, pageSize, sortFields);
92+
IList<T> page = GetPage(griddlyContext.PageNumber, griddlyContext.PageSize, griddlyContext.SortFields);
11493

11594
GriddlyResultPage<T> result = new GriddlyResultPage<T>()
11695
{
11796
Data = page,
11897
Count = page.Count,
119-
PageNumber = pageNumber,
98+
PageNumber = griddlyContext.PageNumber,
12099
Total = GetCount(),
121-
PageSize = pageSize,
122-
SortFields = sortFields,
100+
PageSize = griddlyContext.PageSize,
101+
SortFields = griddlyContext.SortFields,
123102
Settings = settings,
124103
PopulateSummaryValues = PopulateSummaryValues
125104
};
@@ -160,20 +139,23 @@ public override void ExecuteResult(ControllerContext context)
160139
fileName = fileName.Replace(c, '_');
161140
}
162141

163-
if (exportFormat == GriddlyExportFormat.Custom)
142+
NameValueCollection items = new NameValueCollection(context.HttpContext.Request.Params);
143+
144+
if (griddlyContext.ExportFormat == GriddlyExportFormat.Custom)
164145
{
165146
result = GriddlySettings.HandleCustomExport(this, items);
166147
}
167148
else
168149
{
169-
var records = GetAll(sortFields);
170-
if (exportFormat == GriddlyExportFormat.Xlsx)
150+
var records = GetAll(griddlyContext.SortFields);
151+
152+
if (griddlyContext.ExportFormat == GriddlyExportFormat.Xlsx)
171153
{
172154
result = new GriddlyExcelResult<T>(records, settings, fileName, items["exportName"]);
173155
}
174156
else // if (exportFormat == GriddlyExportFormat.Csv || exportFormat == GriddlyExportFormat.Tsv)
175157
{
176-
result = new GriddlyCsvResult<T>(records, settings, fileName, exportFormat.Value, items["exportName"]);
158+
result = new GriddlyCsvResult<T>(records, settings, fileName, griddlyContext.ExportFormat.Value, items["exportName"]);
177159
}
178160
}
179161

0 commit comments

Comments
 (0)