-
-
Save shankardevy/e4469a87002ad986a8b4bdd2cc74c539 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 MangoWeb.Plugs.FetchCart do | |
import Plug.Conn | |
alias Mango.Sales | |
alias Mango.Sales.Order | |
def init(_opts), do: nil | |
def call(conn, _) do | |
with cart_id <- get_session(conn, :cart_id), | |
true <- is_integer(cart_id), | |
%Order{} = cart <- Sales.get_cart(cart_id) | |
do | |
conn |> assign(:cart, cart) | |
else | |
_ -> cart = Sales.create_cart() | |
conn | |
|> put_session(:cart_id, cart.id) | |
|> assign(:cart, cart) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment