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:
@@ -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
|
||||
@@ -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
|
||||
@@ -0,0 +1,99 @@
|
||||
defmodule MusicLibraryWeb.RecordLive.FormComponent do
|
||||
use MusicLibraryWeb, :live_component
|
||||
|
||||
alias MusicLibrary.Records
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div>
|
||||
<.header>
|
||||
<%= @title %>
|
||||
<:subtitle>Use this form to manage record records in your database.</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.simple_form
|
||||
for={@form}
|
||||
id="record-form"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save"
|
||||
>
|
||||
<.input
|
||||
field={@form[:type]}
|
||||
type="select"
|
||||
label="Type"
|
||||
prompt="Choose a value"
|
||||
options={Ecto.Enum.values(MusicLibrary.Records.Record, :type)}
|
||||
/>
|
||||
<.input field={@form[:title]} type="text" label="Title" />
|
||||
<.input field={@form[:musicbrainz_id]} type="text" label="Musicbrainz" />
|
||||
<.input field={@form[:year]} type="number" label="Year" />
|
||||
<.input
|
||||
field={@form[:genres]}
|
||||
type="select"
|
||||
multiple
|
||||
label="Genres"
|
||||
options={[{"Option 1", "option1"}, {"Option 2", "option2"}]}
|
||||
/>
|
||||
<.input field={@form[:image]} type="text" label="Image" />
|
||||
<:actions>
|
||||
<.button phx-disable-with="Saving...">Save Record</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def update(%{record: record} = assigns, socket) do
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign_new(:form, fn ->
|
||||
to_form(Records.change_record(record))
|
||||
end)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"record" => record_params}, socket) do
|
||||
changeset = Records.change_record(socket.assigns.record, record_params)
|
||||
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"record" => record_params}, socket) do
|
||||
save_record(socket, socket.assigns.action, record_params)
|
||||
end
|
||||
|
||||
defp save_record(socket, :edit, record_params) do
|
||||
case Records.update_record(socket.assigns.record, record_params) do
|
||||
{:ok, record} ->
|
||||
notify_parent({:saved, record})
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Record updated successfully")
|
||||
|> push_patch(to: socket.assigns.patch)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, form: to_form(changeset))}
|
||||
end
|
||||
end
|
||||
|
||||
defp save_record(socket, :new, record_params) do
|
||||
case Records.create_record(record_params) do
|
||||
{:ok, record} ->
|
||||
notify_parent({:saved, record})
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Record created successfully")
|
||||
|> push_patch(to: socket.assigns.patch)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, form: to_form(changeset))}
|
||||
end
|
||||
end
|
||||
|
||||
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
|
||||
end
|
||||
@@ -0,0 +1,47 @@
|
||||
defmodule MusicLibraryWeb.RecordLive.Index do
|
||||
use MusicLibraryWeb, :live_view
|
||||
|
||||
alias MusicLibrary.Records
|
||||
alias MusicLibrary.Records.Record
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, stream(socket, :records, Records.list_records())}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(params, _url, socket) do
|
||||
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
||||
end
|
||||
|
||||
defp apply_action(socket, :edit, %{"id" => id}) do
|
||||
socket
|
||||
|> assign(:page_title, "Edit Record")
|
||||
|> assign(:record, Records.get_record!(id))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "New Record")
|
||||
|> assign(:record, %Record{})
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "Listing Records")
|
||||
|> assign(:record, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info({MusicLibraryWeb.RecordLive.FormComponent, {:saved, record}}, socket) do
|
||||
{:noreply, stream_insert(socket, :records, record)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
record = Records.get_record!(id)
|
||||
{:ok, _} = Records.delete_record(record)
|
||||
|
||||
{:noreply, stream_delete(socket, :records, record)}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,46 @@
|
||||
<.header>
|
||||
Listing Records
|
||||
<:actions>
|
||||
<.link patch={~p"/records/new"}>
|
||||
<.button>New Record</.button>
|
||||
</.link>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.table
|
||||
id="records"
|
||||
rows={@streams.records}
|
||||
row_click={fn {_id, record} -> JS.navigate(~p"/records/#{record}") end}
|
||||
>
|
||||
<:col :let={{_id, record}} label="Type"><%= record.type %></:col>
|
||||
<:col :let={{_id, record}} label="Title"><%= record.title %></:col>
|
||||
<:col :let={{_id, record}} label="Musicbrainz"><%= record.musicbrainz_id %></:col>
|
||||
<:col :let={{_id, record}} label="Year"><%= record.year %></:col>
|
||||
<:col :let={{_id, record}} label="Genres"><%= record.genres %></:col>
|
||||
<:col :let={{_id, record}} label="Image"><%= record.image %></:col>
|
||||
<:action :let={{_id, record}}>
|
||||
<div class="sr-only">
|
||||
<.link navigate={~p"/records/#{record}"}>Show</.link>
|
||||
</div>
|
||||
<.link patch={~p"/records/#{record}/edit"}>Edit</.link>
|
||||
</:action>
|
||||
<:action :let={{id, record}}>
|
||||
<.link
|
||||
phx-click={JS.push("delete", value: %{id: record.id}) |> hide("##{id}")}
|
||||
data-confirm="Are you sure?"
|
||||
>
|
||||
Delete
|
||||
</.link>
|
||||
</:action>
|
||||
</.table>
|
||||
|
||||
<.modal :if={@live_action in [:new, :edit]} id="record-modal" show on_cancel={JS.patch(~p"/records")}>
|
||||
<.live_component
|
||||
module={MusicLibraryWeb.RecordLive.FormComponent}
|
||||
id={@record.id || :new}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
record={@record}
|
||||
patch={~p"/records"}
|
||||
/>
|
||||
</.modal>
|
||||
@@ -0,0 +1,21 @@
|
||||
defmodule MusicLibraryWeb.RecordLive.Show do
|
||||
use MusicLibraryWeb, :live_view
|
||||
|
||||
alias MusicLibrary.Records
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _, socket) do
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:page_title, page_title(socket.assigns.live_action))
|
||||
|> assign(:record, Records.get_record!(id))}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: "Show Record"
|
||||
defp page_title(:edit), do: "Edit Record"
|
||||
end
|
||||
@@ -0,0 +1,31 @@
|
||||
<.header>
|
||||
Record <%= @record.id %>
|
||||
<:subtitle>This is a record record from your database.</:subtitle>
|
||||
<:actions>
|
||||
<.link patch={~p"/records/#{@record}/show/edit"} phx-click={JS.push_focus()}>
|
||||
<.button>Edit record</.button>
|
||||
</.link>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.list>
|
||||
<:item title="Type"><%= @record.type %></:item>
|
||||
<:item title="Title"><%= @record.title %></:item>
|
||||
<:item title="Musicbrainz"><%= @record.musicbrainz_id %></:item>
|
||||
<:item title="Year"><%= @record.year %></:item>
|
||||
<:item title="Genres"><%= @record.genres %></:item>
|
||||
<:item title="Image"><%= @record.image %></:item>
|
||||
</.list>
|
||||
|
||||
<.back navigate={~p"/records"}>Back to records</.back>
|
||||
|
||||
<.modal :if={@live_action == :edit} id="record-modal" show on_cancel={JS.patch(~p"/records/#{@record}")}>
|
||||
<.live_component
|
||||
module={MusicLibraryWeb.RecordLive.FormComponent}
|
||||
id={@record.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
record={@record}
|
||||
patch={~p"/records/#{@record}"}
|
||||
/>
|
||||
</.modal>
|
||||
@@ -18,6 +18,13 @@ defmodule MusicLibraryWeb.Router do
|
||||
pipe_through :browser
|
||||
|
||||
get "/", PageController, :home
|
||||
|
||||
live "/records", RecordLive.Index, :index
|
||||
live "/records/new", RecordLive.Index, :new
|
||||
live "/records/:id/edit", RecordLive.Index, :edit
|
||||
|
||||
live "/records/:id", RecordLive.Show, :show
|
||||
live "/records/:id/show/edit", RecordLive.Show, :edit
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
|
||||
Reference in New Issue
Block a user