Skip to content

Instantly share code, notes, and snippets.

@shankardevy

shankardevy/sales.ex Secret

Last active Mar 8, 2021
Embed
What would you like to do?
defmodule Mango.Sales do
alias Mango.Repo
alias Mango.Sales.Order
def get_cart(id) do
Order
|> Repo.get_by(id: id, status: "In Cart")
end
def create_cart do
%Order{status: "In Cart"} |> Repo.insert!()
end
def add_to_cart(%Order{line_items: []} = cart, cart_params) do
attrs = %{line_items: [cart_params]}
update_cart(cart, attrs)
end
def add_to_cart(%Order{line_items: existing_line_items} = cart, cart_params) do
new_item = %{
product_id: String.to_integer(cart_params["product_id"]),
quantity: String.to_integer(cart_params["quantity"])
}
existing_line_items = existing_line_items |> Enum.map(&Map.from_struct/1)
attrs = %{line_items: [new_item | existing_line_items]}
update_cart(cart, attrs)
end
def update_cart(cart, attrs) do
cart
|> Order.changeset(attrs)
|> Repo.update
end
end
@hlxinfeng

This comment has been minimized.

Copy link

@hlxinfeng hlxinfeng commented Mar 8, 2021

Hello Sir,
can you please share the code how to do item consolidation like in mangoweb.

Thankyou

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment