Skip to content

Instantly share code, notes, and snippets.

@emerleite
Last active February 2, 2018 10:01
Show Gist options
  • Save emerleite/731647781b384d03ac375dbda7d51061 to your computer and use it in GitHub Desktop.
Save emerleite/731647781b384d03ac375dbda7d51061 to your computer and use it in GitHub Desktop.
defmodule Upa.VideoWatchProgress do
def save(attrs) do
struct(Upa.VideoWatchProgress, attrs)
|> changeset
|> Upa.Repo.insert(on_conflict: insert_conflict_strategy(attrs))
|> case do
{:error, _} = error -> raise Upa.DatabaseCommandError
{:ok, changeset} -> :ok
end
end
def insert_conflict_strategy(%{fully_watched: fully_watched}) do
from(v in Upa.VideoWatchProgress,
update: [set: [
milliseconds_watched: fragment("IF(last_event_timestamp < VALUES(last_event_timestamp), VALUES(milliseconds_watched), milliseconds_watched)"),
last_event_timestamp: fragment("IF(last_event_timestamp < VALUES(last_event_timestamp), VALUES(last_event_timestamp), last_event_timestamp)"),
updated_at: ^ecto_time(),
fully_watched: ^fully_watched
]
]
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment