-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add base resources for podcast context
- Loading branch information
1 parent
613a0ab
commit 5fc3df2
Showing
9 changed files
with
624 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,293 @@ | ||
defmodule Radiator.Podcast do | ||
@moduledoc """ | ||
The Podcasts context. | ||
Handles repo operations for networks, shows and episodes. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias Radiator.Repo | ||
|
||
alias Radiator.Podcast.{Episode, Network, Show} | ||
|
||
@doc """ | ||
Returns the list of networks. | ||
## Examples | ||
iex> list_networks() | ||
[%Network{}, ...] | ||
""" | ||
def list_networks do | ||
Repo.all(Network) | ||
end | ||
|
||
@doc """ | ||
Gets a single network. | ||
Raises `Ecto.NoResultsError` if the Network does not exist. | ||
## Examples | ||
iex> get_network!(123) | ||
%Network{} | ||
iex> get_network!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_network!(id), do: Repo.get!(Network, id) | ||
|
||
@doc """ | ||
Creates a network. | ||
## Examples | ||
iex> create_network(%{field: value}) | ||
{:ok, %Network{}} | ||
iex> create_network(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_network(attrs \\ %{}) do | ||
%Network{} | ||
|> Network.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Updates a network. | ||
## Examples | ||
iex> update_network(network, %{field: new_value}) | ||
{:ok, %Network{}} | ||
iex> update_network(network, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def update_network(%Network{} = network, attrs) do | ||
network | ||
|> Network.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a network. | ||
## Examples | ||
iex> delete_network(network) | ||
{:ok, %Network{}} | ||
iex> delete_network(network) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def delete_network(%Network{} = network) do | ||
Repo.delete(network) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking network changes. | ||
## Examples | ||
iex> change_network(network) | ||
%Ecto.Changeset{data: %Network{}} | ||
""" | ||
def change_network(%Network{} = network, attrs \\ %{}) do | ||
Network.changeset(network, attrs) | ||
end | ||
|
||
@doc """ | ||
Returns the list of shows. | ||
## Examples | ||
iex> list_shows() | ||
[%Show{}, ...] | ||
""" | ||
def list_shows do | ||
Repo.all(Show) | ||
end | ||
|
||
@doc """ | ||
Gets a single show. | ||
Raises `Ecto.NoResultsError` if the Show does not exist. | ||
## Examples | ||
iex> get_show!(123) | ||
%Show{} | ||
iex> get_show!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_show!(id), do: Repo.get!(Show, id) | ||
|
||
@doc """ | ||
Creates a show. | ||
## Examples | ||
iex> create_show(%{field: value}) | ||
{:ok, %Show{}} | ||
iex> create_show(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_show(attrs \\ %{}) do | ||
%Show{} | ||
|> Show.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Updates a show. | ||
## Examples | ||
iex> update_show(show, %{field: new_value}) | ||
{:ok, %Show{}} | ||
iex> update_show(show, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def update_show(%Show{} = show, attrs) do | ||
show | ||
|> Show.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a show. | ||
## Examples | ||
iex> delete_show(show) | ||
{:ok, %Show{}} | ||
iex> delete_show(show) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def delete_show(%Show{} = show) do | ||
Repo.delete(show) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking show changes. | ||
## Examples | ||
iex> change_show(show) | ||
%Ecto.Changeset{data: %Show{}} | ||
""" | ||
def change_show(%Show{} = show, attrs \\ %{}) do | ||
Show.changeset(show, attrs) | ||
end | ||
|
||
@doc """ | ||
Returns the list of episodes. | ||
## Examples | ||
iex> list_episodes() | ||
[%Episode{}, ...] | ||
""" | ||
def list_episodes 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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Radiator.Podcast.Episode do | ||
@moduledoc """ | ||
Represents the Episode model. | ||
TODO: Episodes should be numbered and ordered inside a show. | ||
""" | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
alias Radiator.Podcast.Show | ||
|
||
schema "episodes" do | ||
field :title, :string | ||
belongs_to :show, Show | ||
timestamps(type: :utc_datetime) | ||
end | ||
|
||
@doc false | ||
def changeset(episode, attrs) do | ||
episode | ||
|> cast(attrs, [:title, :show_id]) | ||
|> validate_required([:title, :show_id]) | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule Radiator.Podcast.Network do | ||
@moduledoc """ | ||
Represents the network model. | ||
A network can host many shows. | ||
""" | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
alias Radiator.Podcast.Show | ||
|
||
schema "networks" do | ||
field :title, :string | ||
|
||
has_many(:shows, Show) | ||
timestamps(type: :utc_datetime) | ||
end | ||
|
||
@doc false | ||
def changeset(network, attrs) do | ||
network | ||
|> cast(attrs, [:title]) | ||
|> validate_required([:title]) | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Radiator.Podcast.Show do | ||
@moduledoc """ | ||
Represents the show model. | ||
A show can have many episodes. | ||
""" | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
alias Radiator.Podcast.{Episode, Network} | ||
|
||
schema "shows" do | ||
field :title, :string | ||
belongs_to :network, Network | ||
has_many(:episodes, Episode) | ||
timestamps(type: :utc_datetime) | ||
end | ||
|
||
@doc false | ||
def changeset(show, attrs) do | ||
show | ||
|> cast(attrs, [:title, :network_id]) | ||
|> validate_required([:title]) | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Radiator.Repo.Migrations.CreateNetworks do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:networks) do | ||
add :title, :string | ||
|
||
timestamps(type: :utc_datetime) | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
defmodule Radiator.Repo.Migrations.CreateShows do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:shows) do | ||
add :title, :string | ||
add :network_id, references(:networks, on_delete: :nothing) | ||
|
||
timestamps(type: :utc_datetime) | ||
end | ||
|
||
create index(:shows, [:network_id]) | ||
end | ||
end |
Oops, something went wrong.