Copser

Copser

Scrivener loads last page when page_number > total_pages

Hey all, I’m trying to figure out why scrivener is not returning an empty list when page_number > total_pages. To handle pagination I’m doing this

  @google_news_table "google_news"

  @google_news_dattrs %{
    paginate: false,
    page: 1,
    page_size: 10
  }

  @list_google_news_dreturn %{
    items: [],
    page_number: 1,
    page_size: 10,
    paginate: false,
    total_entries: 0,
    total_pages: 0
  }

  def google_news_query(google_news_scope) do
    from n in google_news_scope,
    select: %{
      title: n.title,
      url: n.url
    }
  end

  # ==================================================
  #                 QUERY
  # ==================================================
  def list_google_news(viewer, attrs \\ []) do
    %{
      paginate: paginate,
      page: page,
      page_size: page_size,
      start_date: start_date,
      end_date: end_date
    } = Enum.into(attrs, @google_news_dattrs)

    query =
      @google_news_table
      |> google_news_query

    query = unless (start_date && start_date !== "" && end_date && end_date !== ""), do: query, else: Scopes.scope_with_date(query, start_date, end_date)

    if paginate do
      page = Repo.paginate(query, page: page, page_size: page_size)
      items = page.entries

      Map.merge(@list_google_news_dreturn, %{
        paginate: true,
        items: items,
        page_number: page.page_number,
        page_size: page.page_size,
        total_entries: page.total_entries,
        total_pages: page.total_pages
      })
    else
      items = Repo.all(query)

      Map.merge(@list_google_news_dreturn, %{
        items: items,
        total_entries: Enum.count(items)
      })
    end
  end

  def scope_with_date(query, start_date, end_date) do
    {:ok, s_date} = Date.from_iso8601(start_date)
    {:ok, e_date} = Date.from_iso8601(end_date)

    query
    |> where([t], fragment("?::date", t.publish_date) >= ^s_date and fragment("?::date", t.publish_date) <= ^e_date)
  end

Using the function in the REPL and after querying the db with

GoogleNews.list_google_news(viewer, %{start_date: "2021-05-04", end_date: "2021-05-06", paginate: true, page: 1})

I can see

%{
  items: [
    # items from the db
  ],
  page_number: 13,
  page_size: 10,
  paginate: true,
  total_entries: 128,
  total_pages: 13
}

after I query for more data, instead of empty list I can see the same data from the 13th page. Looking at the scrivener_ecto I can see that they handled this but for me, it’s not working. I’m sorry I know it’s confusing I’m trying to explain it as much as I can, so can anyone helps me understand why is this happening?

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement