Current section

Files

Jump to
ami lib ami video.ex
Raw

lib/ami/video.ex

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
@timestamps_opts [type: :naive_datetime_usec]
field(:name, :string)
field(:video, :string)
field(:videoable_id, :integer)
field(:videoable_type, :string)
field(:created_at, :utc_datetime_usec)
field(:updated_at, :utc_datetime_usec)
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
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