add tracking saved to db

This commit is contained in:
2021-09-14 17:24:08 +02:00
parent 1af37846bd
commit 42e025af12
6 changed files with 36 additions and 19 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ defmodule PokemonCouture.Accounts.User do
field :password, :string, virtual: true
field :hashed_password, :string
field :confirmed_at, :naive_datetime
many_to_many :clothes, PokemonCouture.Shops.Clothes, join_through: "ownerships", unique: :true
many_to_many :clothes, PokemonCouture.Shops.Clothes, join_through: "ownerships", unique: :true, on_replace: :delete
timestamps()
end
+17
View File
@@ -21,6 +21,9 @@ defmodule PokemonCouture.Shops do
Repo.all(Clothes)
end
def list_clothes_with_owners do
Repo.all(from c in Clothes, preload: [:users])
end
@doc """
Gets a single clothes.
@@ -101,4 +104,18 @@ defmodule PokemonCouture.Shops do
def change_clothes(%Clothes{} = clothes, attrs \\ %{}) do
Clothes.changeset(clothes, attrs)
end
def add_owner(clothes, user) do
clothes
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:users, [user | clothes.users])
|> Repo.update!()
end
def remove_owner(clothes, user) do
clothes
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:users, List.delete(clothes.users, user))
|> Repo.update!()
end
end
+1 -1
View File
@@ -8,7 +8,7 @@ defmodule PokemonCouture.Shops.Clothes do
field :game, :string
field :location, :string
many_to_many :users, PokemonCouture.Accounts.User, join_through: "ownerships", unique: :true
many_to_many :users, PokemonCouture.Accounts.User, join_through: "ownerships", unique: :true, on_replace: :delete
timestamps()
end