defmodule Ami.Video do @moduledoc """ Video is a sharable module that can be used in multiple umbrella apps. """ alias Ami.{ Repo } import Ecto.Query use Ecto.Schema schema "videos" do field(:name, :string) field(:video, :string) field(:videoable_id, :integer) field(:videoable_type, :string) field(:created_at, :utc_datetime) field(:updated_at, :utc_datetime) end def preload_videos(struct, videoable_type) do struct |> Repo.preload( videos: from( v in __MODULE__, where: v.videoable_type == ^videoable_type and v.videoable_id == ^struct.id, limit: 1 ) ) end def preload_video(struct, videoable_type) do struct |> Repo.preload( video: from( v in __MODULE__, where: v.videoable_type == ^videoable_type and v.videoable_id == ^struct.id ) ) end def video_url_for(video_struct) when not is_nil(video_struct) do "https://ami-aws-s3.s3.amazonaws.com/uploads/video/video/#{video_struct.id}/#{ video_struct.video }" end def video_url_for(video_struct) when is_nil(video_struct) do "" end def fetch(struct, videoable_type) do # [video] = # Repo.all( # from( # v in __MODULE__, # where: v.videoable_type == ^videoable_type and v.videoable_id == ^struct.id, # limit: 1 # ) # ) # video case Repo.all( from( v in __MODULE__, where: v.videoable_type == ^videoable_type and v.videoable_id == ^struct.id, limit: 1 ) ) do [%__MODULE__{} = video] -> video _ -> nil end end end