diff --git a/backend/lib/peach/events.ex b/backend/lib/peach/events.ex index a1e8a6b..1437ec6 100644 --- a/backend/lib/peach/events.ex +++ b/backend/lib/peach/events.ex @@ -6,6 +6,9 @@ defmodule Peach.Events do alias Peach.Repo import Ecto.Query + @default_limit 50 + @default_event_id 0 + @doc """ Creates an event with the given attributes. """ @@ -18,13 +21,73 @@ defmodule Peach.Events do @doc """ Returns the `first` events that end after `after_time` and their id is after `after_event_id` """ - def get_events(after_datetime, after_event_id, first) do - Repo.all( - from e in Event, - where: e.end >= ^after_datetime and e.id > ^after_event_id, - order_by: [asc: e.start, asc: e.id], - limit: ^first - ) + def get_events(%{after_datetime: after_datetime, after_event_id: after_event_id, first: first}) + when is_integer(after_event_id) and + is_integer(first) do + case NaiveDateTime.from_iso8601(after_datetime) do + {:ok, datetime} -> + {:ok, + Repo.all( + from e in Event, + where: e.end >= ^datetime and e.id > ^after_event_id, + order_by: [asc: e.start, asc: e.id], + limit: ^first + )} + + {:error, error} -> + {:error, %{after_datetime: error}} + end + end + + def get_events(%{after_datetime: after_datetime, after_event_id: after_event_id}) + when is_integer(after_event_id) do + {:ok, + Repo.all( + from e in Event, + where: e.end >= ^after_datetime and e.id > ^after_event_id, + order_by: [asc: e.start, asc: e.id], + limit: @default_limit + )} + end + + def get_events(%{after_datetime: after_datetime, limit: limit}) + when is_integer(limit) do + {:ok, + Repo.all( + from e in Event, + where: e.end >= ^after_datetime and e.id > @default_event_id, + order_by: [asc: e.start, asc: e.id], + limit: ^limit + )} + end + + def get_events(%{after_datetime: after_datetime}) do + case NaiveDateTime.from_iso8601(after_datetime) do + {:ok, datetime} -> + {:ok, + Repo.all( + from e in Event, + where: e.end >= ^datetime and e.id > @default_event_id, + order_by: [asc: e.start, asc: e.id], + limit: @default_limit + )} + + {:error, error} -> + {:error, %{after_datetime: error}} + end + end + + def get_events(%{after_datetime: _after_datetime, limit: _limit}) do + {:error, %{limit: "incorrect_type"}} + end + + def get_events(%{after_datetime: _after_datetime, after_event_id: _after_event_id}) do + {:error, %{after_event_id: "incorrect_type"}} + end + + def get_events(params) do + IO.inspect(params) + {:error, %{after_datetime: "Can't be blank"}} end @doc """ diff --git a/backend/lib/peach_web/controllers/event_controller.ex b/backend/lib/peach_web/controllers/event_controller.ex index 4775705..92ac05e 100644 --- a/backend/lib/peach_web/controllers/event_controller.ex +++ b/backend/lib/peach_web/controllers/event_controller.ex @@ -22,19 +22,11 @@ defmodule PeachWeb.EventController do end def events(conn, params) do - with {:ok, after_datetime} <- validate_datetime(Map.get(params, "after_datetime")), - {:ok, after_event_id} <- - validate_integer(Map.get(params, "after_event_id", 0), "after_event_id"), - {:ok, first} <- validate_integer(Map.get(params, "first", 50), "first") do - # Fetch events and map them to desired structure - events = - Events.get_events(after_datetime, after_event_id, first) - |> Enum.map(&format_event/1) - - conn - |> put_status(:ok) - |> json(%{events: events}) - else + # Fetch events and map them to desired structure + case Events.get_events(params) do + {:ok, events} -> + Enum.map(events, &format_event/1) + {:error, error} -> conn |> put_status(:unprocessable_entity) @@ -42,24 +34,6 @@ defmodule PeachWeb.EventController do end end - defp validate_datetime(nil), do: {:error, %{after_datetime: "Can't be blank"}} - - defp validate_datetime(datetime_str) do - case NaiveDateTime.from_iso8601(datetime_str) do - {:ok, datetime} -> {:ok, datetime} - {:error, reason} -> {:error, %{after_datetime: reason}} - end - end - - defp validate_integer(value, _field) when is_integer(value), do: {:ok, value} - - defp validate_integer(value, field) do - case Integer.parse(value) do - {int, ""} -> {:ok, int} - _ -> {:error, %{field => "invalid_type"}} - end - end - defp format_event(event), do: %{ "id" => event.id,