.NET CORE MVC-基于用户数据的动态筛选器?

时间:2019-06-09 23:00:04

标签: c# .net asp.net-mvc entity-framework asp.net-core

我想在.net核心MVC应用程序的后端创建一个过滤器,该过滤器基于ANOTHER表的动态用户数据来过滤来自一个表的数据...

将其置于上下文中...筛选器相当于亚马逊具有一个按钮,可隐藏您先前购买的所有产品...

现在,我知道我有可能做类似的事情:(回到上面的上下文示例) 1.加载用户购买的所有物品。 2.根据任何其他过滤器设置商店商品查询... 3.在初始化对页面项目的查询之前,将购买的项目的ID与项目查询中的ID进行比较,并删除所有与之匹配的项目...

但是,唯一的问题是,为了使应用程序尽可能快,我基于所有用户和组中固定的其他过滤器值来缓存(或想要添加)不同的“商店商品查询”的分页值...所以一次说50个项目...

这是我的一些代码:(当前没有过滤,但会添加..)

        public async Task<IEnumerable<IcoSocialRewardDetailViewModel>> GetIcoRewards(IcoRewardSortFilterPageOptions options)
        {
            var rewards = _dbContext.IcoSocialRewards
                .AsNoTracking()
                .MapIcoSocialRewardToView()
                .OrderRewardsBy(options.OrderByOptions);

            options.SetupPaging(rewards);

            return await rewards.Page(options.PageNum - 1,
                                   options.PageSize).ToListAsync();
        }

public static class IcoSocialRewardListSelect
    {
        public static IQueryable<IcoSocialRewardDetailViewModel>
    MapIcoSocialRewardToView(this IQueryable<IcoSocialReward> rewards)
        {
            return rewards.Select(r => new IcoSocialRewardDetailViewModel
            {
                Id = r.Id,
                CurParticipants = r.CurParticipants,
                MaxParticipants = r.MaxParticipants,
                MaxSubmissions = r.MaxSubmissions,
                CustomTitle = r.CustomTitle,
                Description = r.Description,
                ParamName = r.ParamName,
                ReqUserParam = r.ReqUserParam,
                IcoInfoId = r.IcoInfoId,
                RewardParam = r.RewardParam,
                Tokens = r.Tokens,
                SocialRewardId = r.SocialRewardId,
                Blockchain = r.IcoInfo.TokenPlatform,
                ProjectDescription = r.IcoInfo.Description,
                ProjectLogo = r.IcoInfo.LogoUrl,
                IsFeatured = false,
                ProjectName = r.IcoInfo.Name,
                TokenTicker = r.IcoInfo.TokenTicker,
                SocialPlatform = r.SocialReward.Platform,
                IsActive = r.IcoInfo.IsActive && r.IcoInfo.IsLive,
                EndDate = r.IcoInfo.EndDate
            });
        }
    }

    public static class IcoSocialRewardListSort
    {
        public static IQueryable<IcoSocialRewardDetailViewModel> OrderRewardsBy
    (this IQueryable<IcoSocialRewardDetailViewModel> rewards,
     IcoRewardOrderByOptions orderByOptions)
        {
            switch (orderByOptions)
            {
                case IcoRewardOrderByOptions.SimpleOrder:
                    return rewards.OrderByDescending(
                        x => x.Id);
                case IcoRewardOrderByOptions.TokensAwardedAsc:
                    return rewards.OrderBy(x =>
                        x.Tokens);
                case IcoRewardOrderByOptions.TokensAwardedDec:
                    return rewards.OrderByDescending(
                        x => x.Tokens);
                case IcoRewardOrderByOptions.ByEndDate:
                    return rewards.OrderByDescending(
                        x => x.EndDate);
                case IcoRewardOrderByOptions.ByProject:
                    return rewards.OrderByDescending(
                        x => x.IcoInfoId);
                case IcoRewardOrderByOptions.BySocialPlatform:
                    return rewards.OrderBy(
                        x => x.SocialPlatform);
                case IcoRewardOrderByOptions.ByTokenTicker:
                    return rewards.OrderBy(
                        x => x.TokenTicker);
                default:
                    throw new ArgumentOutOfRangeException(
                        nameof(orderByOptions), orderByOptions, null);
            }
        }
    }

0 个答案:

没有答案