Skip to content
This repository was archived by the owner on Jun 7, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions GraphLabs.Site.Models/Groups/GroupModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class GroupModel : AbstractFilterableModel<Group>, IEntityBasedModel<Grou
[StringFilter("Номер группы")]
public string Name { get; set; }

[StringFilter("Возможность вступить в группу")]
[StringFilter("Доступность для регистрации")]
public bool IsOpen { get; set; }

public ICollection<Student> Students { get; set; }
Expand All @@ -25,10 +25,10 @@ public class GroupModel : AbstractFilterableModel<Group>, IEntityBasedModel<Grou

public static Expression<Func<Group, bool>> CreateFilter(FilterParams filterParams)
{
string Name = filterParams.GetStringParam("Name");
string name = filterParams.GetStringParam("Name");
bool? isOpen = filterParams.GetBoolParam("IsOpen");

return g => (Name == null || Name.Equals(g.Name))
return g => (name == null || name.ToLower().Equals(g.Name.ToLower()))
&& (isOpen == null || g.IsOpen == isOpen);
}
}
Expand Down
20 changes: 16 additions & 4 deletions GraphLabs.Site/Controllers/GraphLabsFilteringController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Reflection;
using System.Web.Mvc;
using GraphLabs.DomainModel.Infrastructure;
using GraphLabs.Site.Core.Filters;
Expand All @@ -11,14 +13,24 @@ public abstract class GraphLabsFilteringController<TModel, TEntity> : GraphLabsC
where TEntity : AbstractEntity
where TModel : AbstractFilterableModel<TEntity>, IEntityBasedModel<TEntity>
{
protected Expression<Func<TEntity, bool>> _fiExpression;
private static readonly ConcurrentDictionary<Type, MethodInfo> _methodInfos = new ConcurrentDictionary<Type, MethodInfo>();

protected Expression<Func<TEntity, bool>> FiExpression { get; private set; }

public abstract ActionResult Index(string message);

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.ActionDescriptor.ActionName.Equals("Index"))//может быть это ограничение лишнее
if (filterContext.ActionDescriptor.ActionName.Equals(nameof(Index)))//может быть это ограничение лишнее
{
FilterParams _filterParams = new FilterParams(Request.QueryString);
_fiExpression = (Expression<Func<TEntity, bool>>) typeof(TModel).GetMethod("CreateFilter").Invoke(null, new[] {_filterParams});
FilterParams filterParams = new FilterParams(Request.QueryString);
Type type = typeof(TModel);
if (!_methodInfos.ContainsKey(type))
{
_methodInfos.TryAdd(type, type.GetMethod(nameof(AbstractFilterableModel<Object>.CreateFilter)));
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо использовать GetOrAdd


FiExpression = (Expression<Func<TEntity, bool>>) _methodInfos[type].Invoke(null, new[] {filterParams});
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions GraphLabs.Site/Controllers/GroupController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public GroupController(
_modelLoader = modelLoader;
}

public ActionResult Index(string message)
public override ActionResult Index(string message)
{
ViewBag.Message = message;
var model = _listModelLoader.LoadListModel<GroupListModel, GroupModel>().Filter(_fiExpression);
var model = _listModelLoader.LoadListModel<GroupListModel, GroupModel>().Filter(FiExpression);
return View((GroupListModel) model);
}

Expand Down