/// <summary> /// Gets paged and sorted entities from database and mapps them to ViewModels. /// </summary> /// <param name="input">Object with paging and sorting</param> /// <param name="includings">Expressions with properties to include</param> /// <returns>Paged and sorted output of ViewModels</returns> public async Task <PagedOutput <TViewModel> > GetAll(PagedAndSortedInput input, params Expression <Func <TEntity, object> >[] includings) { var queryable = _repository.Queryable(); var result = await queryable.SortAndPageBy(input).IncludeMultiple(includings).ToListAsync(); var count = await queryable.CountAsync(); return(new PagedOutput <TViewModel>(_mapper.Map <List <TViewModel> >(result), count)); }
/// <summary> /// Uses CRUD service for getting paged and sorted items. /// </summary> /// <param name="input">Paging and sorting</param> /// <returns>Response object with status description and items as result if success</returns> public async Task <IActionResult> GetAll(PagedAndSortedInput input) { input.ValidateAndSetDefaults(); PagedOutput <PeopleMarkViewModel> model = null; try { model = await _crudService.GetAll(input, p => p.Mark); } catch (Exception ex) { return(StatusCode(500, ApiResult.FailResult(ex.Message))); } return(Json(ApiResult.SuccessResult(model))); }
/// <summary> /// Sorts and pages input set of entities on query level. /// Sorting implemented using Linq.Dynamic package. /// </summary> /// <typeparam name="TEntity">Entity type</typeparam> /// <param name="entities">Set of entities to be sorted and paged</param> /// <param name="input">Object with sorting and paging</param> /// <returns>Query with sorting and paging</returns> public static IQueryable <TEntity> SortAndPageBy <TEntity>(this IQueryable <TEntity> entities, PagedAndSortedInput input) { return(entities .OrderBy(input.Sorting) .Skip(input.Skip) .Take(input.Take)); }