Store user that created a property

This commit is contained in:
Joao Gilberto Balsini Moura 2020-09-14 20:45:56 -03:00
parent 8d9da4ea40
commit 57658a6857
7 changed files with 33 additions and 6 deletions

View file

@ -51,7 +51,7 @@ defmodule RealEstate.Properties do
""" """
def create_property(attrs \\ %{}) do def create_property(attrs \\ %{}) do
%Property{} %Property{}
|> Property.changeset(attrs) |> Property.create_changeset(attrs)
|> Repo.insert() |> Repo.insert()
end end

View file

@ -11,6 +11,12 @@ defmodule RealEstate.Properties.Property do
timestamps() timestamps()
end 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 @doc false
def changeset(property, attrs) do def changeset(property, attrs) do
property property

View file

@ -41,6 +41,9 @@ defmodule RealEstateWeb.PropertyLive.FormComponent do
end end
defp save_property(socket, :new, property_params) do 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 case Properties.create_property(property_params) do
{:ok, _property} -> {:ok, _property} ->
{:noreply, {:noreply,

View file

@ -5,7 +5,8 @@ defmodule RealEstateWeb.PropertyLive.Index do
alias RealEstate.Properties.Property alias RealEstate.Properties.Property
@impl true @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())} {:ok, assign(socket, :properties, list_properties())}
end end

View file

@ -6,6 +6,7 @@
title: @page_title, title: @page_title,
action: @live_action, action: @live_action,
property: @property, property: @property,
current_user: @current_user,
return_to: Routes.property_index_path(@socket, :index) %> return_to: Routes.property_index_path(@socket, :index) %>
<% end %> <% end %>

View file

@ -2,18 +2,26 @@ defmodule RealEstate.PropertiesTest do
use RealEstate.DataCase use RealEstate.DataCase
alias RealEstate.Properties alias RealEstate.Properties
import RealEstate.AccountsFixtures
describe "properties" do describe "properties" do
alias RealEstate.Properties.Property alias RealEstate.Properties.Property
@valid_attrs %{description: "some description", name: "some name", price: "120.5"} @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} @invalid_attrs %{description: nil, name: nil, price: nil}
def property_fixture(attrs \\ %{}) do def property_fixture(attrs \\ %{}) do
user = user_fixture()
{:ok, property} = {:ok, property} =
attrs attrs
|> Enum.into(@valid_attrs) |> Enum.into(@valid_attrs)
|> Enum.into(%{user_id: user.id})
|> Properties.create_property() |> Properties.create_property()
property property
@ -30,14 +38,20 @@ defmodule RealEstate.PropertiesTest do
end end
test "create_property/1 with valid data creates a property" do 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.description == "some description"
assert property.name == "some name" assert property.name == "some name"
assert property.price == Decimal.new("120.5") assert property.price == Decimal.new("120.5")
assert property.user_id == user.id
end end
test "create_property/1 with invalid data returns error changeset" do 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 end
test "update_property/2 with valid data updates the property" do test "update_property/2 with valid data updates the property" do

View file

@ -15,7 +15,9 @@ defmodule RealEstateWeb.PropertyLiveTest do
@invalid_attrs %{description: nil, name: nil, price: nil} @invalid_attrs %{description: nil, name: nil, price: nil}
defp fixture(:property) do 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 property
end end