diff --git a/elixir-api/lib/routes/api/v1/post.ex b/elixir-api/lib/routes/api/v1/post.ex index e74a9827c..13c1d28ab 100644 --- a/elixir-api/lib/routes/api/v1/post.ex +++ b/elixir-api/lib/routes/api/v1/post.ex @@ -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 @@ -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 diff --git a/elixir-api/lib/routes/api/v1/posts.ex b/elixir-api/lib/routes/api/v1/posts.ex index 15c55dcdf..a59679056 100644 --- a/elixir-api/lib/routes/api/v1/posts.ex +++ b/elixir-api/lib/routes/api/v1/posts.ex @@ -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 diff --git a/elixir-api/lib/routes/api/v1/project.ex b/elixir-api/lib/routes/api/v1/project.ex index ef7d32f77..7ddeaaca3 100644 --- a/elixir-api/lib/routes/api/v1/project.ex +++ b/elixir-api/lib/routes/api/v1/project.ex @@ -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 @@ -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 diff --git a/elixir-api/lib/routes/api/v1/projects.ex b/elixir-api/lib/routes/api/v1/projects.ex index 067065608..70ef180eb 100644 --- a/elixir-api/lib/routes/api/v1/projects.ex +++ b/elixir-api/lib/routes/api/v1/projects.ex @@ -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 diff --git a/elixir-api/lib/utils/translate.ex b/elixir-api/lib/utils/translate.ex index 104948cf9..8ad9217ab 100644 --- a/elixir-api/lib/utils/translate.ex +++ b/elixir-api/lib/utils/translate.ex @@ -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}'") diff --git a/elixir-api/lib/utils/utils.ex b/elixir-api/lib/utils/utils.ex index 9861dfc33..a85829201 100644 --- a/elixir-api/lib/utils/utils.ex +++ b/elixir-api/lib/utils/utils.ex @@ -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