Skip to content

Instantly share code, notes, and snippets.

@sobolevn
Last active April 30, 2017 11:39
Show Gist options
  • Save sobolevn/9bc02819fbb796ad0e1bb6a6696f37df to your computer and use it in GitHub Desktop.
Save sobolevn/9bc02819fbb796ad0e1bb6a6696f37df to your computer and use it in GitHub Desktop.
defmodule MediumPhxExample.Audio do
@moduledoc """
The boundary for the Audio system.
"""
import Ecto.{Query, Changeset}, warn: false
alias MediumPhxExample.Repo
alias MediumPhxExample.Audio.Album
def list_albums do
Repo.all(Album)
end
def get_album!(id), do: Repo.get!(Album, id)
def create_album(attrs \\ %{}) do
%Album{}
|> album_changeset(attrs)
|> Repo.insert()
end
# ...
defp album_changeset(%Album{} = album, attrs) do
album
|> cast(attrs, [:name, :release])
|> validate_required([:name, :release])
end
alias MediumPhxExample.Audio.Song
def list_songs do
Repo.all(Song)
end
def get_song!(id), do: Repo.get!(Song, id)
def create_song(attrs \\ %{}) do
%Song{}
|> song_changeset(attrs)
|> Repo.insert()
end
# ...
defp song_changeset(%Song{} = song, attrs) do
song
|> cast(attrs, [:name, :duration])
|> validate_required([:name, :duration])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment