Last active
September 21, 2017 10:27
-
-
Save skota/65eeeff0eec38622996dda57021ae9f2 to your computer and use it in GitHub Desktop.
gist of code causing error in template
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
order supplies schema | |
-------------------------------------------------- | |
defmodule Ostobuddy.Ostomy.OrderSupplies do | |
use Ecto.Schema | |
import Ecto.Changeset | |
alias Ostobuddy.Ostomy.Order | |
alias Ostobuddy.Ostomy.OrderSupplies | |
schema "order_supplies" do | |
field :user_id, :integer, null: false | |
field :supply_id, :integer, null: false | |
field :qty_ordered, :integer, null: false | |
field :price, :decimal, null: false | |
timestamps() | |
belongs_to :order, Order | |
end | |
@required_fields ~w(user_id supply_id qty_ordered price)a | |
@all_fields ~w(user_id supply_id qty_ordered price)a | |
def changeset(%OrderSupplies{} = order_supplies, attrs) do | |
order_supplies | |
|> cast(attrs, @all_fields ) | |
|> validate_required(@required_fields) | |
|> check_user_owns_supplies | |
|> validate_number(:user_id, greater_than: 0) | |
|> validate_number(:supply_id, greater_than: 0) | |
|> validate_number(:qty_ordered, greater_than: 0) | |
|> validate_number(:price, greater_than: 0) | |
end | |
defp check_user_owns_supplies(changeset) do | |
user_id = get_field(changeset, :user_id) | |
supply_id= get_field(changeset, :supply_id) | |
if Ostobuddy.Ostomy.user_owns_supply?(user_id, supply_id) == nil do | |
changeset = | |
add_error(changeset, :supply_id, "You cannot use a supply you do not own.") | |
|> put_change(:valid?, false) | |
changeset | |
else | |
changeset #no change... | |
end | |
end | |
end | |
------ Query in repo ----------- | |
-- this function is called from controlloer OrderSupplies | |
def list_ordersupplies(id) do | |
query = from o in OrderSupplies, | |
join: s in Supply, where: s.id == o.supply_id, | |
select: [ o.id, s.product_name, o.supply_id, o.qty_ordered, o.price], | |
where: o.order_id == ^id, | |
order_by: [desc: o.inserted_at] | |
Repo.all(query) | |
end | |
----- the view template ----------- | |
<div class="panel-body"> | |
<div class="table-responsive"> | |
<table class="table table-bordered table-hover table-striped"> | |
<thead> | |
<tr> | |
<th>Supply</th> | |
<th>Qty Ordered<br/> | |
<th>Price</th> | |
</tr> | |
</thead> | |
<%= @orders_supplies %> | |
<tbody> | |
<%= for order_supply <- @orders_supplies do %> | |
<tr> | |
<td><%= order_supply.product_name %></td> | |
<td><%= order_supply.qty_ordered %></td> | |
<td><%= Decimal.to_string(order_supply.price) %></td> | |
</tr> | |
<% end %> | |
</tbody> | |
</table> | |
</div> | |
</div> | |
------- and this is the controller method that fetches ordersupplies ------- | |
def index(conn, %{"id" => id}) do | |
user = Guardian.Plug.current_resource(conn) | |
orders_supplies = Ostomy.list_ordersupplies(id) | |
render(conn, "index.html", orders_supplies: orders_supplies) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment