Use has_many through association for user feeds

This commit is contained in:
Shadowfacts 2020-07-19 10:36:32 -04:00
parent 93aebeb600
commit 978ee86667
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 5 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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