Reputation: 12433
The code below updates the Product
in changeset
. I'm also trying to update the ProductShop
with a new "price" in changeset2
but it isn't getting updated. I have inspected all the important parts, and the price
has a value, the product_shop
has a value, and "Price updated"
is printed to the console.
put "/products" do
errors = {}
IO.inspect(conn.body_params)
product = Api.Product |> Api.Repo.get(conn.query_params["p_id"])
shop = Api.Shop |> Api.Repo.get(conn.query_params["s_id"])
params = for key <- ~w(image description), value = conn.body_params[key], into: %{}, do: {key, value}
changeset = Api.Product.changeset(product, params)
case Api.Repo.update(changeset) do
{:ok, product} ->
errors = Tuple.append(errors, "Product updated")
{:error, changeset} ->
errors = Tuple.append(errors, "Product not updated")
end
pid = conn.query_params["p_id"]
sid = conn.query_params["s_id"]
price = Float.parse(conn.body_params["price"])
price1 = elem(price, 0)
IO.inspect(price1)
product_shop = Api.Repo.get_by(ProductShop, s_id: sid, p_id: pid)
IO.inspect(product_shop)
changeset2 = Api.ProductShop.changeset(product_shop, %{price: price1})
case Api.Repo.update(changeset2) do
{:ok, product_shop} ->
errors = Tuple.append(errors, "Price updated")
{:error, changeset2} ->
errors = Tuple.append(errors, "Price not updated")
end
IO.inspect(errors)
conn
|> put_resp_content_type("application/json")
|> send_resp(200, Poison.encode!(%{
successs: "success",
errors: Tuple.to_list(errors)
}))
end
Why does the ProductShop
not get updated when price
is populated and so is product_shop
?
ProductShop.ex
defmodule Api.ProductShop do
use Ecto.Schema
import Ecto.Changeset
import Api.Repo
import Ecto.Query
@derive {Poison.Encoder, only: [:s_id, :p_id]}
schema "product_shops" do
field :s_id, :integer
field :p_id, :integer
field :not_in_shop_count, :integer
field :price, :float
end
def changeset(product_shop, params \\ %{}) do
product_shop
|> cast(params, [:s_id, :p_id])
|> validate_required([:s_id, :p_id])
|> unique_constraint(:s_id, name: :unique_product_shop)
end
def insert_product_shop(conn, product_id, shop_id, price) do
changeset = Api.ProductShop.changeset(%Api.ProductShop{p_id: product_id, s_id: shop_id, not_in_shop_count: 0, price: price})
errors = changeset.errors
valid = changeset.valid?
case insert(changeset) do
{:ok, product_shop} ->
{:ok, product_shop}
{:error, changeset} ->
{:error, :failure}
end
end
def delete_all_from_product_shops do
from(Api.ProductShop) |> delete_all
end
def get_product_shops do
Api.ProductShop |> all
end
end
Upvotes: 0
Views: 139
Reputation: 4507
Your missing the :price
and :not_in_shop_count
in your cast call. Try this:
def changeset(product_shop, params \\ %{}) do
product_shop
|> cast(params, [:s_id, :p_id, :price, :not_in_shop_count])
|> validate_required([:s_id, :p_id, :price, :not_in_shop_count])
|> unique_constraint(:s_id, name: :unique_product_shop)
end
Upvotes: 1