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