func generatePagingLinks(baseURL url.URL, query search.Query, total uint32) []models.BundleLinkComponent { links := make([]models.BundleLinkComponent, 0, 5) params := query.URLQueryParameters(true) offset := 0 if pOffset := params.Get(search.OffsetParam); pOffset != "" { offset, _ = strconv.Atoi(pOffset) if offset < 0 { offset = 0 } } count := search.NewQueryOptions().Count if pCount := params.Get(search.CountParam); pCount != "" { count, _ = strconv.Atoi(pCount) if count < 1 { count = search.NewQueryOptions().Count } } // Self link links = append(links, newLink("self", baseURL, params, offset, count)) // First link links = append(links, newLink("first", baseURL, params, 0, count)) // Previous link if offset > 0 { prevOffset := offset - count // Handle case where paging is uneven (e.g., count=10&offset=5) if prevOffset < 0 { prevOffset = 0 } prevCount := offset - prevOffset links = append(links, newLink("previous", baseURL, params, prevOffset, prevCount)) } // Next Link if total > uint32(offset+count) { nextOffset := offset + count links = append(links, newLink("next", baseURL, params, nextOffset, count)) } // Last Link remainder := (int(total) - offset) % count if int(total) < offset { remainder = 0 } newOffset := int(total) - remainder if remainder == 0 && int(total) > count { newOffset = int(total) - count } links = append(links, newLink("last", baseURL, params, newOffset, count)) return links }