initial book review schema

This commit is contained in:
Daniel Yrovas 2025-01-09 21:43:05 +11:00
parent e218290e08
commit 90414e6b93
Signed by: danielyrovas
SSH key fingerprint: SHA256:1avlGZQpGW038lBkNI5lOS7f0hIPM7ecJen2/P1MCCU
15 changed files with 653 additions and 0 deletions

104
lib/wild/book_reads.ex Normal file
View file

@ -0,0 +1,104 @@
defmodule Wild.BookReads do
@moduledoc """
The BookReads context.
"""
import Ecto.Query, warn: false
alias Wild.Repo
alias Wild.BookReads.BookRead
@doc """
Returns the list of book_reads.
## Examples
iex> list_book_reads()
[%BookRead{}, ...]
"""
def list_book_reads do
Repo.all(BookRead)
end
@doc """
Gets a single book_read.
Raises `Ecto.NoResultsError` if the Book read does not exist.
## Examples
iex> get_book_read!(123)
%BookRead{}
iex> get_book_read!(456)
** (Ecto.NoResultsError)
"""
def get_book_read!(id), do: Repo.get!(BookRead, id)
@doc """
Creates a book_read.
## Examples
iex> create_book_read(%{field: value})
{:ok, %BookRead{}}
iex> create_book_read(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_book_read(attrs \\ %{}) do
%BookRead{}
|> BookRead.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a book_read.
## Examples
iex> update_book_read(book_read, %{field: new_value})
{:ok, %BookRead{}}
iex> update_book_read(book_read, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_book_read(%BookRead{} = book_read, attrs) do
book_read
|> BookRead.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a book_read.
## Examples
iex> delete_book_read(book_read)
{:ok, %BookRead{}}
iex> delete_book_read(book_read)
{:error, %Ecto.Changeset{}}
"""
def delete_book_read(%BookRead{} = book_read) do
Repo.delete(book_read)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking book_read changes.
## Examples
iex> change_book_read(book_read)
%Ecto.Changeset{data: %BookRead{}}
"""
def change_book_read(%BookRead{} = book_read, attrs \\ %{}) do
BookRead.changeset(book_read, attrs)
end
end

View file

@ -0,0 +1,24 @@
defmodule Wild.BookReads.BookRead do
use Ecto.Schema
import Ecto.Changeset
@primary_key false
schema "book_reads" do
field :progress, :integer
field :rating, :integer
field :thoughts, :string
field :read_start, :utc_datetime
field :read_finish, :utc_datetime
field :user_id, :id, primary_key: true
field :book_id, :id, primary_key: true
timestamps(type: :utc_datetime)
end
@doc false
def changeset(book_read, attrs) do
book_read
|> cast(attrs, [:rating, :thoughts, :read_start, :read_finish, :progress])
|> validate_required([:rating, :thoughts, :read_start, :read_finish, :progress])
end
end

21
lib/wild/books/author.ex Normal file
View file

@ -0,0 +1,21 @@
defmodule Wild.Books.Author do
use Ecto.Schema
import Ecto.Changeset
schema "authors" do
field :name, :string
field :country, :string
field :bio, :string
field :year_born, :string
field :year_died, :string
timestamps(type: :utc_datetime)
end
@doc false
def changeset(author, attrs) do
author
|> cast(attrs, [:name, :country, :bio, :year_born, :year_died])
|> validate_required([:name, :country, :bio, :year_born, :year_died])
end
end

20
lib/wild/books/book.ex Normal file
View file

@ -0,0 +1,20 @@
defmodule Wild.Books.Book do
use Ecto.Schema
import Ecto.Changeset
schema "books" do
field :title, :string
field :blurb, :string
field :publication_year, :string
field :author_id, :id
timestamps(type: :utc_datetime)
end
@doc false
def changeset(book, attrs) do
book
|> cast(attrs, [:title, :blurb, :publication_year])
|> validate_required([:title, :blurb, :publication_year])
end
end

View file

@ -0,0 +1,86 @@
defmodule WildWeb.BookReadLive.FormComponent do
use WildWeb, :live_component
alias Wild.BookReads
@impl true
def render(assigns) do
~H"""
<div>
<.header>
{@title}
<:subtitle>Use this form to manage book_read records in your database.</:subtitle>
</.header>
<.simple_form
for={@form}
id="book_read-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<.input field={@form[:rating]} type="number" label="Rating" />
<.input field={@form[:thoughts]} type="text" label="Thoughts" />
<.input field={@form[:read_start]} type="datetime-local" label="Read start" />
<.input field={@form[:read_finish]} type="datetime-local" label="Read finish" />
<.input field={@form[:progress]} type="number" label="Progress" />
<:actions>
<.button phx-disable-with="Saving...">Save Book read</.button>
</:actions>
</.simple_form>
</div>
"""
end
@impl true
def update(%{book_read: book_read} = assigns, socket) do
{:ok,
socket
|> assign(assigns)
|> assign_new(:form, fn ->
to_form(BookReads.change_book_read(book_read))
end)}
end
@impl true
def handle_event("validate", %{"book_read" => book_read_params}, socket) do
changeset = BookReads.change_book_read(socket.assigns.book_read, book_read_params)
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
end
def handle_event("save", %{"book_read" => book_read_params}, socket) do
save_book_read(socket, socket.assigns.action, book_read_params)
end
defp save_book_read(socket, :edit, book_read_params) do
case BookReads.update_book_read(socket.assigns.book_read, book_read_params) do
{:ok, book_read} ->
notify_parent({:saved, book_read})
{:noreply,
socket
|> put_flash(:info, "Book read updated successfully")
|> push_patch(to: socket.assigns.patch)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
end
end
defp save_book_read(socket, :new, book_read_params) do
case BookReads.create_book_read(book_read_params) do
{:ok, book_read} ->
notify_parent({:saved, book_read})
{:noreply,
socket
|> put_flash(:info, "Book read 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

View file

@ -0,0 +1,47 @@
defmodule WildWeb.BookReadLive.Index do
use WildWeb, :live_view
alias Wild.BookReads
alias Wild.BookReads.BookRead
@impl true
def mount(_params, _session, socket) do
{:ok, stream(socket, :book_reads, BookReads.list_book_reads())}
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 Book read")
|> assign(:book_read, BookReads.get_book_read!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Book read")
|> assign(:book_read, %BookRead{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Book reads")
|> assign(:book_read, nil)
end
@impl true
def handle_info({WildWeb.BookReadLive.FormComponent, {:saved, book_read}}, socket) do
{:noreply, stream_insert(socket, :book_reads, book_read)}
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
book_read = BookReads.get_book_read!(id)
{:ok, _} = BookReads.delete_book_read(book_read)
{:noreply, stream_delete(socket, :book_reads, book_read)}
end
end

View file

@ -0,0 +1,45 @@
<.header>
Listing Book reads
<:actions>
<.link patch={~p"/book_reads/new"}>
<.button>New Book read</.button>
</.link>
</:actions>
</.header>
<.table
id="book_reads"
rows={@streams.book_reads}
row_click={fn {_id, book_read} -> JS.navigate(~p"/book_reads/#{book_read}") end}
>
<:col :let={{_id, book_read}} label="Rating">{book_read.rating}</:col>
<:col :let={{_id, book_read}} label="Thoughts">{book_read.thoughts}</:col>
<:col :let={{_id, book_read}} label="Read start">{book_read.read_start}</:col>
<:col :let={{_id, book_read}} label="Read finish">{book_read.read_finish}</:col>
<:col :let={{_id, book_read}} label="Progress">{book_read.progress}</:col>
<:action :let={{_id, book_read}}>
<div class="sr-only">
<.link navigate={~p"/book_reads/#{book_read}"}>Show</.link>
</div>
<.link patch={~p"/book_reads/#{book_read}/edit"}>Edit</.link>
</:action>
<:action :let={{id, book_read}}>
<.link
phx-click={JS.push("delete", value: %{id: book_read.id}) |> hide("##{id}")}
data-confirm="Are you sure?"
>
Delete
</.link>
</:action>
</.table>
<.modal :if={@live_action in [:new, :edit]} id="book_read-modal" show on_cancel={JS.patch(~p"/book_reads")}>
<.live_component
module={WildWeb.BookReadLive.FormComponent}
id={@book_read.id || :new}
title={@page_title}
action={@live_action}
book_read={@book_read}
patch={~p"/book_reads"}
/>
</.modal>

View file

@ -0,0 +1,21 @@
defmodule WildWeb.BookReadLive.Show do
use WildWeb, :live_view
alias Wild.BookReads
@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(:book_read, BookReads.get_book_read!(id))}
end
defp page_title(:show), do: "Show Book read"
defp page_title(:edit), do: "Edit Book read"
end

View file

@ -0,0 +1,30 @@
<.header>
Book read {@book_read.id}
<:subtitle>This is a book_read record from your database.</:subtitle>
<:actions>
<.link patch={~p"/book_reads/#{@book_read}/show/edit"} phx-click={JS.push_focus()}>
<.button>Edit book_read</.button>
</.link>
</:actions>
</.header>
<.list>
<:item title="Rating">{@book_read.rating}</:item>
<:item title="Thoughts">{@book_read.thoughts}</:item>
<:item title="Read start">{@book_read.read_start}</:item>
<:item title="Read finish">{@book_read.read_finish}</:item>
<:item title="Progress">{@book_read.progress}</:item>
</.list>
<.back navigate={~p"/book_reads"}>Back to book_reads</.back>
<.modal :if={@live_action == :edit} id="book_read-modal" show on_cancel={JS.patch(~p"/book_reads/#{@book_read}")}>
<.live_component
module={WildWeb.BookReadLive.FormComponent}
id={@book_read.id}
title={@page_title}
action={@live_action}
book_read={@book_read}
patch={~p"/book_reads/#{@book_read}"}
/>
</.modal>