Skip to content

Commit

Permalink
feat: add create / update threshold for episode watch statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
trueChazza committed Feb 26, 2022
1 parent e22c718 commit 815cdf3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
17 changes: 15 additions & 2 deletions lib/media_server/watches.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ defmodule MediaServer.Watches do
end

_ ->

if ((attrs.current_time / attrs.duration) * 100) < 90 do
update_movie(movie, attrs)
else
Expand Down Expand Up @@ -197,10 +198,22 @@ defmodule MediaServer.Watches do

case episode do
nil ->
create_episode(attrs)

if ((attrs.current_time / attrs.duration) * 100) < 90 do
create_episode(attrs)
else
nil
end

_ ->
update_episode(episode, attrs)

if ((attrs.current_time / attrs.duration) * 100) < 90 do
update_episode(episode, attrs)
else
delete_episode(episode)

nil
end
end
end

Expand Down
20 changes: 18 additions & 2 deletions test/media_server/watches_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,33 @@ defmodule MediaServer.WatchesTest do
update_attrs = %{
episode_id: 42,
serie_id: 42,
current_time: 62,
current_time: 89,
duration: 100,
user_id: user.id
}

{:ok, %Episode{} = episode} = Watches.update_or_create_episode(update_attrs)
assert episode.current_time == 62
assert episode.duration == 100
assert episode.current_time == 89
assert episode.episode_id == 42
assert episode.serie_id == 42
assert episode.user_id == user.id
end

test "update_or_create_movie/2 deletes movie" do
user = AccountsFixtures.user_fixture()
episode_fixture(%{user_id: user.id})
update_attrs = %{
episode_id: 42,
serie_id: 42,
current_time: 90,
duration: 100,
user_id: user.id
}

refute Watches.update_or_create_episode(update_attrs)
end

test "update_or_create_episode/2 creates episode" do
user = AccountsFixtures.user_fixture()
episode_fixture(%{user_id: user.id})
Expand Down
26 changes: 25 additions & 1 deletion test/media_server_web/live/watch_episode_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule MediaServerWeb.WatchEpisodeLiveTest do
assert html_response(conn, 200)
end

test "it has watch status", %{conn: conn, user: user} do
test "has watch status", %{conn: conn, user: user} do

conn =
post(conn, Routes.user_session_path(conn, :create), %{
Expand All @@ -53,5 +53,29 @@ defmodule MediaServerWeb.WatchEpisodeLiveTest do

assert WatchesFixtures.get_episode_watch()
end

test "no watch status", %{conn: conn, user: user} do

conn =
post(conn, Routes.user_session_path(conn, :create), %{
"user" => %{"email" => user.email, "password" => AccountsFixtures.valid_user_password()}
})

serie = SeriesFixtures.get_serie()

episode = EpisodesFixtures.get_episode(serie["id"])

{:ok, view, _html} = live(conn, Routes.watch_episode_show_path(conn, :show, episode["id"]))

render_hook(view, :episode_destroyed, %{
episode_id: episode["id"],
serie_id: episode["seriesId"],
current_time: 90,
duration: 100,
user_id: user.id
})

refute WatchesFixtures.get_episode_watch()
end
end
end

0 comments on commit 815cdf3

Please sign in to comment.