diff --git a/lib/real_estate/properties.ex b/lib/real_estate/properties.ex index bca299af..5116d08c 100644 --- a/lib/real_estate/properties.ex +++ b/lib/real_estate/properties.ex @@ -51,7 +51,7 @@ defmodule RealEstate.Properties do """ def create_property(attrs \\ %{}) do %Property{} - |> Property.changeset(attrs) + |> Property.create_changeset(attrs) |> Repo.insert() end diff --git a/lib/real_estate/properties/property.ex b/lib/real_estate/properties/property.ex index 2786b1f5..9be09847 100644 --- a/lib/real_estate/properties/property.ex +++ b/lib/real_estate/properties/property.ex @@ -11,6 +11,12 @@ defmodule RealEstate.Properties.Property do timestamps() end + def create_changeset(property, attrs) do + property + |> cast(attrs, [:name, :price, :description, :user_id]) + |> validate_required([:name, :price, :description, :user_id]) + end + @doc false def changeset(property, attrs) do property diff --git a/lib/real_estate_web/live/property_live/form_component.ex b/lib/real_estate_web/live/property_live/form_component.ex index 7097e6ea..0d8d3146 100644 --- a/lib/real_estate_web/live/property_live/form_component.ex +++ b/lib/real_estate_web/live/property_live/form_component.ex @@ -41,6 +41,9 @@ defmodule RealEstateWeb.PropertyLive.FormComponent do end defp save_property(socket, :new, property_params) do + current_user = socket.assigns.current_user + property_params = Map.put(property_params, "user_id", current_user.id) + case Properties.create_property(property_params) do {:ok, _property} -> {:noreply, diff --git a/lib/real_estate_web/live/property_live/index.ex b/lib/real_estate_web/live/property_live/index.ex index edd658af..2fddfa7d 100644 --- a/lib/real_estate_web/live/property_live/index.ex +++ b/lib/real_estate_web/live/property_live/index.ex @@ -5,7 +5,8 @@ defmodule RealEstateWeb.PropertyLive.Index do alias RealEstate.Properties.Property @impl true - def mount(_params, _session, socket) do + def mount(_params, session, socket) do + socket = assign_defaults(session, socket) {:ok, assign(socket, :properties, list_properties())} end diff --git a/lib/real_estate_web/live/property_live/index.html.leex b/lib/real_estate_web/live/property_live/index.html.leex index 3d99ff25..9f5c80c5 100644 --- a/lib/real_estate_web/live/property_live/index.html.leex +++ b/lib/real_estate_web/live/property_live/index.html.leex @@ -6,6 +6,7 @@ title: @page_title, action: @live_action, property: @property, + current_user: @current_user, return_to: Routes.property_index_path(@socket, :index) %> <% end %> diff --git a/test/real_estate/properties_test.exs b/test/real_estate/properties_test.exs index ae063e51..faa853d1 100644 --- a/test/real_estate/properties_test.exs +++ b/test/real_estate/properties_test.exs @@ -2,18 +2,26 @@ defmodule RealEstate.PropertiesTest do use RealEstate.DataCase alias RealEstate.Properties + import RealEstate.AccountsFixtures describe "properties" do alias RealEstate.Properties.Property @valid_attrs %{description: "some description", name: "some name", price: "120.5"} - @update_attrs %{description: "some updated description", name: "some updated name", price: "456.7"} + @update_attrs %{ + description: "some updated description", + name: "some updated name", + price: "456.7" + } @invalid_attrs %{description: nil, name: nil, price: nil} def property_fixture(attrs \\ %{}) do + user = user_fixture() + {:ok, property} = attrs |> Enum.into(@valid_attrs) + |> Enum.into(%{user_id: user.id}) |> Properties.create_property() property @@ -30,14 +38,20 @@ defmodule RealEstate.PropertiesTest do end test "create_property/1 with valid data creates a property" do - assert {:ok, %Property{} = property} = Properties.create_property(@valid_attrs) + user = user_fixture() + create_attributes = Enum.into(%{user_id: user.id}, @valid_attrs) + + assert {:ok, %Property{} = property} = Properties.create_property(create_attributes) assert property.description == "some description" assert property.name == "some name" assert property.price == Decimal.new("120.5") + assert property.user_id == user.id end test "create_property/1 with invalid data returns error changeset" do - assert {:error, %Ecto.Changeset{}} = Properties.create_property(@invalid_attrs) + user = user_fixture() + create_attributes = Enum.into(%{user_id: user.id}, @invalid_attrs) + assert {:error, %Ecto.Changeset{}} = Properties.create_property(create_attributes) end test "update_property/2 with valid data updates the property" do diff --git a/test/real_estate_web/live/property_live_test.exs b/test/real_estate_web/live/property_live_test.exs index 4ed26794..f3376343 100644 --- a/test/real_estate_web/live/property_live_test.exs +++ b/test/real_estate_web/live/property_live_test.exs @@ -15,7 +15,9 @@ defmodule RealEstateWeb.PropertyLiveTest do @invalid_attrs %{description: nil, name: nil, price: nil} defp fixture(:property) do - {:ok, property} = Properties.create_property(@create_attrs) + user = user_fixture() + create_attributes = Enum.into(%{user_id: user.id}, @create_attrs) + {:ok, property} = Properties.create_property(create_attributes) property end