defmodule Ami.TeaserImage do alias Ami.{ Repo } import Ecto.Query use Ecto.Schema schema "teaser_images" do field(:name, :string) field(:page_position, :integer) field(:teaseable_id, :integer) field(:teaseable_type, :string) field(:file, :string) end def preload_teaser_images(struct, teasable_type) do struct |> Repo.preload( teaser_images: from(ti in __MODULE__, where: ti.teaseable_type == ^teasable_type) ) end def preload_preview_images(struct, teasable_type) do struct |> Repo.preload( teaser_images: from( ti in __MODULE__, where: ti.teaseable_type == ^teasable_type, where: ti.page_position == 4 ) ) end def preload_main_teaser_images(struct, teasable_type) do struct |> Repo.preload( teaser_images: from( ti in __MODULE__, where: ti.teaseable_type == ^teasable_type, where: ti.page_position == 0 ) ) end def preload_detail_teaser_images(struct, teasable_type) do struct |> Repo.preload( teaser_images: from( ti in __MODULE__, where: ti.teaseable_type == ^teasable_type, where: ti.page_position == 1 ) ) end def preload_certificate_teaser_images(struct, teasable_type) do struct |> Repo.preload( teaser_images: from( ti in __MODULE__, where: ti.teaseable_type == ^teasable_type, where: ti.page_position == 3 ) ) end def preload_download_certificate_teaser_images(struct, teaseable_type) do struct |> Repo.preload( teaser_images: from( ti in __MODULE__, where: ti.teaseable_type == ^teaseable_type, where: ti.page_position == 5 ) ) end def teaser_image_from(list) do ti = List.last(list) if ti != nil, do: "https://ami-aws-s3.s3.amazonaws.com/uploads/teaser_image/file/#{ti.id}/#{ti.file}" end end