From 482a73e9da84776ee1451e97f3e8b0aca636f99a Mon Sep 17 00:00:00 2001 From: Mebareksaf Date: Wed, 28 Feb 2024 19:29:20 +0100 Subject: [PATCH] adjust query params for cursor based pagination and test --- .../controllers/api/recording_controller.ex | 5 ++-- .../ex_nvr_web/live/recordings_list_live.ex | 6 +++-- .../api/recording_controller_test.exs | 9 +++---- .../live/recordings_list_live_test.exs | 24 +++++-------------- 4 files changed, 15 insertions(+), 29 deletions(-) diff --git a/apps/ex_nvr_web/lib/ex_nvr_web/controllers/api/recording_controller.ex b/apps/ex_nvr_web/lib/ex_nvr_web/controllers/api/recording_controller.ex index 60803281..957f8c5b 100644 --- a/apps/ex_nvr_web/lib/ex_nvr_web/controllers/api/recording_controller.ex +++ b/apps/ex_nvr_web/lib/ex_nvr_web/controllers/api/recording_controller.ex @@ -28,10 +28,9 @@ defmodule ExNVRWeb.API.RecordingController do {:ok, {recordings, meta}} -> meta = Map.take(meta, [ - :current_page, + :start_cursor, :page_size, - :total_count, - :total_pages + :end_cursor ]) recordings = Enum.map(recordings, &Map.drop(&1, [:device_name, :timezone])) diff --git a/apps/ex_nvr_web/lib/ex_nvr_web/live/recordings_list_live.ex b/apps/ex_nvr_web/lib/ex_nvr_web/live/recordings_list_live.ex index ac874912..571e6a86 100644 --- a/apps/ex_nvr_web/lib/ex_nvr_web/live/recordings_list_live.ex +++ b/apps/ex_nvr_web/lib/ex_nvr_web/live/recordings_list_live.ex @@ -144,15 +144,17 @@ defmodule ExNVRWeb.RecordingListLive do @impl true def handle_event("paginate", pagination_params, socket) do after_cursor = Map.get(pagination_params, "after-cursor", nil) + first = Map.get(pagination_params, "first", 100) before_cursor = Map.get(pagination_params, "before-cursor", nil) + last = Map.get(pagination_params, "last", 100) cursor_params = if not is_nil(after_cursor) do old_pagination_params = Map.drop(socket.assigns.pagination_params, ["before", "before-cursor", "last"]) - Map.merge(old_pagination_params, %{"first" => 100, "after" => after_cursor}) + Map.merge(old_pagination_params, %{"first" => first, "after" => after_cursor}) else old_pagination_params = Map.drop(socket.assigns.pagination_params, ["after", "after-cursor", "first"]) - Map.merge(old_pagination_params, %{"last" => 100, "before" => before_cursor}) + Map.merge(old_pagination_params, %{"last" => last, "before" => before_cursor}) end pagination_params = Map.merge(pagination_params, cursor_params) diff --git a/apps/ex_nvr_web/test/ex_nvr_web/controllers/api/recording_controller_test.exs b/apps/ex_nvr_web/test/ex_nvr_web/controllers/api/recording_controller_test.exs index 4eba39a8..74ec863e 100644 --- a/apps/ex_nvr_web/test/ex_nvr_web/controllers/api/recording_controller_test.exs +++ b/apps/ex_nvr_web/test/ex_nvr_web/controllers/api/recording_controller_test.exs @@ -88,12 +88,9 @@ defmodule ExNVRWeb.API.RecordingControllerTest do assert Enum.map(response["data"], & &1["id"]) |> MapSet.new() == Enum.map(recordings, & &1.id) |> MapSet.new() - assert %{ - "total_count" => 4, - "total_pages" => 1, - "current_page" => 1, - "page_size" => 100 - } = response["meta"] + assert Map.get(response["meta"], "page_size") == 100 + assert Map.has_key?(response["meta"], "start_cursor") + assert Map.has_key?(response["meta"], "end_cursor") end test "filter recordings chunks", %{conn: conn, device: device, recordings: recordings} do diff --git a/apps/ex_nvr_web/test/ex_nvr_web/live/recordings_list_live_test.exs b/apps/ex_nvr_web/test/ex_nvr_web/live/recordings_list_live_test.exs index 8c1924ed..0cb1376a 100644 --- a/apps/ex_nvr_web/test/ex_nvr_web/live/recordings_list_live_test.exs +++ b/apps/ex_nvr_web/test/ex_nvr_web/live/recordings_list_live_test.exs @@ -63,7 +63,7 @@ defmodule ExNVRWeb.RecordingListLiveTest do {:ok, lv, html} = conn |> log_in_user(user_fixture()) - |> live(~p"/recordings?page_size=1") + |> live(~p"/recordings?first=1") # check pagination assert lv @@ -74,14 +74,6 @@ defmodule ExNVRWeb.RecordingListLiveTest do |> element("a", "Next") |> has_element?() - assert lv - |> element("a", "1") - |> has_element?() - - for page_label <- ["1", "2", "...", "4", "5", "6", "...", "9", "10"] do - assert html =~ page_label - end - assert_recording_info(lv, html, device, List.last(recordings)) end @@ -91,14 +83,10 @@ defmodule ExNVRWeb.RecordingListLiveTest do |> log_in_user(user_fixture()) |> live(~p"/recordings?page_size=1") - html = element(lv, "a", "2") |> render_click(%{page_size: 1}) - - for page_label <- ["1", "2", "...", "4", "5", "6", "...", "9", "10"] do - assert html =~ page_label - end + html = element(lv, "a", "Next") |> render_click(%{last: 1}) # check recording - recording = Enum.at(recordings, length(recordings) - 2) + recording = Enum.at(recordings, 0) assert_recording_info(lv, html, device, recording) end @@ -240,7 +228,7 @@ defmodule ExNVRWeb.RecordingListLiveTest do {:ok, lv, html} = logged_in_conn |> live( - ~p"/recordings?page_size=1&order_by[]=device_name&order_by[]=start_date&order_by[]=end_date&order_directions[]=asc&order_directions[]=desc" + ~p"/recordings?first=1&order_by[]=device_name&order_by[]=start_date&order_by[]=end_date&order_directions[]=asc&order_directions[]=desc" ) assert_recording_info(lv, html, device, List.last(recordings)) @@ -249,7 +237,7 @@ defmodule ExNVRWeb.RecordingListLiveTest do {:ok, lv, html} = logged_in_conn |> live( - ~p"/recordings?page_size=1&order_by[]=start_date&order_by[]=device_name&order_directions[]=asc&order_directions[]=asc" + ~p"/recordings?first=1&order_by[]=start_date&order_by[]=device_name&order_directions[]=asc&order_directions[]=asc" ) assert_recording_info(lv, html, new_device, List.first(new_recordings)) @@ -258,7 +246,7 @@ defmodule ExNVRWeb.RecordingListLiveTest do {:ok, lv, html} = logged_in_conn |> live( - ~p"/recordings?page_size=1&order_by[]=end_date&order_by[]=device_name&order_directions[]=desc&order_directions[]=desc" + ~p"/recordings?first=1&order_by[]=end_date&order_by[]=device_name&order_directions[]=desc&order_directions[]=desc" ) assert_recording_info(lv, html, new_device, List.last(new_recordings))