Skip to content

Commit

Permalink
Merge pull request #30 from KainosSoftwareLtd/kainos-develop
Browse files Browse the repository at this point in the history
merge kainos-master with kainos-develop
  • Loading branch information
karsiwek-kainos authored Nov 14, 2016
2 parents c3b97fe + 39a4d52 commit 4911f54
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
10 changes: 3 additions & 7 deletions src/fhir/search_helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ exports.search_links = (query, expr, total)->
base_url = base_url + "&_page=0"

requested_count = expr.count
requested_page = expr.page
requested_page = expr.page ? 0

res.push({relation: 'self', url: base_url})

if requested_count && requested_count < total
if requested_count && requested_count * (requested_page + 1) < total
next_url = base_url.replace /_page=\d+/, -> "_page=#{requested_page + 1}"
res.push({relation: 'next', url: next_url})

Expand All @@ -27,11 +27,7 @@ exports.search_links = (query, expr, total)->
res.push({relation: 'previous', url: next_url})

if requested_count
last_page = if requested_count < total
Math.floor(total / requested_count)
else
requested_page

last_page = Math.max(0, Math.ceil(total / requested_count) - 1)
last_url = base_url.replace /_page=\d+/, -> "_page=#{last_page}"
res.push({relation: 'last', url: last_url})

Expand Down
73 changes: 71 additions & 2 deletions test/fhir/search/pagination_search.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ queries:
- path: ['link', 2, 'url']
result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=0&_count=1"
- path: ['link', 3, 'url']
result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=3&_count=1"
result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=2&_count=1"

- query: {resourceType: 'Encounter', queryString: 'patient=Patient/nicola&_sort=patient&_page=1&_count=1&_totalMethod=exact'}
total: 3
Expand All @@ -76,10 +76,79 @@ queries:
- path: ['link', 2, 'url']
result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=0&_count=1&_totalMethod=exact"
- path: ['link', 3, 'url']
result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=3&_count=1&_totalMethod=exact"
result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=2&_count=1&_totalMethod=exact"

- query: {resourceType: 'Encounter', queryString: 'patient=Patient/nicola&_sort=patient&_page=1&_count=1&_totalMethod=no'}
total: _undefined
probes:
- path: ['link']
result: '_undefined'

- query: {resourceType: 'Encounter', queryString: 'patient=Patient/nicola&_sort=patient&_count=3'}
total: 3
probes:
- path: ['entry', 'length']
result: 3
- path: ['link', 0, 'url']
result: "/Encounter?patient=Patient/nicola&_sort=patient&_count=3&_page=0"
- path: ['link', 1, 'url']
result: "/Encounter?patient=Patient/nicola&_sort=patient&_count=3&_page=0"
- path: ['link', 2, 'url']

- query: {resourceType: 'Encounter', queryString: 'patient=Patient/nicola&_sort=patient&_page=0&_count=3'}
total: 3
probes:
- path: ['entry', 'length']
result: 3
- path: ['link', 0, 'url']
result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=0&_count=3"
- path: ['link', 1, 'url']
result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=0&_count=3"
- path: ['link', 2, 'url']

- query: {resourceType: 'Encounter', queryString: 'patient=Patient/nicola&_sort=patient&_page=2&_count=3'}
total: 3
probes:
- path: ['entry', 'length']
result: 0
- path: ['link', 0, 'url']
result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=2&_count=3"
- path: ['link', 1, 'url']
result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=1&_count=3"
- path: ['link', 2, 'url']
result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=0&_count=3"
- path: ['link', 3, 'url']

- query: {resourceType: 'Encounter', queryString: 'patient=Patient/nicola&_sort=patient&_page=1&_count=2'}
total: 3
probes:
- path: ['entry', 'length']
result: 1
- path: ['link', 0, 'relation']
result: "self"
- path: ['link', 0, 'url']
result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=1&_count=2"
- path: ['link', 1, 'relation']
result: "previous"
- path: ['link', 1, 'url']
result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=0&_count=2"
- path: ['link', 2, 'relation']
result: "last"
- path: ['link', 2, 'url']
result: "/Encounter?patient=Patient/nicola&_sort=patient&_page=1&_count=2"
- path: ['link', 3, 'url']

- query: {resourceType: 'Encounter', queryString: 'patient=Patient/nonexistent&_sort=patient&_page=0&_count=3'}
total: 0
probes:
- path: ['entry', 'length']
result: 0
- path: ['link', 0, 'relation']
result: "self"
- path: ['link', 0, 'url']
result: "/Encounter?patient=Patient/nonexistent&_sort=patient&_page=0&_count=3"
- path: ['link', 1, 'relation']
result: "last"
- path: ['link', 1, 'url']
result: "/Encounter?patient=Patient/nonexistent&_sort=patient&_page=0&_count=3"
- path: ['link', 2, 'url']

0 comments on commit 4911f54

Please sign in to comment.