add notes

This commit is contained in:
2022-07-25 20:08:40 -04:00
parent 1a423f703b
commit 63fca7ff6a
14 changed files with 607 additions and 12 deletions

104
lib/memex/notes.ex Normal file
View File

@ -0,0 +1,104 @@
defmodule Memex.Notes do
@moduledoc """
The Notes context.
"""
import Ecto.Query, warn: false
alias Memex.Repo
alias Memex.Notes.Note
@doc """
Returns the list of notes.
## Examples
iex> list_notes()
[%Note{}, ...]
"""
def list_notes do
Repo.all(Note)
end
@doc """
Gets a single note.
Raises `Ecto.NoResultsError` if the Note does not exist.
## Examples
iex> get_note!(123)
%Note{}
iex> get_note!(456)
** (Ecto.NoResultsError)
"""
def get_note!(id), do: Repo.get!(Note, id)
@doc """
Creates a note.
## Examples
iex> create_note(%{field: value})
{:ok, %Note{}}
iex> create_note(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_note(attrs \\ %{}) do
%Note{}
|> Note.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a note.
## Examples
iex> update_note(note, %{field: new_value})
{:ok, %Note{}}
iex> update_note(note, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_note(%Note{} = note, attrs) do
note
|> Note.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a note.
## Examples
iex> delete_note(note)
{:ok, %Note{}}
iex> delete_note(note)
{:error, %Ecto.Changeset{}}
"""
def delete_note(%Note{} = note) do
Repo.delete(note)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking note changes.
## Examples
iex> change_note(note)
%Ecto.Changeset{data: %Note{}}
"""
def change_note(%Note{} = note, attrs \\ %{}) do
Note.changeset(note, attrs)
end
end

22
lib/memex/notes/note.ex Normal file
View File

@ -0,0 +1,22 @@
defmodule Memex.Notes.Note do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "notes" do
field :content, :string
field :tag, {:array, :string}
field :title, :string
field :visibility, Ecto.Enum, values: [:public, :private, :unlisted]
timestamps()
end
@doc false
def changeset(note, attrs) do
note
|> cast(attrs, [:title, :content, :tag, :visibility])
|> validate_required([:title, :content, :tag, :visibility])
end
end

View File

@ -92,6 +92,7 @@ defmodule MemexWeb do
# Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc)
import Phoenix.LiveView.Helpers
import MemexWeb.LiveHelpers
# Import basic rendering functionality (render, render_layout, etc)
import Phoenix.View

View File

@ -0,0 +1,55 @@
defmodule MemexWeb.NoteLive.FormComponent do
use MemexWeb, :live_component
alias Memex.Notes
@impl true
def update(%{note: note} = assigns, socket) do
changeset = Notes.change_note(note)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"note" => note_params}, socket) do
changeset =
socket.assigns.note
|> Notes.change_note(note_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event("save", %{"note" => note_params}, socket) do
save_note(socket, socket.assigns.action, note_params)
end
defp save_note(socket, :edit, note_params) do
case Notes.update_note(socket.assigns.note, note_params) do
{:ok, _note} ->
{:noreply,
socket
|> put_flash(:info, "Note updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_note(socket, :new, note_params) do
case Notes.create_note(note_params) do
{:ok, _note} ->
{:noreply,
socket
|> put_flash(:info, "Note created successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
end

View File

@ -0,0 +1,32 @@
<div>
<h2><%= @title %></h2>
<.form
let={f}
for={@changeset}
id="note-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save">
<%= label f, :title %>
<%= text_input f, :title %>
<%= error_tag f, :title %>
<%= label f, :content %>
<%= textarea f, :content %>
<%= error_tag f, :content %>
<%= label f, :tag %>
<%= multiple_select f, :tag, ["Option 1": "option1", "Option 2": "option2"] %>
<%= error_tag f, :tag %>
<%= label f, :visibility %>
<%= select f, :visibility, Ecto.Enum.values(Memex.Notes.Note, :visibility), prompt: "Choose a value" %>
<%= error_tag f, :visibility %>
<div>
<%= submit "Save", phx_disable_with: "Saving..." %>
</div>
</.form>
</div>

View File

@ -0,0 +1,46 @@
defmodule MemexWeb.NoteLive.Index do
use MemexWeb, :live_view
alias Memex.Notes
alias Memex.Notes.Note
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :notes, list_notes())}
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 Note")
|> assign(:note, Notes.get_note!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Note")
|> assign(:note, %Note{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Notes")
|> assign(:note, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
note = Notes.get_note!(id)
{:ok, _} = Notes.delete_note(note)
{:noreply, assign(socket, :notes, list_notes())}
end
defp list_notes do
Notes.list_notes()
end
end

View File

@ -0,0 +1,45 @@
<h1>Listing Notes</h1>
<%= if @live_action in [:new, :edit] do %>
<.modal return_to={Routes.note_index_path(@socket, :index)}>
<.live_component
module={MemexWeb.NoteLive.FormComponent}
id={@note.id || :new}
title={@page_title}
action={@live_action}
note={@note}
return_to={Routes.note_index_path(@socket, :index)}
/>
</.modal>
<% end %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th>Tag</th>
<th>Visibility</th>
<th></th>
</tr>
</thead>
<tbody id="notes">
<%= for note <- @notes do %>
<tr id={"note-#{note.id}"}>
<td><%= note.title %></td>
<td><%= note.content %></td>
<td><%= note.tag %></td>
<td><%= note.visibility %></td>
<td>
<span><%= live_redirect "Show", to: Routes.note_show_path(@socket, :show, note) %></span>
<span><%= live_patch "Edit", to: Routes.note_index_path(@socket, :edit, note) %></span>
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: note.id, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= live_patch "New Note", to: Routes.note_index_path(@socket, :new) %></span>

View File

@ -0,0 +1,21 @@
defmodule MemexWeb.NoteLive.Show do
use MemexWeb, :live_view
alias Memex.Notes
@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(:note, Notes.get_note!(id))}
end
defp page_title(:show), do: "Show Note"
defp page_title(:edit), do: "Edit Note"
end

View File

@ -0,0 +1,41 @@
<h1>Show Note</h1>
<%= if @live_action in [:edit] do %>
<.modal return_to={Routes.note_show_path(@socket, :show, @note)}>
<.live_component
module={MemexWeb.NoteLive.FormComponent}
id={@note.id}
title={@page_title}
action={@live_action}
note={@note}
return_to={Routes.note_show_path(@socket, :show, @note)}
/>
</.modal>
<% end %>
<ul>
<li>
<strong>Title:</strong>
<%= @note.title %>
</li>
<li>
<strong>Content:</strong>
<%= @note.content %>
</li>
<li>
<strong>Tag:</strong>
<%= @note.tag %>
</li>
<li>
<strong>Visibility:</strong>
<%= @note.visibility %>
</li>
</ul>
<span><%= live_patch "Edit", to: Routes.note_show_path(@socket, :edit, @note), class: "button" %></span> |
<span><%= live_redirect "Back", to: Routes.note_index_path(@socket, :index) %></span>

View File

@ -53,23 +53,38 @@ defmodule MemexWeb.Router do
put "/users/reset_password/:token", UserResetPasswordController, :update
end
scope "/", MemexWeb do
pipe_through [:browser, :require_authenticated_user]
live_session :default do
scope "/", MemexWeb do
pipe_through [:browser]
get "/users/settings", UserSettingsController, :edit
put "/users/settings", UserSettingsController, :update
delete "/users/settings/:id", UserSettingsController, :delete
get "/users/settings/confirm_email/:token", UserSettingsController, :confirm_email
live "/notes", NoteLive.Index, :index
live "/notes/:id", NoteLive.Show, :show
end
scope "/", MemexWeb do
pipe_through [:browser, :require_authenticated_user]
live "/notes/new", NoteLive.Index, :new
live "/notes/:id/edit", NoteLive.Index, :edit
live "/notes/:id/show/edit", NoteLive.Show, :edit
get "/users/settings", UserSettingsController, :edit
put "/users/settings", UserSettingsController, :update
delete "/users/settings/:id", UserSettingsController, :delete
get "/users/settings/confirm_email/:token", UserSettingsController, :confirm_email
end
end
scope "/", MemexWeb do
pipe_through [:browser, :require_authenticated_user, :require_admin]
live_session :admin do
scope "/", MemexWeb do
pipe_through [:browser, :require_authenticated_user, :require_admin]
live_dashboard "/dashboard", metrics: MemexWeb.Telemetry, ecto_repos: [Memex.Repo]
live_dashboard "/dashboard", metrics: MemexWeb.Telemetry, ecto_repos: [Memex.Repo]
live "/invites", InviteLive.Index, :index
live "/invites/new", InviteLive.Index, :new
live "/invites/:id/edit", InviteLive.Index, :edit
live "/invites", InviteLive.Index, :index
live "/invites/new", InviteLive.Index, :new
live "/invites/:id/edit", InviteLive.Index, :edit
end
end
scope "/", MemexWeb do