/// <summary> /// The helper method which returns the HAL build factory that can build the HAL with the <see cref="IPagedResult"/> /// object. /// </summary> /// <param name="context">The <see cref="HalBuildContext"/> object that carries the information for creating /// the HAL builder.</param> /// <returns>The <see cref="IBuilder"/> object which can build a HAL data structure.</returns> protected IBuilder GetPagedResultHalBuildFactory(HalBuildContext context) { var state = (IPagedResult)context.State; var pageSize = state.PageSize; var pageNumber = state.PageNumber; var totalRecords = state.TotalRecords; var totalPages = state.TotalPages; var selfLinkItem = context.HttpContext.Request.GetEncodedUrl(); string prevLinkItem = null; if (pageNumber > 1 && pageNumber <= totalPages) { prevLinkItem = GenerateLink(context.HttpContext.Request, new Dictionary <string, StringValues> { { "page", (pageNumber - 1).ToString() } }); } string nextLinkItem = null; if (pageNumber < totalPages) { nextLinkItem = GenerateLink(context.HttpContext.Request, new Dictionary <string, StringValues> { { "page", (pageNumber + 1).ToString() } }); } var linkItemBuilder = new ResourceBuilder() .WithState(new { pageNumber, pageSize, totalRecords, totalPages }) .AddSelfLink().WithLinkItem(selfLinkItem); if (!string.IsNullOrEmpty(prevLinkItem)) { linkItemBuilder = linkItemBuilder.AddLink("prev").WithLinkItem(prevLinkItem); } if (!string.IsNullOrEmpty(nextLinkItem)) { linkItemBuilder = linkItemBuilder.AddLink("next").WithLinkItem(nextLinkItem); } var embeddedResourceName = context.ControllerAction.ControllerName.ToLower(); var attribute = context.ControllerAction.MethodInfo.CustomAttributes.FirstOrDefault(c => c.AttributeType == typeof(HalEmbeddedResourceAttribute)); if (attribute != null) { embeddedResourceName = (string)attribute.ConstructorArguments.FirstOrDefault(x => x.ArgumentType == typeof(string)).Value; } var resourceBuilder = linkItemBuilder.AddEmbedded(embeddedResourceName) .Resource(new ResourceBuilder().WithState(context.State)); return(resourceBuilder); }
/// <summary> /// Registers the hal builder factory for the default HTTP GET method, which returns a collection /// of the aggregates with pagination enabled and a search criteria applied. /// </summary> protected virtual void RegisterHalBuilderFactoryForGetAll() { this.RegisterHalBuilderFactory("*.Get(int, int)", context => { var state = (IPagedResult)context.State; var pageSize = state.PageSize; var pageNumber = state.PageNumber; var totalRecords = state.TotalRecords; var totalPages = state.TotalPages; var selfLinkItem = context.HttpContext.Request.GetEncodedUrl(); string prevLinkItem = null; if (pageNumber > 1 && pageNumber <= totalPages) { prevLinkItem = GenerateLink(context.HttpContext.Request, new Dictionary <string, StringValues> { { "page", (pageNumber - 1).ToString() } }); } string nextLinkItem = null; if (pageNumber < totalPages) { nextLinkItem = GenerateLink(context.HttpContext.Request, new Dictionary <string, StringValues> { { "page", (pageNumber + 1).ToString() } }); } var linkItemBuilder = new ResourceBuilder() .WithState(new { pageNumber, pageSize, totalRecords, totalPages }) .AddSelfLink().WithLinkItem(selfLinkItem); if (!string.IsNullOrEmpty(prevLinkItem)) { linkItemBuilder = linkItemBuilder.AddLink("prev").WithLinkItem(prevLinkItem); } if (!string.IsNullOrEmpty(nextLinkItem)) { linkItemBuilder = linkItemBuilder.AddLink("next").WithLinkItem(nextLinkItem); } var resourceBuilder = linkItemBuilder.AddEmbedded(context.ControllerAction.ControllerName.ToLower()) .Resource(new ResourceBuilder().WithState(context.State)); return(resourceBuilder); } ); }
/// <summary> /// The helper method which returns the HAL build factory that can build the HAL with the <see cref="IPagedResult"/> /// object. /// </summary> /// <param name="context">The <see cref="HalBuildContext"/> object that carries the information for creating /// the HAL builder.</param> /// <returns>The <see cref="IBuilder"/> object which can build a HAL data structure.</returns> protected IBuilder GetPagedResultHalBuildFactory(HalBuildContext context) { if (!(context.State is IPagedResult)) { throw new HalBuildConfigurationException("The state returned from the controller action cannot be converted into IPagedResult instance."); } var state = (IPagedResult)context.State; var pageSize = state.PageSize; var pageNumber = state.PageNumber <= 0 ? 1 : state.PageNumber; var totalRecords = state.TotalRecords; var totalPages = state.TotalPages; var selfLinkItem = context.HttpContext.Request.GetEncodedUrl(); var pageNumberParameterName = InferPageNumberParameterName(context.ControllerAction); var linkItemBuilder = new ResourceBuilder() .WithState(new { pageNumber, pageSize, totalRecords, totalPages }) .AddSelfLink().WithLinkItem(selfLinkItem); if (!string.IsNullOrEmpty(pageNumberParameterName)) { var firstLinkItem = GenerateLink(context.HttpContext.Request, new Dictionary <string, StringValues> { { pageNumberParameterName, 1.ToString() } }); linkItemBuilder = linkItemBuilder.AddLink("first").WithLinkItem(firstLinkItem); var lastLinkItem = GenerateLink(context.HttpContext.Request, new Dictionary <string, StringValues> { { pageNumberParameterName, totalPages.ToString() } }); linkItemBuilder = linkItemBuilder.AddLink("last").WithLinkItem(lastLinkItem); string prevLinkItem = null; if (pageNumber > 1 && pageNumber <= totalPages) { prevLinkItem = GenerateLink(context.HttpContext.Request, new Dictionary <string, StringValues> { { pageNumberParameterName, (pageNumber - 1).ToString() } }); linkItemBuilder = linkItemBuilder.AddLink("prev").WithLinkItem(prevLinkItem); } string nextLinkItem = null; if (pageNumber >= 1 && pageNumber < totalPages) { nextLinkItem = GenerateLink(context.HttpContext.Request, new Dictionary <string, StringValues> { { pageNumberParameterName, (pageNumber + 1).ToString() } }); linkItemBuilder = linkItemBuilder.AddLink("next").WithLinkItem(nextLinkItem); } } var embeddedResourceName = context.ControllerAction.ControllerName.ToLower(); var attribute = context.ControllerAction.MethodInfo.CustomAttributes.FirstOrDefault(c => c.AttributeType == typeof(HalEmbeddedResourceAttribute)); if (attribute != null) { embeddedResourceName = (string)attribute.ConstructorArguments.FirstOrDefault(x => x.ArgumentType == typeof(string)).Value; } var resourceBuilder = linkItemBuilder.AddEmbedded(embeddedResourceName) .Resource(new ResourceBuilder().WithState(context.State)); return(resourceBuilder); }