Skip to content

Commit

Permalink
fix: No idea why this was broken but ok
Browse files Browse the repository at this point in the history
  • Loading branch information
kiosion committed Nov 8, 2023
1 parent bd9bd7c commit 2b587ac
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions elixir-api/lib/routes/api/v1/inc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ defmodule Router.Api.V1.Inc do
|> send_resp(200, "")
end

@spec handle_inc(Plug.Conn.t(), binary(), map()) :: Plug.Conn.t()
def handle_inc(conn, id, body) when is_map(body) do
if body["target"] == "views" do
@spec handle_inc(Plug.Conn.t(), binary(), binary()) :: Plug.Conn.t()
def handle_inc(conn, id, target) when is_binary(target) do
Logger.info("Incrementing view count for #{id}")
Logger.info("Incrementing for target: #{target}")

if target == "views" do
query = "*[_id == '#{id}'][0]"
{:ok, _pid} = try_increment_view_count(query)

Expand All @@ -37,16 +40,26 @@ defmodule Router.Api.V1.Inc do
end
end

def handle_inc(conn, _id, _body),
def handle_inc(conn, _id, _target),
do: conn |> error_res(400, "Bad Request", "Invalid body content")

post "/:id" do
if is_binary(conn.params["id"]) do
try do
handle_inc(conn, conn.params["id"], conn.body_params)
{:ok, body, _conn} = read_body(conn)

# No fucking clue why this isn't working with *just* `read_body/1`
# Do not have the energy so this shall do for now.
decoded_body =
case Poison.decode(body) do
{:ok, decoded_body} when is_map(decoded_body) -> decoded_body
_ -> conn.body_params
end

handle_inc(conn, conn.params["id"], decoded_body["target"])
rescue
e ->
IO.puts("caught error: #{inspect(e)}")
Logger.error("Failed to increment view count: #{inspect(e)}")
conn |> error_res(400, "Bad Request", "Invalid body content")
end
else
Expand Down

0 comments on commit 2b587ac

Please sign in to comment.