Skip to content

Commit

Permalink
fix: Further API cleanup, better error handling in ui
Browse files Browse the repository at this point in the history
  • Loading branch information
kiosion committed Jan 18, 2024
1 parent 097133d commit 020044d
Show file tree
Hide file tree
Showing 17 changed files with 315 additions and 196 deletions.
2 changes: 1 addition & 1 deletion elixir-api/lib/app/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Hexerei.Router do
{result, upt}
end

@spec uptime_string(integer) :: tuple
@spec uptime_string(integer) :: map()
defp uptime_string(upt) do
days_in_ms = 86_400_000
hours_in_ms = 3_600_000
Expand Down
14 changes: 5 additions & 9 deletions elixir-api/lib/routes/api/v1/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ defmodule Router.Api.V1.Config do
end

get "/" do
with params <-
validate_query_params(
fetch_query_params(conn).query_params,
%{
"lang" => "en"
}
) do
with {:ok, params} <- validate_query_params(conn, %{"lang" => "en"}) do
query =
Query.new()
|> Query.filter([%{"_type" => "'siteSettings'"}])
Expand All @@ -36,9 +30,11 @@ defmodule Router.Api.V1.Config do
end
)
else
_ ->
{:error, reason} ->
conn
|> error_res(400, "Invalid request", [%{message: "Unknown error collecting params"}])
|> error_res(400, "Invalid request", [
%{message: "Invalid or malformed params", detail: reason}
])
end
end
end
25 changes: 11 additions & 14 deletions elixir-api/lib/routes/api/v1/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@ defmodule Router.Api.V1.Post do
end

