diff --git a/lib/diff_web/controllers/page_controller.ex b/lib/diff_web/controllers/page_controller.ex index f63b45d..e78f1a2 100644 --- a/lib/diff_web/controllers/page_controller.ex +++ b/lib/diff_web/controllers/page_controller.ex @@ -28,18 +28,13 @@ defmodule DiffWeb.PageController do end end - def expand_context(conn, %{"file_name" => file_name} = params) do - %{ - "version" => version, - "package" => package, - "from_line" => from_line, - "direction" => direction - } = params - - case parse_version(version) do + def expand_context(conn, %{"file_name" => _file_name} = params) do + case parse_version(params["version"]) do {:ok, version} -> version = to_string(version) - do_expand_context(conn, package, version, file_name, from_line, direction) + params = Map.update!(params, "version", fn _ -> version end) + + do_expand_context(conn, params) :error -> conn @@ -96,10 +91,22 @@ defmodule DiffWeb.PageController do end end - defp do_expand_context(conn, package, version, file_name, from_line, direction) do - case Diff.Hex.get_chunk(package, version, file_name, from_line, direction) do + defp do_expand_context(conn, params) do + chunk_extractor_params = + for {key, val} <- params, into: %{} do + {String.to_existing_atom(key), val} + end + + case Diff.Hex.get_chunk(chunk_extractor_params) do {:ok, chunk} -> - json(conn, %{chunk: chunk}) + rendered_chunk = + Phoenix.View.render_to_string(DiffWeb.RenderView, "render_context_chunk.html", + chunk: chunk + ) + + conn + |> put_status(200) + |> json(%{chunk: rendered_chunk, lines: length(chunk)}) {:error, %{errors: errors}} -> conn diff --git a/lib/diff_web/router.ex b/lib/diff_web/router.ex index 191e07e..b9427d0 100644 --- a/lib/diff_web/router.ex +++ b/lib/diff_web/router.ex @@ -15,7 +15,11 @@ defmodule DiffWeb.Router do pipe_through :browser live "/", SearchLiveView - get "/diff/:package/:version/expand/:from_line/:direction", PageController, :expand_context + + get "/diff/:package/:version/expand/:from_line/:to_line/:right_line/", + PageController, + :expand_context + get "/diff/:package/:versions", PageController, :diff get "/diffs", PageController, :diffs end diff --git a/lib/diff_web/templates/render/render_context_chunk.html.eex b/lib/diff_web/templates/render/render_context_chunk.html.eex new file mode 100644 index 0000000..90396ee --- /dev/null +++ b/lib/diff_web/templates/render/render_context_chunk.html.eex @@ -0,0 +1,15 @@ +<%= for line <- @chunk do %> + + +
+ <%= line_number(line.from_line_number) %> +
+
+ <%= line_number(line.to_line_number) %> +
+ + +
<%= line_text(line.text) %>
+ + +<% end %> diff --git a/test/diff_web/controllers/page_controller_test.exs b/test/diff_web/controllers/page_controller_test.exs index 7110c3a..9ee0bcd 100644 --- a/test/diff_web/controllers/page_controller_test.exs +++ b/test/diff_web/controllers/page_controller_test.exs @@ -42,35 +42,23 @@ defmodule DiffWeb.PageControllerTest do end end - describe "/GET /diff/:package/:versions/expand/:from_line/:direction" do + describe "/GET /diff/:package/:versions/expand/:from/:to/:right_line" do setup :verify_on_exit! test "requires file_name query param", %{conn: conn} do - conn = get(conn, "/diff/phoenix/1.4.5/expand/1/down") + conn = get(conn, "/diff/phoenix/1.4.5/expand/1/1/1") assert %{"error" => "missing query parameter: file_name"} = json_response(conn, 400) end - test "doesn't accept direction other that up or down", %{conn: conn} do - conn = get(conn, "/diff/phoenix/1.4.5/expand/1/left?file_name=mix.exs") - assert %{"errors" => %{"direction" => error}} = json_response(conn, 400) - assert error =~ ~r/direction must be either \"up\" or \"down\"/ - end - - test "doesn't accept negative from_line", %{conn: conn} do - conn = get(conn, "/diff/phoenix/1.4.5/expand/-2/up?file_name=mix.exs") + test "doesn't accept when to_line is less than from_line", %{conn: conn} do + conn = get(conn, "/diff/phoenix/1.4.5/expand/2/1/1?file_name=mix.exs") assert json_response(conn, 400) end test "returns chunk of the file", %{conn: conn} do - conn = get(conn, "/diff/phoenix/1.4.5/expand/2/up?file_name=mix.exs") + conn = get(conn, "/diff/phoenix/1.4.5/expand/1/4/1?file_name=mix.exs") assert %{"chunk" => chunk} = json_response(conn, 200) - - assert [ - %{"line_text" => "defmodule Phoenix.MixProject do"}, - %{"line_text" => " use Mix.Project"} - ] = chunk - - assert 2 = length(chunk) + assert chunk =~ "defmodule Phoenix.MixProject do" end end end