Skip to content

Instantly share code, notes, and snippets.

@shankardevy
Last active August 18, 2017 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shankardevy/f943b7ce7e66bfb45f642a401573d7bc to your computer and use it in GitHub Desktop.
Save shankardevy/f943b7ce7e66bfb45f642a401573d7bc to your computer and use it in GitHub Desktop.
defmodule MangoWeb.CartController do
use MangoWeb, :controller
alias Mango.Sales
def add(conn, %{"cart" => cart_params}) do
cart = conn.assigns.cart
case Sales.add_to_cart(cart, cart_params) do
{:ok, _} ->
%{"product_name" => name, "pack_size" => size, "quantity" => qty} = cart_params
message = "Product added to cart - #{name}(#{size}) x #{qty} qty"
conn
|> put_flash(:info, message)
|> redirect(to: page_path(conn, :index))
{:error, _} ->
conn
|> put_flash(:info, "Error adding product to cart")
|> redirect(to: page_path(conn, :index))
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment