-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
105 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule MarketWeb.Retailer.ProductController do | ||
use MarketWeb, :controller | ||
|
||
alias Market.Products | ||
|
||
action_fallback MarketWeb.FallbackController | ||
|
||
def index(conn, _params) do | ||
products = Products.list_products() | ||
render(conn, :index, products: products) | ||
end | ||
|
||
def show(conn, %{"id" => id}) do | ||
product = Products.get_product!(id) | ||
render(conn, :show, product: product) | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule MarketWeb.Retailer.ProductJSON do | ||
alias Market.Products.Product | ||
|
||
def index(%{products: products}) do | ||
%{data: for(product <- products, do: data(product))} | ||
end | ||
|
||
def show(%{product: product}) do | ||
%{data: data(product)} | ||
end | ||
|
||
defp data(%Product{} = product) do | ||
%{ | ||
id: product.id, | ||
name: product.name, | ||
vendor_id: product.vendor_id, | ||
description: product.description, | ||
price: product.price | ||
} | ||
end | ||
end |
This file contains 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
This file contains 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
37 changes: 37 additions & 0 deletions
37
test/market_web/controllers/retailer/product_controller_test.exs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule MarketWeb.Retailer.ProductControllerTest do | ||
use MarketWeb.ConnCase | ||
|
||
import Market.Factory | ||
|
||
setup :register_and_log_in_retailer | ||
|
||
setup %{conn: conn} do | ||
{:ok, conn: put_req_header(conn, "accept", "application/json")} | ||
end | ||
|
||
describe "index" do | ||
test "lists all products", %{conn: conn} do | ||
insert_list(2, :product, vendor: insert(:vendor)) | ||
|
||
conn = get(conn, ~p"/retailers/products") | ||
assert length(json_response(conn, 200)["data"]) == 2 | ||
end | ||
end | ||
|
||
describe "show product" do | ||
test "renders product", %{conn: conn} do | ||
vendor = insert(:vendor) | ||
product = insert(:product, vendor: vendor) | ||
|
||
conn = get(conn, ~p"/retailers/products/#{product.id}") | ||
|
||
assert %{ | ||
"id" => product.id, | ||
"description" => product.description, | ||
"name" => product.name, | ||
"price" => product.price, | ||
"vendor_id" => vendor.id | ||
} == json_response(conn, 200)["data"] | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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