Last active
February 2, 2018 10:01
-
-
Save emerleite/731647781b384d03ac375dbda7d51061 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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