get "/:id" do
with params <-
validate_query_params(
Map.merge(
fetch_query_params(conn).query_params,
%{"id" => conn.params["id"]}
),
%{
"id" => nil,
"lang" => "en",
"preview" => false
}
) do
with {:ok, params} <-
validate_query_params(conn, %{"id" => conn.params["id"]}, %{
"id" => nil,
"lang" => "en",
"preview" => false
}) do
query =
Query.new(%{:include_drafts => params["preview"]})
|> Query.filter([
Expand Down Expand Up @@ -67,8 +61,11 @@ defmodule Router.Api.V1.Post do
update_meta_and_send_response(conn, code, transformed_result, meta, duration)
end)
else
_ ->
conn |> error_res(400, "Invalid request", [%{message: "Invalid or missing parameters"}])
{:error, reason} ->
conn
|> error_res(400, "Invalid request", [
%{message: "Invalid or malformed params", detail: reason}
])
end
end
end
63 changes: 28 additions & 35 deletions elixir-api/lib/routes/api/v1/posts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ defmodule Router.Api.V1.Posts do
end

get "/" do
with params <-
fetch_query_params(conn).query_params
|> validate_query_params(%{
with {:ok, params} <-
validate_query_params(conn, %{
"limit" => 10,
"skip" => 0,
"s" => "date",
Expand All @@ -28,36 +27,18 @@ defmodule Router.Api.V1.Posts do
true <- params["limit"] >= 0 and params["skip"] >= 0 do
query =
Query.new()
|> Query.filter(%{
"_type" => "'post'"
})

query =
case params["date"] do
nil ->
query

_ ->
query
|> Query.filter(%{
"date" => "'#{params["date"]}'"
})
end

query =
case params["tags"] do
nil ->
query

_ ->
query
|> Query.filter(%{
"tags[]->slug.current" => ["match", "'#{params["tags"]}'"]
})
end

query =
query
|> Query.filter(%{"_type" => "'post'"})
|> (fn q ->
if params["date"] == nil,
do: q,
else: q |> Query.filter(%{"date" => ["<=", "'#{params["date"]}'"]})
end).()
|> (fn q ->
if params["tags"] == nil,
do: q,
else:
q |> Query.filter(%{"tags[]->slug.current" => ["match", "'#{params["tags"]}'"]})
end).()
|> Query.project([
"_id",
"_rev",
Expand Down Expand Up @@ -110,8 +91,20 @@ defmodule Router.Api.V1.Posts do
update_meta_and_send_response(conn, code, transformed_result, meta, duration)
end)
else
_ ->
conn |> error_res(400, "Invalid request", [%{message: "Invalid or missing parameters"}])
false ->
conn
|> error_res(400, "Invalid request", [
%{
message: "Invalid or malformed params",
detail: "Limit and skip must be positive integers"
}
])

{:error, reason} ->
conn
|> error_res(400, "Invalid request", [
%{message: "Invalid or malformed params", detail: reason}
])
end
end
end
25 changes: 11 additions & 14 deletions elixir-api/lib/routes/api/v1/project.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@ defmodule Router.Api.V1.Project do
end

get "/:id" do
with params <-
validate_query_params(
Map.merge(
fetch_query_params(conn).query_params,
%{"id" => conn.params["id"]}
),
%{
"id" => nil,
"lang" => "en",
"preview" => false
}
) do
with {:ok, params} <-
validate_query_params(conn, %{"id" => conn.params["id"]}, %{
"id" => nil,
"lang" => "en",
"preview" => false
}) do
query =
Query.new(%{:include_drafts => params["preview"]})
|> Query.filter([
Expand Down Expand Up @@ -66,8 +60,11 @@ defmodule Router.Api.V1.Project do
update_meta_and_send_response(conn, code, transformed_result, meta, duration)
end)
else
false ->
conn |> error_res(400, "Invalid request", [%{message: "Invalid or missing parameters"}])
{:error, reason} ->
conn
|> error_res(400, "Invalid request", [
%{message: "Invalid or malformed params", detail: reason}
])
end
end
end
56 changes: 24 additions & 32 deletions elixir-api/lib/routes/api/v1/projects.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ defmodule Router.Api.V1.Projects do
end

get "/" do
with params <-
fetch_query_params(conn).query_params
|> validate_query_params(%{
with {:ok, params} <-
validate_query_params(conn, %{
"limit" => 10,
"skip" => 0,
"s" => "date",
Expand All @@ -31,33 +30,17 @@ defmodule Router.Api.V1.Projects do
|> Query.filter(%{
"_type" => "'project'"
})

query =
case params["date"] do
nil ->
query

_ ->
query
|> Query.filter(%{
"date" => ["<=", "'#{params["date"]}'"]
})
end

query =
case params["tags"] do
nil ->
query

_ ->
query
|> Query.filter(%{
"tags[]->slug.current" => ["match", "'#{params["tags"]}'"]
})
end

query =
query
|> (fn q ->
if params["date"] == nil,
do: q,
else: q |> Query.filter(%{"date" => ["<=", "'#{params["date"]}'"]})
end).()
|> (fn q ->
if params["tags"] == nil,
do: q,
else:
q |> Query.filter(%{"tags[]->slug.current" => ["match", "'#{params["tags"]}'"]})
end).()
|> Query.project([
"_id",
"_type",
Expand Down Expand Up @@ -113,8 +96,17 @@ defmodule Router.Api.V1.Projects do
update_meta_and_send_response(conn, code, transformed_result, meta, duration)
end)
else
_ ->
conn |> error_res(400, "Invalid request", [%{message: "Invalid or missing parameters"}])
false ->
conn
|> error_res(400, "Invalid request", [
%{message: "Limit and skip must be positive integers"}
])

{:error, reason} ->
conn
|> error_res(400, "Invalid request", [
%{message: "Invalid or malformed params", detail: reason}
])
end
end
end
13 changes: 10 additions & 3 deletions elixir-api/lib/routes/api/v1/tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ defmodule Router.Api.V1.Tag do
get "/:id" do
with true <- is_binary(id),
true <- String.trim(id) != "",
params <-
fetch_query_params(conn).query_params |> validate_query_params(%{"type" => nil}),
{:ok, params} <-
validate_query_params(conn, %{"type" => nil}),
true <- params["type"] in ["post", "project"] do
query =
Query.new()
Expand Down Expand Up @@ -84,7 +84,14 @@ defmodule Router.Api.V1.Tag do
update_meta_and_send_response(conn, code, transformed_result, meta, duration)
end)
else
_ -> conn |> error_res(400, "Invalid request", [%{message: "Missing ID or invalid type"}])
false ->
conn |> error_res(400, "Invalid request", [%{message: "Missing ID or invalid type"}])

{:error, reason} ->
conn
|> error_res(400, "Invalid request", [
%{message: "Invalid or malformed params", detail: reason}
])
end
end
end
59 changes: 32 additions & 27 deletions elixir-api/lib/routes/api/v1/tags.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ defmodule Router.Api.V1.Tags do
end

get "/" do
with params <-
fetch_query_params(conn).query_params
|> validate_query_params(%{
with {:ok, params} <-
validate_query_params(conn, %{
"type" => nil,
"limit" => 10,
"skip" => 0,
Expand All @@ -34,29 +33,26 @@ defmodule Router.Api.V1.Tags do
"title",
"slug"
])

query =
case params["type"] do
nil ->
query

_ ->
query
|> Query.project([
[
"'referencedBy'",
Query.new()
|> Query.filter([
%{"_type" => "'#{params["type"]}'"},
%{"references" => "^._id", :nest => true}
])
|> Query.project([
"_id"
])
|> Query.build!()
]
])
end
|> (fn q ->
if params["type"] == nil,
do: q,
else:
q
|> Query.project([
[
"'referencedBy'",
Query.new()
|> Query.filter([
%{"_type" => "'#{params["type"]}'"},
%{"references" => "^._id", :nest => true}
])
|> Query.project([
"_id"
])
|> Query.build!()
]
])
end).()

query_limited =
query
Expand Down Expand Up @@ -88,7 +84,16 @@ defmodule Router.Api.V1.Tags do
end)
else
false ->
conn |> error_res(400, "Invalid request", [%{message: "Invalid or missing parameters"}])
conn
|> error_res(400, "Invalid request", [
%{message: "Limit and skip must be positive integers"}
])

{:error, reason} ->
conn
|> error_res(400, "Invalid request", [
%{message: "Invalid or malformed params", detail: reason}
])
end
end
end
Loading

0 comments on commit 020044d

Please sign in to comment.