public virtual async Task <ActionResult <List <TDto> > > GetAllPaged() { var cts = TaskHelper.CreateChildCancellationTokenSource(ClientDisconnectedToken()); var dataTask = Service.GetAllAsync(cts.Token, null, null, null, true); var totalTask = Service.GetCountAsync(cts.Token); await TaskHelper.WhenAllOrException(cts, dataTask, totalTask); var data = dataTask.Result; var total = totalTask.Result; var paginationMetadata = new PagingInfoDto { Page = 1, PageSize = total, Records = total, PreviousPageLink = "", NextPageLink = "" }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); return(Success(data.ToList())); }
private async Task <ActionResult> List(WebApiSearchQueryParamsDto <TDto> resourceParameters) { if (string.IsNullOrEmpty(resourceParameters.OrderBy)) { resourceParameters.OrderBy = "Id"; } if (!UIHelper.ValidOrderByFor <TDto>(resourceParameters.OrderBy)) { return(BadRequest(Messages.OrderByInvalid)); } if (!UIHelper.ValidFieldsFor <TDto>(resourceParameters.Fields)) { return(BadRequest(Messages.FieldsInvalid)); } if (!UIHelper.ValidFilterFor <TDto>(HttpContext.Request.Query)) { return(BadRequest(Messages.FiltersInvalid)); } var cts = TaskHelper.CreateChildCancellationTokenSource(ClientDisconnectedToken()); var dataTask = Service.SearchAsync(cts.Token, resourceParameters.UserId, resourceParameters.Search, UIHelper.GetFilter <TDto>(HttpContext.Request.Query), resourceParameters.OrderBy, resourceParameters.Page, resourceParameters.PageSize, false); await TaskHelper.WhenAllOrException(cts, dataTask); var data = dataTask.Result; var paginationMetadata = new PagingInfoDto { Page = resourceParameters.Page, PageSize = resourceParameters.PageSize, Records = data.TotalCount, PreviousPageLink = null, NextPageLink = null }; if (paginationMetadata.HasPrevious) { paginationMetadata.PreviousPageLink = CreateResourceUri(resourceParameters, ResourceUriType.PreviousPage); } if (paginationMetadata.HasNext) { paginationMetadata.NextPageLink = CreateResourceUri(resourceParameters, ResourceUriType.NextPage); } Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata).Replace(Environment.NewLine, "")); var links = CreateLinksForCollections(resourceParameters, paginationMetadata.HasNext, paginationMetadata.HasPrevious); var shapedData = UIHelper.ShapeListData(data.ToList(), resourceParameters.Fields); var shapedDataWithLinks = shapedData.Select(dto => { var dtoAsDictionary = dto as IDictionary <string, object>; var dtoLinks = CreateLinks( dtoAsDictionary["Id"].ToString(), resourceParameters.Fields); dtoAsDictionary.Add("links", dtoLinks); return(dtoAsDictionary); }); var linkedCollectionResource = new { value = shapedDataWithLinks, links = links }; return(Ok(linkedCollectionResource)); }
public virtual async Task <IActionResult> GetByIdChildCollection(string id, string collection, WebApiSearchQueryParamsDto resourceParameters) { if (string.IsNullOrEmpty(resourceParameters.OrderBy)) { resourceParameters.OrderBy = "Id"; } //order by if (!UIHelper.ValidOrderByFor <TDto>(resourceParameters.OrderBy)) { return(BadRequest(Messages.OrderByInvalid)); } if (!RelationshipHelper.IsValidCollectionExpression(collection, typeof(TDto))) { return(BadRequest(Messages.CollectionInvalid)); } if (RelationshipHelper.IsCollectionExpressionCollectionItem(collection)) { return(await GetCollectionItem(id, collection, resourceParameters.Fields)); } var collectionItemType = RelationshipHelper.GetCollectionExpressionType(collection, typeof(TDto)); //fields if (!UIHelper.ValidFieldsFor(collectionItemType, resourceParameters.Fields)) { return(BadRequest(Messages.FieldsInvalid)); } //filter if (!UIHelper.ValidFilterFor(collectionItemType, HttpContext.Request.Query)) { return(BadRequest(Messages.FiltersInvalid)); } var filter = UIHelper.GetFilter(collectionItemType, HttpContext.Request.Query); var cts = TaskHelper.CreateChildCancellationTokenSource(ClientDisconnectedToken()); var result = await Service.GetByIdWithPagedCollectionPropertyAsync(cts.Token, id, collection, resourceParameters.Search, filter, resourceParameters.OrderBy, resourceParameters.Page, resourceParameters.PageSize); IEnumerable <Object> list = ((IEnumerable <Object>)RelationshipHelper.GetCollectionExpressionData(collection, typeof(TDto), result.Dto)); var paginationMetadata = new PagingInfoDto { Page = resourceParameters.Page, PageSize = resourceParameters.PageSize, Records = result.TotalCount, PreviousPageLink = null, NextPageLink = null }; if (paginationMetadata.HasPrevious) { paginationMetadata.PreviousPageLink = CreateCollectionPropertyResourceUri(collection, resourceParameters, ResourceUriType.PreviousPage); } if (paginationMetadata.HasNext) { paginationMetadata.NextPageLink = CreateCollectionPropertyResourceUri(collection, resourceParameters, ResourceUriType.NextPage); } Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata).Replace(Environment.NewLine, "")); var links = CreateLinksForCollectionProperty(collection, resourceParameters, paginationMetadata.HasNext, paginationMetadata.HasPrevious); var shapedData = UIHelper.ShapeListData(list, collectionItemType, resourceParameters.Fields); var shapedDataWithLinks = shapedData.Select(collectionPropertyDtoItem => { var collectionPropertyDtoItemAsDictionary = collectionPropertyDtoItem as IDictionary <string, object>; var collectionPropertyDtoItemLinks = CreateLinksForCollectionItem(id, collection + "/" + collectionPropertyDtoItemAsDictionary["Id"].ToString(), resourceParameters.Fields); collectionPropertyDtoItemAsDictionary.Add("links", collectionPropertyDtoItem); return(collectionPropertyDtoItemAsDictionary); }).ToList(); var linkedCollectionResource = new WebApiListResponseDto <IDictionary <string, object> > { Value = shapedDataWithLinks , Links = links }; return(Ok(linkedCollectionResource)); }
private async Task <ActionResult> List(WebApiPagedSearchOrderingRequestDto resourceParameters) { if (string.IsNullOrEmpty(resourceParameters.OrderBy)) { resourceParameters.OrderBy = "Id"; } if (!TypeHelperService.TypeHasProperties <TDto>(resourceParameters.Fields)) { return(ApiErrorMessage(Messages.FieldsInvalid)); } var cts = TaskHelper.CreateChildCancellationTokenSource(ClientDisconnectedToken()); var dataTask = Service.SearchAsync(cts.Token, resourceParameters.UserId, resourceParameters.Search, null, AutoMapperHelper.GetOrderBy <TDto>(resourceParameters.OrderBy, resourceParameters.OrderType), resourceParameters.Page.HasValue ? resourceParameters.Page - 1 : null, resourceParameters.PageSize, true); var totalTask = Service.GetSearchCountAsync(cts.Token, resourceParameters.UserId, resourceParameters.Search, null); await TaskHelper.WhenAllOrException(cts, dataTask, totalTask); var data = dataTask.Result; var total = totalTask.Result; var paginationMetadata = new PagingInfoDto { Page = resourceParameters.Page.HasValue ? resourceParameters.Page.Value : 1, PageSize = resourceParameters.PageSize.HasValue ? resourceParameters.PageSize.Value : data.Count(), Records = total, PreviousPageLink = null, NextPageLink = null }; if (paginationMetadata.HasPrevious) { paginationMetadata.PreviousPageLink = CreateResourceUri(resourceParameters, ResourceUriType.PreviousPage); } if (paginationMetadata.HasNext) { paginationMetadata.NextPageLink = CreateResourceUri(resourceParameters, ResourceUriType.NextPage); } Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); var links = CreateLinksForCollections(resourceParameters, paginationMetadata.HasNext, paginationMetadata.HasPrevious); var shapedData = IEnumerableExtensions.ShapeData(data.ToList(), resourceParameters.Fields); var shapedDataWithLinks = shapedData.Select(dto => { var dtoAsDictionary = dto as IDictionary <string, object>; var dtoLinks = CreateLinks( dtoAsDictionary["Id"].ToString(), resourceParameters.Fields); dtoAsDictionary.Add("links", dtoLinks); return(dtoAsDictionary); }); var linkedCollectionResource = new { value = shapedDataWithLinks, links = links }; return(Ok(linkedCollectionResource)); }
public virtual async Task <IActionResult> GetByIdChildCollection(string id, string collection, WebApiPagedSearchOrderingRequestDto resourceParameters) { if (string.IsNullOrEmpty(resourceParameters.OrderBy)) { resourceParameters.OrderBy = "Id"; } if (!RelationshipHelper.IsValidCollectionExpression(collection, typeof(TDto))) { return(ApiErrorMessage(Messages.CollectionInvalid)); } if (RelationshipHelper.IsCollectionExpressionCollectionItem(collection)) { return(await GetCollectionItem(id, collection, resourceParameters.Fields)); } var collectionItemType = RelationshipHelper.GetCollectionExpressionType(collection, typeof(TDto)); if (!TypeHelperService.TypeHasProperties(collectionItemType, resourceParameters.Fields)) { return(ApiErrorMessage(Messages.FieldsInvalid)); } var cts = TaskHelper.CreateChildCancellationTokenSource(ClientDisconnectedToken()); var dataTask = Service.GetByIdWithPagedCollectionPropertyAsync(cts.Token, id, collection, resourceParameters.Search, resourceParameters.OrderBy, resourceParameters.OrderType == "asc" ? true : false, resourceParameters.Page.HasValue ? resourceParameters.Page - 1 : null, resourceParameters.PageSize); var totalTask = Service.GetByIdWithPagedCollectionPropertyCountAsync(cts.Token, id, collection, resourceParameters.Search); await TaskHelper.WhenAllOrException(cts, dataTask, totalTask); var result = dataTask.Result; var total = totalTask.Result; IEnumerable <Object> list = ((IEnumerable <Object>)RelationshipHelper.GetCollectionExpressionData(collection, typeof(TDto), result)); var paginationMetadata = new PagingInfoDto { Page = resourceParameters.Page.HasValue ? resourceParameters.Page.Value : 1, PageSize = resourceParameters.PageSize.HasValue ? resourceParameters.PageSize.Value : list.Count(), Records = total, PreviousPageLink = null, NextPageLink = null }; if (paginationMetadata.HasPrevious) { paginationMetadata.PreviousPageLink = CreateCollectionPropertyResourceUri(collection, resourceParameters, ResourceUriType.PreviousPage); } if (paginationMetadata.HasNext) { paginationMetadata.NextPageLink = CreateCollectionPropertyResourceUri(collection, resourceParameters, ResourceUriType.NextPage); } Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); var links = CreateLinksForCollectionProperty(collection, resourceParameters, paginationMetadata.HasNext, paginationMetadata.HasPrevious); var shapedData = IEnumerableExtensions.ShapeData(list, collectionItemType, resourceParameters.Fields); var shapedDataWithLinks = shapedData.Select(collectionPropertyDtoItem => { var collectionPropertyDtoItemAsDictionary = collectionPropertyDtoItem as IDictionary <string, object>; var collectionPropertyDtoItemLinks = CreateLinksForCollectionItem(id, collection + "/" + collectionPropertyDtoItemAsDictionary["Id"].ToString(), resourceParameters.Fields); collectionPropertyDtoItemAsDictionary.Add("links", collectionPropertyDtoItem); return(collectionPropertyDtoItemAsDictionary); }); var linkedCollectionResource = new WebApiListResponseDto <IDictionary <string, object> > { Value = shapedDataWithLinks , Links = links }; return(Ok(linkedCollectionResource)); }