func generatePagingLinks(r *http.Request, query search.Query, total uint32) []models.BundleLinkComponent { links := make([]models.BundleLinkComponent, 0, 5) values := query.NormalizedQueryValues(false) options := query.Options() count := uint32(options.Count) offset := uint32(options.Offset) // First create the base URL for paging baseURL := responseURL(r, query.Resource) // Self link links = append(links, newLink("self", baseURL, values, count, offset)) // First link links = append(links, newLink("first", baseURL, values, count, uint32(0))) // Previous link if offset > uint32(0) { newOffset := offset - count // Handle case where paging is uneven (e.g., count=10&offset=5) if count > offset { newOffset = uint32(0) } links = append(links, newLink("previous", baseURL, values, offset-newOffset, newOffset)) } // Next Link if total > (offset + count) { links = append(links, newLink("next", baseURL, values, count, offset+count)) } // Last Link remainder := (total - offset) % count if total < offset { remainder = uint32(0) } newOffset := total - remainder if remainder == uint32(0) && total > count { newOffset = total - count } links = append(links, newLink("last", baseURL, values, count, newOffset)) return links }