defmodule Ami.Post do use Ecto.Schema import Ecto.{ Query, Changeset } alias Ami.{ User, Lesson, Community, CoursePeriod, Comment, Resource, Like, Share, PostAttachment # Repo } schema "posts" do field(:title, :string) field(:url, :string) field(:body, :string) field(:thumbnail_url, :string) field(:created_at, :utc_datetime) field(:updated_at, :utc_datetime) field(:is_inappropriate, :boolean, default: false) field(:is_featured, :boolean, default: false) field(:flagged_as_inappropriate, :boolean, default: false) belongs_to(:author, User, foreign_key: :author_id) belongs_to(:lesson, Lesson) belongs_to(:community, Community) belongs_to(:course_period, CoursePeriod) has_many(:comments, Comment, foreign_key: :commentable_id) has_many(:likes, Like, foreign_key: :likeable_id) has_many(:resources, Resource, foreign_key: :resourceable_id) has_many(:shares, Share, foreign_key: :shareable_id) has_many(:post_attachments, PostAttachment, on_delete: :delete_all) end def preload_comments(post), do: Comment.preload_comments(post, "Post") def preload_likes(post), do: Like.preload_likes(post, "Post") def preload_resources(post), do: Resource.preload_resources(post, "Post") def preload_shares(post), do: Share.preload_shares(post, "Post") def preload_posts_query do from( p in __MODULE__, where: p.is_featured == false, where: p.is_inappropriate == false, join: a in assoc(p, :author), left_join: a_prof in assoc(a, :profile), left_join: patt in assoc(p, :post_attachments), left_join: lk in assoc(p, :likes), on: p.id == lk.likeable_id and lk.likeable_type == "Post", left_join: lku in assoc(lk, :user), left_join: lku_prof in assoc(lku, :profile), left_join: r in assoc(p, :resources), on: p.id == r.resourceable_id and r.resourceable_type == "Post", left_join: sh in assoc(p, :shares), on: p.id == sh.shareable_id and sh.shareable_type == "Post", left_join: c in assoc(p, :comments), on: p.id == c.commentable_id and c.commentable_type == "Post", left_join: catt in assoc(c, :comment_attachments), left_join: cusr in assoc(c, :user), left_join: cusr_prof in assoc(cusr, :profile), left_join: sc in assoc(c, :subcomments), left_join: lsc in assoc(sc, :likes), on: sc.id == lsc.likeable_id and lsc.likeable_type == "Comment", left_join: scu in assoc(sc, :user), left_join: scu_prof in assoc(scu, :profile), preload: [ author: {a, profile: a_prof}, post_attachments: patt, likes: {lk, user: {lku, profile: lku_prof}}, shares: sh, resources: r, comments: {c, user: {cusr, profile: cusr_prof}, comment_attachments: catt, subcomments: {sc, likes: lsc, user: {scu, profile: scu_prof}}} ], order_by: [desc: p.updated_at], limit: 5 ) end def preload_posts_query(course_period_id) do from( p in __MODULE__, where: p.is_featured == false, where: p.is_inappropriate == false, where: p.course_period_id == ^course_period_id, join: a in assoc(p, :author), left_join: a_prof in assoc(a, :profile), left_join: patt in assoc(p, :post_attachments), left_join: lk in assoc(p, :likes), on: p.id == lk.likeable_id and lk.likeable_type == "Post", left_join: lku in assoc(lk, :user), left_join: lku_prof in assoc(lku, :profile), left_join: r in assoc(p, :resources), on: p.id == r.resourceable_id and r.resourceable_type == "Post", left_join: sh in assoc(p, :shares), on: p.id == sh.shareable_id and sh.shareable_type == "Post", left_join: c in assoc(p, :comments), on: p.id == c.commentable_id and c.commentable_type == "Post", left_join: catt in assoc(c, :comment_attachments), left_join: cusr in assoc(c, :user), left_join: cusr_prof in assoc(cusr, :profile), left_join: sc in assoc(c, :subcomments), left_join: lsc in assoc(sc, :likes), on: sc.id == lsc.likeable_id and lsc.likeable_type == "Comment", left_join: scu in assoc(sc, :user), left_join: scu_prof in assoc(scu, :profile), preload: [ author: {a, profile: a_prof}, post_attachments: patt, likes: {lk, user: {lku, profile: lku_prof}}, shares: sh, resources: r, comments: {c, user: {cusr, profile: cusr_prof}, comment_attachments: catt, subcomments: {sc, likes: lsc, user: {scu, profile: scu_prof}}} ], order_by: [desc: p.updated_at], limit: 5 ) end def featured_post_query(%Community{} = community) do from( p in __MODULE__, where: p.community_id == ^community.id, where: p.is_featured == true, join: a in assoc(p, :author), left_join: prof in assoc(a, :profile), left_join: patt in assoc(p, :post_attachments), left_join: lk in assoc(p, :likes), on: lk.likeable_id == p.id and lk.likeable_type == "Post", left_join: lku in assoc(lk, :user), left_join: lku_prof in assoc(lku, :profile), left_join: r in assoc(p, :resources), on: r.resourceable_id == p.id and r.resourceable_type == "Post", left_join: sh in assoc(p, :shares), on: p.id == sh.shareable_id and sh.shareable_type == "Post", left_join: c in assoc(p, :comments), left_join: catt in assoc(c, :comment_attachments), left_join: lkc in assoc(c, :likes), on: lkc.likeable_id == c.id and lkc.likeable_type == "Comment", left_join: sc in assoc(c, :subcomments), left_join: scu in assoc(sc, :user), left_join: scu_prof in assoc(scu, :profile), left_join: lksc in assoc(sc, :likes), on: lksc.likeable_id == sc.id and lksc.likeable_type == "Comment", preload: [ resources: r, post_attachments: patt, shares: sh, author: {a, profile: prof}, likes: {lk, user: {lku, profile: lku_prof}}, comments: {c, likes: lkc, comment_attachments: catt, subcomments: {sc, likes: lksc, user: {scu, profile: scu_prof}}} ] ) end def featured_post_query(%Lesson{} = lesson) do from( p in __MODULE__, where: p.lesson_id == ^lesson.id, where: p.is_featured == true, join: a in assoc(p, :author), left_join: prof in assoc(a, :profile), left_join: patt in assoc(p, :post_attachments), left_join: lk in assoc(p, :likes), on: lk.likeable_id == p.id and lk.likeable_type == "Post", left_join: lku in assoc(lk, :user), left_join: lku_prof in assoc(lku, :profile), left_join: r in assoc(p, :resources), on: r.resourceable_id == p.id and r.resourceable_type == "Post", left_join: sh in assoc(p, :shares), on: p.id == sh.shareable_id and sh.shareable_type == "Post", left_join: c in assoc(p, :comments), left_join: catt in assoc(c, :comment_attachments), left_join: lkc in assoc(c, :likes), on: lkc.likeable_id == c.id and lkc.likeable_type == "Comment", left_join: sc in assoc(c, :subcomments), left_join: scu in assoc(sc, :user), left_join: scu_prof in assoc(scu, :profile), left_join: lksc in assoc(sc, :likes), on: lksc.likeable_id == sc.id and lksc.likeable_type == "Comment", preload: [ resources: r, post_attachments: patt, shares: sh, author: {a, profile: prof}, likes: {lk, user: {lku, profile: lku_prof}}, comments: {c, likes: lkc, comment_attachments: catt, subcomments: {sc, likes: lksc, user: {scu, profile: scu_prof}}} ] ) end def changeset(post, params \\ %{}) do post |> cast(params, [ :created_at, :updated_at, :body, :community_id, :lesson_id, :course_period_id, :author_id, :title, :is_inappropriate, :is_featured, :thumbnail_url, :url ]) |> validate_required([:author_id]) end end