Created
March 28, 2018 18:34
-
-
Save iamvery/5c706a1e89a8f3f5d2bcd672207c6228 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/pants_web/endpoint.ex b/lib/pants_web/endpoint.ex | |
index c24852a..a8ae2cf 100644 | |
--- a/lib/pants_web/endpoint.ex | |
+++ b/lib/pants_web/endpoint.ex | |
@@ -45,7 +45,19 @@ defmodule PantsWeb.Endpoint do | |
signing_salt: "HqMY6W5G" | |
) | |
- plug(PantsWeb.Router) | |
+ def version(conn, []) do | |
+ content_type = get_req_header(conn, "content-type") | |
+ "application/json; version=" <> version = hd content_type | |
+ | |
+ if version == "2" do | |
+ PantsWeb.V2.Router.call(conn, []) | |
+ else | |
+ PantsWeb.Router.call(conn, []) | |
+ end | |
+ end | |
+ plug(:version) | |
+ | |
+ #plug(PantsWeb.Router) | |
@doc """ | |
Callback invoked for dynamically configuring the endpoint. | |
diff --git a/lib/pants_web/v2/controllers/jean_controller.ex b/lib/pants_web/v2/controllers/jean_controller.ex | |
new file mode 100644 | |
index 0000000..a392082 | |
--- /dev/null | |
+++ b/lib/pants_web/v2/controllers/jean_controller.ex | |
@@ -0,0 +1,13 @@ | |
+defmodule PantsWeb.V2.JeanController do | |
+ use PantsWeb, :controller | |
+ | |
+ alias Pants.Wardrobe | |
+ alias Pants.Wardrobe.Jean | |
+ | |
+ action_fallback(PantsWeb.FallbackController) | |
+ | |
+ def index(conn, _params) do | |
+ jeans = Wardrobe.list_jeans() | |
+ render(conn, PantsWeb.JeanView, :index, jeans: jeans, version: "2") | |
+ end | |
+end | |
diff --git a/lib/pants_web/v2/router.ex b/lib/pants_web/v2/router.ex | |
new file mode 100644 | |
index 0000000..06ebba0 | |
--- /dev/null | |
+++ b/lib/pants_web/v2/router.ex | |
@@ -0,0 +1,13 @@ | |
+defmodule PantsWeb.V2.Router do | |
+ use PantsWeb, :router | |
+ | |
+ pipeline :api do | |
+ plug(:accepts, ["json"]) | |
+ end | |
+ | |
+ scope "/api", PantsWeb.V2 do | |
+ pipe_through(:api) | |
+ | |
+ resources("/jeans", JeanController, only: [:index]) | |
+ end | |
+end | |
diff --git a/lib/pants_web/views/jean_view.ex b/lib/pants_web/views/jean_view.ex | |
index fa5dd53..be6d429 100644 | |
--- a/lib/pants_web/views/jean_view.ex | |
+++ b/lib/pants_web/views/jean_view.ex | |
@@ -2,6 +2,10 @@ defmodule PantsWeb.JeanView do | |
use PantsWeb, :view | |
alias PantsWeb.JeanView | |
+ def render("index.json", %{jeans: jeans, version: "2"}) do | |
+ %{jeans: render_many(jeans, JeanView, "jean.json")} | |
+ end | |
+ | |
def render("index.json", %{jeans: jeans}) do | |
%{data: render_many(jeans, JeanView, "jean.json")} | |
end | |
diff --git a/test/pants_web/controllers/jean_controller_test.exs b/test/pants_web/controllers/jean_controller_test.exs | |
index 6bf9221..588e88b 100644 | |
--- a/test/pants_web/controllers/jean_controller_test.exs | |
+++ b/test/pants_web/controllers/jean_controller_test.exs | |
@@ -24,6 +24,25 @@ defmodule PantsWeb.JeanControllerTest do | |
end | |
end | |
+ describe "index v2" do | |
+ test "lists all jeans", %{conn: conn} do | |
+ jean = fixture(:jean) | |
+ | |
+ conn = | |
+ conn | |
+ |> put_req_header("content-type", "application/json; version=2") | |
+ |> get(jean_path(conn, :index)) | |
+ | |
+ response = %{ | |
+ "jeans" => [ | |
+ %{"id" => jean.id, "brand" => "some brand"} | |
+ ] | |
+ } | |
+ | |
+ assert json_response(conn, 200) == response | |
+ end | |
+ end | |
+ | |
describe "create jean" do | |
test "renders jean when data is valid", %{conn: conn} do | |
conn = post(conn, jean_path(conn, :create), jean: @create_attrs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment