-
-
Save wende/cdbdb74bc6776f23e2ea40df4754720e to your computer and use it in GitHub Desktop.
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
defmodule USPDistributorWeb.OrderController do | |
use USPDistributorWeb, :controller | |
alias USPDistributor.Orders | |
alias USPDistributor.Orders.Order | |
action_fallback USPDistributorWeb.FallbackController | |
plug Guardian.Permissions.Bitwise, [ensure: %{admin: [:list_orders]}] when action in [:index] | |
plug Guardian.Permissions.Bitwise, [ensure: %{order: [:create]}] when action in [:create] | |
plug Guardian.Permissions.Bitwise, [ensure: %{order: [:view]}] when action in [:show] | |
plug Guardian.Permissions.Bitwise, [ensure: %{order: [:edit_details]}] when action in [:update] | |
plug Guardian.Permissions.Bitwise, [ensure: %{admin: [:delete_order]}] when action in [:delete] | |
def index(conn, _params) do | |
orders = Orders.list_orders() | |
render(conn, "index.json", orders: orders) | |
end | |
def create(conn, %{"order" => order_params}) do | |
with {:ok, %Order{} = order} <- Orders.create_order(order_params) do | |
conn | |
|> put_status(:created) | |
|> put_resp_header("location", order_path(conn, :show, order)) | |
|> render("show.json", order: order) | |
end | |
end | |
def show(conn, %{"id" => id}) do | |
order = Orders.get_order!(id) | |
owner = USPDistributor.Guardian.Plug.current_resource(conn) | |
if(owner.role != :distributor || owner.id == order.distributor_id) do | |
render(conn, "show.json", order: order) | |
else | |
send_resp(conn, 401, Poison.encode!(%{message: "Aunatuhorized"})) | |
end | |
end | |
def update(conn, %{"id" => id, "order" => order_params}) do | |
order = Orders.get_order!(id) | |
owner = USPDistributor.TestGuardian.Plug.current_resource(conn) | |
if(owner.role != :distributor || owner.id == order.distributor_id) do | |
with {:ok, %Order{} = order} <- Orders.update_order(order, order_params) do | |
render(conn, "show.json", order: order) | |
end | |
else | |
send_resp(conn, 401, Poison.encode!(%{message: "Aunatuhorized"})) | |
end | |
end | |
def update_price(conn, %{"id" => id, "order" => %{"price" => price } }) do | |
order = Orders.get_order!(id) | |
with {:ok, %Order{} = order} <- Orders.update_price_order(order, %{"price" => price}) do | |
render(conn, "show.json", order: order) | |
end | |
end | |
def delete(conn, %{"id" => id}) do | |
order = Orders.get_order!(id) | |
with {:ok, %Order{}} <- Orders.delete_order(order) do | |
send_resp(conn, :no_content, "") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment