Skip to content

Commit

Permalink
fuck phoenix parses everything as strings even integers fuck fuck fuck
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Oct 15, 2024
1 parent fbe7195 commit 34c0472
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 38 deletions.
77 changes: 70 additions & 7 deletions backend/lib/peach/events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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 """
Expand Down
36 changes: 5 additions & 31 deletions backend/lib/peach_web/controllers/event_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,18 @@ 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)
|> json(%{errors: error})
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,
Expand Down

0 comments on commit 34c0472

Please sign in to comment.