Commit 123693d2 authored by Frank Rousseau's avatar Frank Rousseau
Browse files

Allow to filter tracker entries with a start and end date

parent d50c11d9
......@@ -16,6 +16,13 @@ defmodule Gaja.TrackerEntries do
Repo.all(Ecto.assoc(tracker, :tracker_entries))
end
def list_tracker_entries(%Tracker{id: tracker_id}, start_date, end_date) do
TrackerEntry
|> where([t], t.tracker_id == ^tracker_id)
|> where([t], t.date >= ^start_date and t.date <= ^end_date)
|> Repo.all()
end
def get_tracker_entry(%Tracker{id: tracker_id}, id) do
TrackerEntry
|> where([t], t.id == ^id and t.tracker_id == ^tracker_id)
......
......@@ -31,6 +31,32 @@ defmodule Gaja.Web.TrackerEntryControllerTest do
assert json_response(conn, 200)
end
@tag login: "bill@example.com"
test "between given dates", %{conn: conn, tracker: tracker} do
attrs = %{ value: 2, date: "2019-06-01T00:00:00" }
TrackerEntries.create_tracker_entry(tracker, attrs)
attrs = %{ value: 5, date: "2019-07-01T00:00:00" }
{:ok, tracker_entry} = TrackerEntries.create_tracker_entry(
tracker,
attrs
)
response =
conn
|> get(
Routes.tracker_tracker_entry_path(conn, :index, tracker),
start_date: "2019-06-15T00:00:00",
end_date: "2019-07-15T00:00:00"
)
|> json_response(200)
expected = %{"data" => [%{
"date" => "2019-07-01T00:00:00",
"uuid" => tracker_entry.id,
"value" => 5.0,
"value_string" => nil
}]}
assert response == expected
end
test "401 for unauthorized user", %{conn: conn, tracker: tracker} do
conn = get(conn, Routes.tracker_tracker_entry_path(conn, :index, tracker))
assert json_response(conn, 401)
......
......@@ -9,6 +9,15 @@ defmodule Gaja.TrackerEntryController do
:index, :show, :create, :update, :delete
]
def index(
%Plug.Conn{assigns: %{current_user: user}} = conn,
%{"tracker_id" => tracker_id, "start_date" => start_date, "end_date" => end_date}
) do
tracker = Trackers.get_tracker(user, tracker_id)
tracker_entries = TrackerEntries.list_tracker_entries(tracker, start_date, end_date)
render(conn, "index.json", tracker_entries: tracker_entries)
end
def index(
%Plug.Conn{assigns: %{current_user: user}} = conn,
%{"tracker_id" => tracker_id}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment