コード例 #1
0
 public PagingViewModel Get([FromQuery] PlayerFilterModel model)
 {
     return(new PagingViewModel
     {
         Data = _playerService.GetPlayersByFilterModel(model, out int pageCount),
         PageCount = pageCount
     });
コード例 #2
0
ファイル: PlayerService.cs プロジェクト: narinfinity/S7App
        public IEnumerable <Player> GetPlayersByFilterModel(PlayerFilterModel model, out int pageCount)
        {
            Func <IQueryable <Player>, IOrderedQueryable <Player> > orderBy(OrderBy ob)
            {
                if (ob == null)
                {
                    return(null);
                }

                if (ob.Prop == "name" && ob.Asc == true)
                {
                    return(o => o.OrderBy(e => e.Name));
                }
                else if (ob.Prop == "name" && ob.Asc == false)
                {
                    return(o => o.OrderByDescending(e => e.Name));
                }

                else if (ob.Prop == "isActive" && ob.Asc == true)
                {
                    return(o => o.OrderBy(e => e.IsActive));
                }
                else if (ob.Prop == "isActive" && ob.Asc == false)
                {
                    return(o => o.OrderByDescending(e => e.IsActive));
                }

                else if (ob.Prop == "age" && ob.Asc == true)
                {
                    return(o => o.OrderBy(e => e.Age));
                }
                else if (ob.Prop == "age" && ob.Asc == false)
                {
                    return(o => o.OrderByDescending(e => e.Age));
                }

                else if (ob.Prop == "yellowCards" && ob.Asc == true)
                {
                    return(o => o.OrderBy(e => e.YellowCards));
                }
                else if (ob.Prop == "yellowCards" && ob.Asc == false)
                {
                    return(o => o.OrderByDescending(e => e.YellowCards));
                }

                else if (ob.Prop == "redCards" && ob.Asc == true)
                {
                    return(o => o.OrderBy(e => e.RedCards));
                }
                else if (ob.Prop == "redCards" && ob.Asc == false)
                {
                    return(o => o.OrderByDescending(e => e.RedCards));
                }

                else if (ob.Prop == "goals" && ob.Asc == true)
                {
                    return(o => o.OrderBy(e => e.Goals));
                }
                else if (ob.Prop == "goals" && ob.Asc == false)
                {
                    return(o => o.OrderByDescending(e => e.Goals));
                }

                else if (ob.Prop == "appearances" && ob.Asc == true)
                {
                    return(o => o.OrderBy(e => e.Appearances));
                }
                else if (ob.Prop == "appearances" && ob.Asc == false)
                {
                    return(o => o.OrderByDescending(e => e.Appearances));
                }

                else if (ob.Prop == "gender" && ob.Asc == true)
                {
                    return(o => o.OrderBy(e => e.Gender));
                }
                else if (ob.Prop == "gender" && ob.Asc == false)
                {
                    return(o => o.OrderByDescending(e => e.Gender));
                }

                else if (ob.Prop == "team" && ob.Asc == true)
                {
                    return(o => o.OrderBy(e => e.Team.Name));
                }
                else if (ob.Prop == "team" && ob.Asc == false)
                {
                    return(o => o.OrderByDescending(e => e.Team.Name));
                }

                else
                {
                    return(null);
                }
            }

            Expression <Func <Player, bool> > filter(string keyword)
            {
                if (model.Keyword == null)
                {
                    return(null);
                }

                return(e => e.Name.StartsWith(model.Keyword, StringComparison.InvariantCultureIgnoreCase) ||
                       e.Team.Name.StartsWith(model.Keyword, StringComparison.InvariantCultureIgnoreCase));
            };
            var includeUsefulProps = new List <Expression <Func <Player, object> > > {
                e => e.Team
            };

            pageCount = _unitOfWork.Repository <Player, int>().GetList(filter(model.Keyword), tracking: false).Count() / model.pageSize;
            return(_unitOfWork.Repository <Player, int>().GetList(
                       filter(model.Keyword),
                       orderBy(model.OrderBy),
                       includeUsefulProps,
                       page: model.Page, pageSize: model.pageSize, tracking: false));
        }