Skip to content

Commit

Permalink
chore: Clean up some dupe api code
Browse files Browse the repository at this point in the history
  • Loading branch information
kiosion committed Sep 30, 2023
1 parent 3db36a7 commit 5ec18cd
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 110 deletions.
53 changes: 17 additions & 36 deletions elixir-api/lib/routes/api/v1/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,35 +73,26 @@ defmodule Router.Api.V1.Post do

conn
|> handle_sanity_fetch(query, fn conn, result, duration ->
count =
{translated_result, meta, code} =
case result["result"] do
nil -> 0
_ -> 1
end

translated_result =
case params["lang"] do
"en" -> result
"fr" -> Translate.translate(:post, result, "fr", "en")
_ -> conn |> error_res(400, "Invalid request", "Invalid language") |> halt()
end
|> case do
{:error, message} ->
Logger.error("Error translating post: #{inspect(message)}")
result

translated_result ->
translated_result
end
nil ->
{result,
%{
"total" => 0,
"count" => 0,
"id" => nil
}, 404}

code =
if count == 0 do
404
else
200
_ ->
{Translate.handle_translate(:post, result, params["lang"]),
%{
"total" => 1,
"count" => 1,
"id" => params["id"]
}, 200}
end

case count do
case meta["count"] do
0 ->
translated_result

Expand All @@ -112,17 +103,7 @@ defmodule Router.Api.V1.Post do
PT.build_summary(translated_result["result"]["body"])
)
end
|> Map.put("meta", %{
"total" => count,
"count" => count,
"id" =>
if result["result"] != nil do
params["id"]
else
nil
end
})
|> Map.update("ms", duration, &(&1 + (duration - &1)))
|> update_meta(meta, duration)
|> (fn data -> conn |> json_res(code, %{code: code, data: data}) end).()
end)
else
Expand Down
45 changes: 26 additions & 19 deletions elixir-api/lib/routes/api/v1/posts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,33 @@ defmodule Router.Api.V1.Posts do
|> handle_sanity_fetch(query_limited, fn conn, result, duration ->
parsed_counts = Task.await(counts)

case params["lang"] do
"en" -> result
"fr" -> Translate.translate(:posts, result, "fr", "en")
_ -> conn |> error_res(400, "Invalid request", "Invalid language") |> halt()
end
|> case do
{:error, message} ->
Logger.error("Error fetching posts: #{inspect(message)}")
result
{translated_result, meta} =
case result["result"] do
nil ->
{
result,
%{
"total" => 0,
"count" => 0,
"sort" => params["s"],
"order" => params["o"]
}
}

translated_result ->
translated_result
end
|> Map.put("meta", %{
"total" => parsed_counts["result"]["total"],
"count" => parsed_counts["result"]["count"],
"sort" => params["s"],
"order" => params["o"]
})
|> Map.update("ms", duration, &(&1 + (duration - &1)))
_ ->
{
Translate.handle_translate(:posts, result, params["lang"]),
%{
"total" => parsed_counts["result"]["total"],
"count" => parsed_counts["result"]["count"],
"sort" => params["s"],
"order" => params["o"]
}
}
end

translated_result
|> update_meta(meta, duration)
|> (fn data -> conn |> json_res(200, %{code: 200, data: data}) end).()
end)
else
Expand Down
56 changes: 20 additions & 36 deletions elixir-api/lib/routes/api/v1/project.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,35 +71,29 @@ defmodule Router.Api.V1.Project do

conn
|> handle_sanity_fetch(query, fn conn, result, duration ->
count =
{translated_result, meta, code} =
case result["result"] do
nil -> 0
_ -> 1
end

translated_result =
case params["lang"] do
"en" -> result
"fr" -> Translate.translate(:project, result, "fr", "en")
_ -> conn |> error_res(400, "Invalid request", "Invalid language") |> halt()
end
|> case do
{:error, message} ->
Logger.error("Error translating post: #{inspect(message)}")
result

translated_result ->
translated_result
end
nil ->
{result,
%{
"total" => 0,
"count" => 0,
"id" => nil
}, 404}

code =
if count == 0 do
404
else
200
_ ->
{
Translate.handle_translate(:project, result, params["lang"]),
%{
"total" => 1,
"count" => 1,
"id" => params["id"]
},
200
}
end

case count do
case meta["count"] do
0 ->
translated_result

Expand All @@ -110,17 +104,7 @@ defmodule Router.Api.V1.Project do
PT.build_summary(translated_result["result"]["body"])
)
end
|> Map.put("meta", %{
"total" => count,
"count" => count,
"id" =>
if result["result"] != nil do
params["id"]
else
nil
end
})
|> Map.update("ms", duration, &(&1 + (duration - &1)))
|> update_meta(meta, duration)
|> (fn data -> conn |> json_res(code, %{code: code, data: data}) end).()
end)
else
Expand Down
45 changes: 26 additions & 19 deletions elixir-api/lib/routes/api/v1/projects.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,26 +113,33 @@ defmodule Router.Api.V1.Projects do
|> handle_sanity_fetch(query_limited, fn conn, result, duration ->
parsed_counts = Task.await(counts)

case params["lang"] do
"en" -> result
"fr" -> Translate.translate(:projects, result, "fr", "en")
_ -> conn |> error_res(400, "Invalid request", "Invalid language") |> halt()
end
|> case do
{:error, message} ->
Logger.error("Error fetching projects: #{inspect(message)}")
result
{translated_result, meta} =
case result["result"] do
nil ->
{
result,
%{
"total" => 0,
"count" => 0,
"sort" => params["s"],
"order" => params["o"]
}
}

translated_result ->
translated_result
end
|> Map.put("meta", %{
"total" => parsed_counts["result"]["total"],
"count" => parsed_counts["result"]["count"],
"sort" => params["s"],
"order" => params["o"]
})
|> Map.update("ms", duration, &(&1 + (duration - &1)))
_ ->
{
Translate.handle_translate(:projects, result, params["lang"]),
%{
"total" => parsed_counts["result"]["total"],
"count" => parsed_counts["result"]["count"],
"sort" => params["s"],
"order" => params["o"]
}
}
end

translated_result
|> update_meta(meta, duration)
|> (fn data -> conn |> json_res(200, %{code: 200, data: data}) end).()
end)
else
Expand Down
16 changes: 16 additions & 0 deletions elixir-api/lib/utils/translate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ defmodule Hexerei.Translate do
}
end

def handle_translate(type, doc, lang_param) do
case lang_param do
"en" -> doc
"fr" -> Translate.translate(type, doc, "fr", "en")
_ -> {:error, "Invalid language"}
end
|> case do
{:error, message} ->
Logger.error("Error translating #{type}: #{inspect(message)}")
doc

res ->
res
end
end

def translate(type, sanity_response, target_lang, source_lang \\ "en") do
Logger.info("Handling translation for #{type} to '#{target_lang}'")

Expand Down
6 changes: 6 additions & 0 deletions elixir-api/lib/utils/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ defmodule Hexerei.Utils do
end
end

def update_meta(map, meta, duration) do
map
|> Map.put("meta", meta)
|> Map.update("ms", duration, &(&1 + (duration - &1)))
end

def validate_query_params(params, expected) do
Enum.reduce(expected, %{}, fn {key, default}, acc ->
if Map.has_key?(params, key) do
Expand Down

0 comments on commit 5ec18cd

Please sign in to comment.