Use has_many through association for user feeds
This commit is contained in:
parent
93aebeb600
commit
978ee86667
|
@ -12,6 +12,7 @@ defmodule Frenzy.User do
|
|||
has_many :approved_clients, Frenzy.ApprovedClient, on_delete: :delete_all
|
||||
|
||||
has_many :groups, Frenzy.Group, on_delete: :delete_all
|
||||
has_many :feeds, through: [:groups, :feeds]
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
|
|
@ -7,13 +7,11 @@ defmodule FrenzyWeb.Fervor.ItemsController do
|
|||
plug :get_specific_item
|
||||
|
||||
def get_specific_item(%Plug.Conn{path_params: %{"id" => id}} = conn, _opts) do
|
||||
user = conn.assigns[:user] |> Repo.preload(groups: [:feeds])
|
||||
|
||||
feeds = Enum.flat_map(user.groups, fn g -> g.feeds end)
|
||||
user = conn.assigns[:user] |> Repo.preload(:feeds)
|
||||
|
||||
item = Repo.get(Item, id)
|
||||
|
||||
if Enum.any?(feeds, fn f -> f.id == item.feed_id end) do
|
||||
if Enum.any?(user.feeds, fn f -> f.id == item.feed_id end) do
|
||||
assign(conn, :item, item)
|
||||
else
|
||||
conn
|
||||
|
|
|
@ -10,9 +10,7 @@ defmodule FrenzyWeb.ItemController do
|
|||
|
||||
item = Repo.get(Item, id)
|
||||
|
||||
feeds = Enum.flat_map(user.groups, fn g -> g.feeds end)
|
||||
|
||||
if Enum.any?(feeds, fn f -> f.id == item.feed_id end) do
|
||||
if Enum.any?(user.feeds, fn f -> f.id == item.feed_id end) do
|
||||
conn
|
||||
|> assign(:item, item)
|
||||
else
|
||||
|
|
|
@ -25,7 +25,7 @@ defmodule FrenzyWeb.Plug.Authenticate do
|
|||
|> halt()
|
||||
|
||||
user ->
|
||||
user = Repo.preload(user, groups: [:feeds])
|
||||
user = Repo.preload(user, [:groups, :feeds])
|
||||
assign(conn, :user, user)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue