Skip to content

Commit

Permalink
feat: add episode "played" action
Browse files Browse the repository at this point in the history
  • Loading branch information
trueChazza authored Apr 19, 2022
1 parent c2fc4cb commit 470861e
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 68 deletions.
96 changes: 96 additions & 0 deletions lib/media_server/actions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,100 @@ defmodule MediaServer.Actions do
def change_movie(%Movie{} = movie, attrs \\ %{}) do
Movie.changeset(movie, attrs)
end

alias MediaServer.Actions.Episode

@doc """
Returns the list of episode_actions.
## Examples
iex> list_episode_actions()
[%Episode{}, ...]
"""
def list_episode_actions do
Repo.all(Episode)
end

@doc """
Gets a single episode.
Raises `Ecto.NoResultsError` if the Episode does not exist.
## Examples
iex> get_episode!(123)
%Episode{}
iex> get_episode!(456)
** (Ecto.NoResultsError)
"""
def get_episode!(id), do: Repo.get!(Episode, id)

@doc """
Creates a episode.
## Examples
iex> create_episode(%{field: value})
{:ok, %Episode{}}
iex> create_episode(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_episode(attrs \\ %{}) do
%Episode{}
|> Episode.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a episode.
## Examples
iex> update_episode(episode, %{field: new_value})
{:ok, %Episode{}}
iex> update_episode(episode, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_episode(%Episode{} = episode, attrs) do
episode
|> Episode.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a episode.
## Examples
iex> delete_episode(episode)
{:ok, %Episode{}}
iex> delete_episode(episode)
{:error, %Ecto.Changeset{}}
"""
def delete_episode(%Episode{} = episode) do
Repo.delete(episode)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking episode changes.
## Examples
iex> change_episode(episode)
%Ecto.Changeset{data: %Episode{}}
"""
def change_episode(%Episode{} = episode, attrs \\ %{}) do
Episode.changeset(episode, attrs)
end
end
21 changes: 21 additions & 0 deletions lib/media_server/actions/episode.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule MediaServer.Actions.Episode do
use Ecto.Schema
import Ecto.Changeset

schema "episode_actions" do
field :episode_id, :integer
field :serie_id, :integer
field :title, :string
field :user_id, :id
field :action_id, :id

timestamps()
end

@doc false
def changeset(episode, attrs) do
episode
|> cast(attrs, [:episode_id, :serie_id, :title, :user_id, :action_id])
|> validate_required([:episode_id, :serie_id, :title, :user_id, :action_id])
end
end
16 changes: 16 additions & 0 deletions lib/media_server_web/live/watch_episode_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule MediaServerWeb.WatchEpisodeLive.Show do
alias MediaServer.Accounts
alias MediaServerWeb.Repositories.Episodes
alias MediaServer.Continues
alias MediaServer.Components
alias MediaServer.Actions

@impl true
def mount(_params, session, socket) do
Expand All @@ -18,6 +20,8 @@ defmodule MediaServerWeb.WatchEpisodeLive.Show do
def handle_params(%{"episode" => episode_id}, _url, socket) do
episode = Episodes.get_episode(episode_id)

create_action(episode, socket)

{
:noreply,
socket
Expand Down Expand Up @@ -52,4 +56,16 @@ defmodule MediaServerWeb.WatchEpisodeLive.Show do

{:noreply, socket}
end

defp create_action(episode, socket) do
action = Components.list_actions() |> List.first()

Actions.create_episode(%{
episode_id: episode["id"],
serie_id: episode["seriesId"],
title: episode["title"],
user_id: socket.assigns.current_user.id,
action_id: action.id
})
end
end
18 changes: 18 additions & 0 deletions priv/repo/migrations/20220419024142_create_episode_actions.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule MediaServer.Repo.Migrations.CreateEpisodeActions do
use Ecto.Migration

def change do
create table(:episode_actions) do
add :episode_id, :integer
add :serie_id, :integer
add :title, :string
add :user_id, references(:users, on_delete: :nothing)
add :action_id, references(:actions, on_delete: :nothing)

timestamps()
end

create index(:episode_actions, [:user_id])
create index(:episode_actions, [:action_id])
end
end
Loading

0 comments on commit 470861e

Please sign in to comment.