Skip to content

Commit

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

case movie do
nil ->
create_movie(attrs)

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

_ ->
update_movie(movie, attrs)
if ((attrs.current_time / attrs.duration) * 100) < 90 do
update_movie(movie, attrs)
else
delete_movie(movie)

nil
end
end
end

Expand Down
19 changes: 17 additions & 2 deletions test/media_server/watches_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,31 @@ defmodule MediaServer.WatchesTest do
movie_fixture(%{user_id: user.id})
update_attrs = %{
movie_id: 42,
current_time: 62,
current_time: 89,
duration: 100,
user_id: user.id
}

{:ok, %Movie{} = movie} = Watches.update_or_create_movie(update_attrs)
assert movie.movie_id == 42
assert movie.current_time == 62
assert movie.current_time == 89
assert movie.duration == 100
assert movie.user_id == user.id
end

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

refute Watches.update_or_create_movie(update_attrs)
end

test "update_or_create_movie/2 creates movie" do
user = AccountsFixtures.user_fixture()
movie_fixture(%{user_id: user.id})
Expand Down
27 changes: 24 additions & 3 deletions test/media_server_web/live/watch_movie_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule MediaServerWeb.WatchMovieLiveTest 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 @@ -40,12 +40,33 @@ defmodule MediaServerWeb.WatchMovieLiveTest do

render_hook(view, :movie_destroyed, %{
movie_id: movie["id"],
current_time: 39,
duration: 78,
current_time: 89,
duration: 100,
user_id: user.id
})

assert WatchesFixtures.get_movie_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()}
})

movie = MoviesFixtures.get_movie()

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

render_hook(view, :movie_destroyed, %{
movie_id: movie["id"],
current_time: 90,
duration: 100,
user_id: user.id
})

refute WatchesFixtures.get_movie_watch()
end
end
end

0 comments on commit e22c718

Please sign in to comment.