Generate schema and live_view scaffold for records

1. Records are not yet tied to artists
2. The generated edit route, along with related functionality, may be
   deleted as I don't think it's gonna be needed. Leaving it included in
   the commit for reference.
This commit is contained in:
Claudio Ortolina
2024-09-12 14:42:15 +01:00
parent 34111a44ba
commit e08fbe3740
12 changed files with 603 additions and 0 deletions
+104
View File
@@ -0,0 +1,104 @@
defmodule MusicLibrary.Records do
@moduledoc """
The Records context.
"""
import Ecto.Query, warn: false
alias MusicLibrary.Repo
alias MusicLibrary.Records.Record
@doc """
Returns the list of records.
## Examples
iex> list_records()
[%Record{}, ...]
"""
def list_records do
Repo.all(Record)
end
@doc """
Gets a single record.
Raises `Ecto.NoResultsError` if the Record does not exist.
## Examples
iex> get_record!(123)
%Record{}
iex> get_record!(456)
** (Ecto.NoResultsError)
"""
def get_record!(id), do: Repo.get!(Record, id)
@doc """
Creates a record.
## Examples
iex> create_record(%{field: value})
{:ok, %Record{}}
iex> create_record(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_record(attrs \\ %{}) do
%Record{}
|> Record.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a record.
## Examples
iex> update_record(record, %{field: new_value})
{:ok, %Record{}}
iex> update_record(record, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_record(%Record{} = record, attrs) do
record
|> Record.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a record.
## Examples
iex> delete_record(record)
{:ok, %Record{}}
iex> delete_record(record)
{:error, %Ecto.Changeset{}}
"""
def delete_record(%Record{} = record) do
Repo.delete(record)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking record changes.
## Examples
iex> change_record(record)
%Ecto.Changeset{data: %Record{}}
"""
def change_record(%Record{} = record, attrs \\ %{}) do
Record.changeset(record, attrs)
end
end
+24
View File
@@ -0,0 +1,24 @@
defmodule MusicLibrary.Records.Record do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "records" do
field :type, Ecto.Enum, values: [:album, :ep, :live, :compilation, :single, :other]
field :title, :string
field :image, :string
field :year, :integer
field :musicbrainz_id, Ecto.UUID
field :genres, {:array, :string}
timestamps(type: :utc_datetime)
end
@doc false
def changeset(record, attrs) do
record
|> cast(attrs, [:type, :title, :musicbrainz_id, :year, :genres, :image])
|> validate_required([:type, :title, :musicbrainz_id, :year, :genres, :image])
end
end