A last.fm clone written in Elixir
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

126 lines
2.1 KiB

defmodule Dagon.Listens.Albums do
@moduledoc """
The Listens.Albums context.
"""
import Ecto.Query, warn: false
alias Dagon.Repo
alias Dagon.Listens.Albums.Album
@doc """
Returns the list of albums.
## Examples
iex> list_albums()
[%Album{}, ...]
"""
def list_albums do
Album
|> order_by([:image, :name])
|> Repo.all()
|> Repo.preload(:artist)
end
def list_albums_without_cover do
Album
|> where([a], is_nil(a.image))
|> where([a], is_nil(a.discogs_id))
|> limit(10)
|> Repo.all()
|> Repo.preload(:artist)
end
def list_albums_with_cover do
Album
|> where([a], not is_nil(a.image))
|> Repo.all()
|> Repo.preload(:artist)
end
@doc """
Gets a single album.
Raises `Ecto.NoResultsError` if the Album does not exist.
## Examples
iex> get_album!(123)
%Album{}
iex> get_album!(456)
** (Ecto.NoResultsError)
"""
def get_album!(id),
do:
Repo.get!(Album, id)
|> Repo.preload([:artist])
@doc """
Creates a album.
## Examples
iex> create_album(%{field: value})
{:ok, %Album{}}
iex> create_album(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_album(attrs \\ %{}) do
%Album{}
|> Album.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a album.
## Examples
iex> update_album(album, %{field: new_value})
{:ok, %Album{}}
iex> update_album(album, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_album(%Album{} = album, attrs) do
album
|> Album.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Album.
## Examples
iex> delete_album(album)
{:ok, %Album{}}
iex> delete_album(album)
{:error, %Ecto.Changeset{}}
"""
def delete_album(%Album{} = album) do
Repo.delete(album)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking album changes.
## Examples
iex> change_album(album)
%Ecto.Changeset{source: %Album{}}
"""
def change_album(%Album{} = album) do
Album.changeset(album, %{})
end
end