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 3daff26
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
27 changes: 20 additions & 7 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 @@ -21,7 +24,7 @@ defmodule Router.Api.V1.Inc do
case result do
:ok ->
conn
|> json_res(200, %{code: 200, message: "OK", data: %{id: id, target: "views"}})
|> json_res(200, %{code: 200, data: %{id: id, target: "views"}})

:error ->
conn
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
3 changes: 2 additions & 1 deletion svelte-app/src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ const incViews = (fetch: RouteFetch, doc: DocumentRegistry[keyof DocumentRegistr
method: 'POST',
body: JSON.stringify({
id: doc._id,
action: 'inc'
action: 'inc',
field: 'views'
})
});
} catch (e) {
Expand Down
9 changes: 5 additions & 4 deletions svelte-app/src/routes/api/[[version]]/mutate/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@ export const POST = (async ({ request }) => {
body: JSON.stringify({ target: 'views' })
})
.then((res) => res.json())
.catch((e) => ({ error: e }));
.catch((e) => ({ message: e }));

if (remoteRes.message || remoteRes.error) {
if (remoteRes.code !== 200 || remoteRes.message) {
return endpointResponse(
{
status: remoteRes.code ?? 500,
error: remoteRes.message ?? remoteRes.error,
error: remoteRes.message ?? 'Unknown error',
detail: remoteRes.detail
},
remoteRes.code ?? 500
);
}

return endpointResponse({
status: 200
status: 200,
data: { id: remoteRes.data.id, views: remoteRes.data.views }
});
}
default: {
Expand Down

0 comments on commit 3daff26

Please sign in to comment.