     query =
       from i in Item,
       join: f in Feed, on: i.feed_id == f.id,
-      where: f.folder_id == ^folder_id,
-      select: i
-    if params["limit"] do query = from i in query, limit: ^params["limit"] end
-    items = Repo.all(query)
+      where: f.folder_id == ^folder_id
+    items = Repo.all(query |> paginate(params) |> filter(params))
     render(conn, "index.json", items: items)
   end
 

    query =
      from i in Item,
      where: i.feed_id == ^feed_id
    if params["limit"] do query = from i in query, limit: ^params["limit"] end
    items = Repo.all(query)
    render(conn, "index.json", items: items)
  end

  def index(conn, params) do
    query = from f in Item
    if params["filter"] do
      query = Enum.reduce(params["filter"], query, fn {col, val}, query ->
        from i in query, where: field(i, ^String.to_atom(col)) == ^val
      end)
    end
    if params["limit"] do query = from i in query, limit: ^params["limit"] end

    items = Repo.all(query)
    render(conn, "index.json", items: items)
  end



    send_resp(conn, :no_content, "")
  end

  defp paginate(query, params) do
    if params["limit"] do query = from i in query, limit: ^params["limit"] end
    if params["offset"] do query = from i in query, offset: ^params["offset"] end
    query
  end

  defp filter(query, params) do
    case params["filter"] do
      nil -> query
      filters ->
        Enum.reduce(filters, query, fn {col, val}, query ->
          from i in query, where: field(i, ^String.to_atom(col)) == ^val
        end)
    end
  end

  defp count(query) do
    from i in query, select: count(i.id)
  end
end
