Skip to content

Commit

Permalink
Merge pull request #6 from jeregrine/fix_paging
Browse files Browse the repository at this point in the history
Making paging optional
  • Loading branch information
Jason S. committed Jun 22, 2015
2 parents 4ca7882 + 8084801 commit 517915f
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions lib/jsonapi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,27 @@ defmodule JSONAPI do
defp handle_paging(doc, mod, params, endpoint) do
links = %{
self: mod.url_func().(endpoint, :index, params),
next_page: nil,
previous_page: nil
}

number = get_in(params, [:page, :number])
page_number = get_in(params, [:page, :number])
page_size = get_in(params, [:page, :size])
resources = Map.get(doc, :data, [])

if Enum.count(resources) == page_size do
next_page = mod.url_func().(endpoint, :index, put_in(params, [:page, :number], number+1))
links = Dict.put(links, :next_page, next_page)
else
end
if page_number && page_size do

if Enum.count(resources) == page_size do
next_page = mod.url_func().(endpoint, :index, put_in(params, [:page, :number], page_number+1))
links = Dict.put(links, :next_page, next_page)
end

if page_number > 0 do
previous_page = mod.url_func().(endpoint, :index, put_in(params, [:page, :number], page_number-1))
links = Dict.put(links, :previous_page, previous_page)
end

if number > 0 do
previous_page = mod.url_func().(endpoint, :index, put_in(params, [:page, :number], number-1))
links = Dict.put(links, :previous_page, previous_page)
links = Map.get(doc, :links, %{}) |> Map.merge(links)
end

links = Map.get(doc, :links, %{}) |> Map.merge(links)
Map.put(doc, :links, links)
end

Expand Down

0 comments on commit 517915f

Please sign in to comment.