Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule PhoenixBanditWeb.BenchmarkController do

@compile {:inline, clamp_int: 3, sum_params: 1}

@body_length 16_384

def pipeline(conn, _params) do
conn
|> put_resp_content_type("text/plain")
Expand Down Expand Up @@ -106,7 +108,7 @@ defmodule PhoenixBanditWeb.BenchmarkController do
end

def upload(conn, _params) do
size = read_body_chunks(conn, 0)
{size, conn} = read_body_chunks(conn, 0)

conn
|> put_resp_header("server", "Phoenix")
Expand Down Expand Up @@ -173,9 +175,9 @@ defmodule PhoenixBanditWeb.BenchmarkController do
end

defp read_body_chunks(conn, acc_size) do
case read_body(conn) do
{:ok, binary, _conn} ->
acc_size + byte_size(binary)
case read_body(conn, length: @body_length) do
{:ok, binary, conn} ->
{acc_size + byte_size(binary), conn}

{:more, binary, conn} ->
read_body_chunks(conn, acc_size + byte_size(binary))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ defmodule PhoenixBanditWeb.CrudController do

def list(conn, params) do
category = Map.get(params, "category", "electronics")
page = params |> Map.get("page", "1") |> String.to_integer() |> max(1)
limit = params |> Map.get("limit", "10") |> String.to_integer() |> max(1) |> min(50)
page = params |> Map.get("page", "1") |> String.to_integer()
limit = params |> Map.get("limit", "10") |> String.to_integer()

offset = (page - 1) * limit

Expand Down Expand Up @@ -80,12 +80,20 @@ defmodule PhoenixBanditWeb.CrudController do
price,
quantity
]) do
{:ok, %Postgrex.Result{rows: [row]}} ->
{:ok, %Postgrex.Result{num_rows: 1}} ->
:ets.delete(:items_cache, to_string(id))

item = %{
id: id,
name: name,
category: category,
price: price,
quantity: quantity
}

conn
|> put_status(:created)
|> json(row_to_item(row))
|> json(item)

_error ->
send_resp(conn, 500, "")
Expand All @@ -104,10 +112,17 @@ defmodule PhoenixBanditWeb.CrudController do
quantity,
item_id
]) do
{:ok, %Postgrex.Result{rows: [row]}} ->
{:ok, %Postgrex.Result{num_rows: 1}} ->
:ets.delete(:items_cache, id)

json(conn, row_to_item(row))
item = %{
id: item_id,
name: name,
price: price,
quantity: quantity
}

json(conn, item)

{:ok, %Postgrex.Result{num_rows: 0}} ->
send_resp(conn, 404, "")
Expand Down