/// <summary> /// 获取与给定的 <see cref="UserSortKey"/> 值对应的实体对象成员定义。 /// </summary> /// <param name="key"><see cref="UserSortKey"/> 值。</param> /// <returns>与给定的 <see cref="UserSortKey"/> 值对应的实体对象成员定义。</returns> /// <exception cref="ArgumentException">给定的 <see cref="UserSortKey"/> 值无效。</exception> public static FieldDefinition <User> GetField(this UserSortKey key) { LambdaExpression fieldSelector; switch (key) { case UserSortKey.TotalSubmissions: fieldSelector = (Expression <Func <User, int> >)(u => u.TotalSubmissions); break; case UserSortKey.TotalAcceptedSubmissions: fieldSelector = (Expression <Func <User, int> >)(u => u.TotalAcceptedSubmissions); break; case UserSortKey.TotalProblemsAttempted: fieldSelector = (Expression <Func <User, int> >)(u => u.TotalProblemsAttempted); break; case UserSortKey.TotalProblemsAccepted: fieldSelector = (Expression <Func <User, int> >)(u => u.TotalProblemsAccepted); break; case UserSortKey.JoinTime: fieldSelector = (Expression <Func <User, DateTime> >)(u => u.JoinTime); break; default: throw new ArgumentException($"Invalid UserSortKey value: {key}"); } return(new ExpressionFieldDefinition <User>(fieldSelector)); }
// GET: /users public async Task <PaginatedListActionResult <UserListInfo> > GetUsers( [FromQuery][Page] int page = 0, [FromQuery][ItemsPerPage] int itemsPerPage = 1, [FromQuery] UserSortKey by = UserSortKey.TotalProblemsAccepted, [FromQuery] bool descend = true) { var findPipeline = new UserFindPipeline(UserFilterBuilder.Empty) { SortKey = by, SortByDescending = descend, Pagination = new Pagination(page, itemsPerPage) }; var findResult = await _repo.Users.FindManyAsync(findPipeline); var models = findResult.ResultSet.Select(e => _mapper.Map <User, UserListInfo>(e)); return(findResult.TotalCount, models); }