Store user that created a property
This commit is contained in:
parent
8d9da4ea40
commit
57658a6857
7 changed files with 33 additions and 6 deletions
|
@ -51,7 +51,7 @@ defmodule RealEstate.Properties do
|
|||
"""
|
||||
def create_property(attrs \\ %{}) do
|
||||
%Property{}
|
||||
|> Property.changeset(attrs)
|
||||
|> Property.create_changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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 %>
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue