dings with user assoc

This commit is contained in:
Rüdiger Diedrich
2024-02-26 14:37:03 +01:00
parent c242bbc633
commit 8c46519aaf
13 changed files with 638 additions and 0 deletions

View File

@ -8,6 +8,8 @@ defmodule Dinge.Accounts.User do
field :hashed_password, :string, redact: true
field :confirmed_at, :naive_datetime
has_many :dings, Dinge.Ledger.Ding
timestamps(type: :utc_datetime)
end

111
lib/dinge/ledger.ex Normal file
View File

@ -0,0 +1,111 @@
defmodule Dinge.Ledger do
@moduledoc """
The Ledger context.
"""
import Ecto.Query, warn: false
alias Dinge.Repo
alias Dinge.Ledger.Ding
@doc """
Returns the list of dings.
## Examples
iex> list_dings()
[%Ding{}, ...]
"""
def list_dings do
Repo.all(Ding)
end
def list_dings_by_user(user) do
query =
from d in Ding,
where: d.user_id == ^user.id
Repo.all(query)
end
@doc """
Gets a single ding.
Raises `Ecto.NoResultsError` if the Ding does not exist.
## Examples
iex> get_ding!(123)
%Ding{}
iex> get_ding!(456)
** (Ecto.NoResultsError)
"""
def get_ding!(id), do: Repo.get!(Ding, id)
@doc """
Creates a ding.
## Examples
iex> create_ding(%{field: value})
{:ok, %Ding{}}
iex> create_ding(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_ding(attrs \\ %{}) do
%Ding{}
|> Ding.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a ding.
## Examples
iex> update_ding(ding, %{field: new_value})
{:ok, %Ding{}}
iex> update_ding(ding, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_ding(%Ding{} = ding, attrs) do
ding
|> Ding.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a ding.
## Examples
iex> delete_ding(ding)
{:ok, %Ding{}}
iex> delete_ding(ding)
{:error, %Ecto.Changeset{}}
"""
def delete_ding(%Ding{} = ding) do
Repo.delete(ding)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking ding changes.
## Examples
iex> change_ding(ding)
%Ecto.Changeset{data: %Ding{}}
"""
def change_ding(%Ding{} = ding, attrs \\ %{}) do
Ding.changeset(ding, attrs)
end
end

24
lib/dinge/ledger/ding.ex Normal file
View File

@ -0,0 +1,24 @@
defmodule Dinge.Ledger.Ding do
use Ecto.Schema
import Ecto.Changeset
schema "dings" do
field :count, :integer
field :status, Ecto.Enum, values: [:active, :inactive, :finished, :hidden, :deleted]
field :title, :string
field :target_count, :integer
field :ding_type, Ecto.Enum, values: [:countdown, :countup]
field :target_type, Ecto.Enum, values: [:default, :daily, :weekly, :monthly]
belongs_to :user, Dinge.Accounts.User
timestamps(type: :utc_datetime)
end
@doc false
def changeset(ding, attrs) do
ding
|> cast(attrs, [:title, :count, :target_count, :ding_type, :target_type, :status, :user_id])
|> cast_assoc(:user)
|> validate_required([:title, :count, :target_count, :ding_type, :target_type, :status, :user_id])
end
end

View File

@ -0,0 +1,115 @@
defmodule DingeWeb.DingLive.FormComponent do
use DingeWeb, :live_component
alias Dinge.Ledger
@impl true
def render(assigns) do
~H"""
<div>
<.header>
<%= @title %>
<:subtitle>Use this form to manage ding records in your database.</:subtitle>
</.header>
<.simple_form
for={@form}
id="ding-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<.input field={@form[:title]} type="text" label="Title" />
<.input field={@form[:count]} type="number" label="Count" value="0" />
<.input field={@form[:target_count]} type="number" label="Target count" value="10" />
<.input
field={@form[:ding_type]}
type="select"
label="Ding type"
prompt="Choose a value"
options={Ecto.Enum.values(Dinge.Ledger.Ding, :ding_type)}
/>
<.input
field={@form[:target_type]}
type="select"
label="Target type"
prompt="Choose a value"
options={Ecto.Enum.values(Dinge.Ledger.Ding, :target_type)}
/>
<.input
field={@form[:status]}
type="select"
label="Status"
prompt="Choose a value"
options={Ecto.Enum.values(Dinge.Ledger.Ding, :status)}
/>
<:actions>
<.button phx-disable-with="Saving...">Save Ding</.button>
</:actions>
</.simple_form>
</div>
"""
end
@impl true
def update(%{ding: ding} = assigns, socket) do
changeset = Ledger.change_ding(ding)
{:ok,
socket
|> assign(assigns)
|> assign_form(changeset)}
end
@impl true
def handle_event("validate", %{"ding" => ding_params}, socket) do
changeset =
socket.assigns.ding
|> Ledger.change_ding(ding_params)
|> Map.put(:action, :validate)
{:noreply, assign_form(socket, changeset)}
end
def handle_event("save", %{"ding" => ding_params}, socket) do
save_ding(socket, socket.assigns.action, ding_params)
end
defp save_ding(socket, :edit, ding_params) do
case Ledger.update_ding(socket.assigns.ding, ding_params) do
{:ok, ding} ->
notify_parent({:saved, ding})
{:noreply,
socket
|> put_flash(:info, "Ding updated successfully")
|> push_patch(to: socket.assigns.patch)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end
defp save_ding(socket, :new, ding_params) do
ding_params = Map.put(ding_params, "user_id", socket.assigns.user.id)
case Ledger.create_ding(ding_params) do
{:ok, ding} ->
notify_parent({:saved, ding})
{:noreply,
socket
|> put_flash(:info, "Ding created successfully")
|> push_patch(to: socket.assigns.patch)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :form, to_form(changeset))
end
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
end

View File

@ -0,0 +1,47 @@
defmodule DingeWeb.DingLive.Index do
use DingeWeb, :live_view
alias Dinge.Ledger
alias Dinge.Ledger.Ding
@impl true
def mount(_params, _session, socket) do
{:ok, stream(socket, :dings, Ledger.list_dings_by_user(socket.assigns.current_user))}
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 Ding")
|> assign(:ding, Ledger.get_ding!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Ding")
|> assign(:ding, %Ding{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Dings")
|> assign(:ding, nil)
end
@impl true
def handle_info({DingeWeb.DingLive.FormComponent, {:saved, ding}}, socket) do
{:noreply, stream_insert(socket, :dings, ding)}
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
ding = Ledger.get_ding!(id)
{:ok, _} = Ledger.delete_ding(ding)
{:noreply, stream_delete(socket, :dings, ding)}
end
end

View File

@ -0,0 +1,47 @@
<.header>
Listing Dings
<:actions>
<.link patch={~p"/dings/new"}>
<.button>New Ding</.button>
</.link>
</:actions>
</.header>
<.table
id="dings"
rows={@streams.dings}
row_click={fn {_id, ding} -> JS.navigate(~p"/dings/#{ding}") end}
>
<:col :let={{_id, ding}} label="Title"><%= ding.title %></:col>
<:col :let={{_id, ding}} label="Count"><%= ding.count %></:col>
<:col :let={{_id, ding}} label="Target count"><%= ding.target_count %></:col>
<:col :let={{_id, ding}} label="Ding type"><%= ding.ding_type %></:col>
<:col :let={{_id, ding}} label="Target type"><%= ding.target_type %></:col>
<:col :let={{_id, ding}} label="Status"><%= ding.status %></:col>
<:action :let={{_id, ding}}>
<div class="sr-only">
<.link navigate={~p"/dings/#{ding}"}>Show</.link>
</div>
<.link patch={~p"/dings/#{ding}/edit"}>Edit</.link>
</:action>
<:action :let={{id, ding}}>
<.link
phx-click={JS.push("delete", value: %{id: ding.id}) |> hide("##{id}")}
data-confirm="Are you sure?"
>
Delete
</.link>
</:action>
</.table>
<.modal :if={@live_action in [:new, :edit]} id="ding-modal" show on_cancel={JS.patch(~p"/dings")}>
<.live_component
module={DingeWeb.DingLive.FormComponent}
id={@ding.id || :new}
title={@page_title}
action={@live_action}
ding={@ding}
user={@current_user}
patch={~p"/dings"}
/>
</.modal>

View File

@ -0,0 +1,21 @@
defmodule DingeWeb.DingLive.Show do
use DingeWeb, :live_view
alias Dinge.Ledger
@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(:ding, Ledger.get_ding!(id))}
end
defp page_title(:show), do: "Show Ding"
defp page_title(:edit), do: "Edit Ding"
end

View File

@ -0,0 +1,31 @@
<.header>
Ding <%= @ding.id %>
<:subtitle>This is a ding record from your database.</:subtitle>
<:actions>
<.link patch={~p"/dings/#{@ding}/show/edit"} phx-click={JS.push_focus()}>
<.button>Edit ding</.button>
</.link>
</:actions>
</.header>
<.list>
<:item title="Title"><%= @ding.title %></:item>
<:item title="Count"><%= @ding.count %></:item>
<:item title="Target count"><%= @ding.target_count %></:item>
<:item title="Ding type"><%= @ding.ding_type %></:item>
<:item title="Target type"><%= @ding.target_type %></:item>
<:item title="Status"><%= @ding.status %></:item>
</.list>
<.back navigate={~p"/dings"}>Back to dings</.back>
<.modal :if={@live_action == :edit} id="ding-modal" show on_cancel={JS.patch(~p"/dings/#{@ding}")}>
<.live_component
module={DingeWeb.DingLive.FormComponent}
id={@ding.id}
title={@page_title}
action={@live_action}
ding={@ding}
patch={~p"/dings/#{@ding}"}
/>
</.modal>

View File

@ -23,6 +23,20 @@ defmodule DingeWeb.Router do
get "/", PageController, :home
end
scope "/", DingeWeb do
pipe_through [:browser, :require_authenticated_user]
live_session :dings_authenticated_user,
on_mount: [{DingeWeb.UserAuth, :ensure_authenticated}] do
live "/dings", DingLive.Index, :index
live "/dings/new", DingLive.Index, :new
live "/dings/:id/edit", DingLive.Index, :edit
live "/dings/:id", DingLive.Show, :show
live "/dings/:id/show/edit", DingLive.Show, :edit
end
end
# Other scopes may use custom stacks.
# scope "/api", DingeWeb do
# pipe_through :api