Skip to content

Commit

Permalink
[feature] Added the GET list at the products resource
Browse files Browse the repository at this point in the history
  • Loading branch information
julioleitao committed Mar 15, 2019
1 parent 948988d commit 6d21f18
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 6 deletions.
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM bitwalker/alpine-elixir:1.8.0

# the source dir
ENV APP_DIR /usr/src/app/
WORKDIR $APP_DIR
COPY . $APP_DIR

# install the required frameworks and tools
RUN mix local.hex --force
RUN mix archive.install hex phx_new 1.4.0

EXPOSE 4000

# build the app
RUN mix clean && \
mix deps.get && \
mix compile
2 changes: 1 addition & 1 deletion apps/store_core/config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ use Mix.Config
config :store_core, StoreCore.Repo,
url:
System.get_env("DATABASE_URL") ||
"postgresql://sw_store_usr:sw_store_pwd@localhost/sw_store_db?pool_size=10"
"postgresql://sw_store_usr:sw_store_pwd@localhost/sw_store?pool_size=10"
6 changes: 4 additions & 2 deletions apps/store_core/lib/store_core/contexts/products.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ defmodule StoreCore.Products do
"""
alias StoreCore.{Product, Repo}

def list, do: Repo.all(Product)

def create(params) do
%Product{}
|> Product.changeset(params)
|> Repo.insert
|> Repo.insert()
end

def get(id) do
Expand All @@ -21,7 +23,7 @@ defmodule StoreCore.Products do
def update(product, params) do
product
|> Product.changeset(params)
|> Repo.update
|> Repo.update()
end

def delete(product), do: Repo.delete(product)
Expand Down
8 changes: 8 additions & 0 deletions apps/store_core/test/store_core/contexts/products_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ defmodule StoreCore.ProductsTest do
price: nil
}

test "list a Product" do
assert [] = Products.list()

product = fixture()

assert [product] = Products.list()
end

test "create a Product" do
assert %{id: id} = fixture()
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ defmodule StoreWeb.ProductController do

action_fallback StoreWeb.FallbackController

def index(conn, _) do
conn
|> render("index.json", products: Products.list())
end

def create(conn, attrs) do
with {:ok, product} <- Products.create(attrs) do
conn
Expand All @@ -22,7 +27,7 @@ defmodule StoreWeb.ProductController do
def update(conn, %{"id" => id} = params) do
with {:ok, product} <- Products.get(id),
{:ok, product} <- Products.update(product, params) do
render(conn, "show.json",product: product)
render(conn, "show.json", product: product)
end
end

Expand Down
2 changes: 1 addition & 1 deletion apps/store_web/lib/store_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ defmodule StoreWeb.Router do
scope "/api", StoreWeb do
pipe_through :api

resources "/product", ProductController, only: [:create, :show, :update, :delete]
resources "/products", ProductController, only: [:index, :create, :show, :update, :delete]
end
end
4 changes: 4 additions & 0 deletions apps/store_web/lib/store_web/views/product_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ defmodule StoreWeb.ProductView do

alias StoreWeb.ProductView

def render("index.json", %{products: products}) do
%{data: render_many(products, ProductView, "product.json")}
end

def render("show.json", %{product: product}) do
%{data: render_one(product, ProductView, "product.json")}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ defmodule StoreWeb.ProductControllerTest do
price: nil
}

test "get list of products", %{conn: conn} do
product = fixture(conn)

conn = get(conn, Routes.product_path(conn, :index))

assert [product] === json_response(conn, 200)["data"]
end

describe "post an product " do
test "with valid data", %{conn: conn} do
conn =
Expand Down
15 changes: 14 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@ services:
image: postgres:9.6
container_name: sw_store_db
environment:
- POSTGRES_DB=sw_store_db
- POSTGRES_DB=sw_store
- POSTGRES_USER=sw_store_usr
- POSTGRES_PASSWORD=sw_store_pwd
- TZ=GMT
volumes:
- ./.local_db/postgres:/var/lib/postgresql/data
ports:
- 5432:5432

app_server:
build: .
container_name: api_server
environment:
- DATABASE_URL=postgresql://sw_store_usr:sw_store_pwd@sw_store_db/sw_store?pool_size=10
command: mix phx.server
volumes:
- $PWD:/usr/src/app
ports:
- 8080:4000
depends_on:
- postgres_database

0 comments on commit 6d21f18

Please sign in to comment.