Browse Source

add notes

main
Inhji Y. 10 months ago
parent
commit
3d2cb2381f
  1. 104
      lib/mirage/notes.ex
  2. 17
      lib/mirage/notes/note.ex
  3. 62
      lib/mirage_web/controllers/note_controller.ex
  4. 2
      lib/mirage_web/router.ex
  5. 5
      lib/mirage_web/templates/note/edit.html.eex
  6. 15
      lib/mirage_web/templates/note/form.html.eex
  7. 26
      lib/mirage_web/templates/note/index.html.eex
  8. 5
      lib/mirage_web/templates/note/new.html.eex
  9. 13
      lib/mirage_web/templates/note/show.html.eex
  10. 3
      lib/mirage_web/views/note_view.ex
  11. 12
      priv/repo/migrations/20210108235219_create_notes.exs
  12. 64
      test/mirage/notes_test.exs
  13. 88
      test/mirage_web/controllers/note_controller_test.exs

104
lib/mirage/notes.ex

@ -0,0 +1,104 @@
defmodule Mirage.Notes do
@moduledoc """
The Notes context.
"""
import Ecto.Query, warn: false
alias Mirage.Repo
alias Mirage.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

17
lib/mirage/notes/note.ex

@ -0,0 +1,17 @@
defmodule Mirage.Notes.Note do
use Ecto.Schema
import Ecto.Changeset
schema "notes" do
field :content, :string
timestamps()
end
@doc false
def changeset(note, attrs) do
note
|> cast(attrs, [:content])
|> validate_required([:content])
end
end

62
lib/mirage_web/controllers/note_controller.ex

@ -0,0 +1,62 @@
defmodule MirageWeb.NoteController do
use MirageWeb, :controller
alias Mirage.Notes
alias Mirage.Notes.Note
def index(conn, _params) do
notes = Notes.list_notes()
render(conn, "index.html", notes: notes)
end
def new(conn, _params) do
changeset = Notes.change_note(%Note{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"note" => note_params}) do
case Notes.create_note(note_params) do
{:ok, note} ->
conn
|> put_flash(:info, "Note created successfully.")
|> redirect(to: Routes.note_path(conn, :show, note))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
note = Notes.get_note!(id)
render(conn, "show.html", note: note)
end
def edit(conn, %{"id" => id}) do
note = Notes.get_note!(id)
changeset = Notes.change_note(note)
render(conn, "edit.html", note: note, changeset: changeset)
end
def update(conn, %{"id" => id, "note" => note_params}) do
note = Notes.get_note!(id)
case Notes.update_note(note, note_params) do
{:ok, note} ->
conn
|> put_flash(:info, "Note updated successfully.")
|> redirect(to: Routes.note_path(conn, :show, note))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", note: note, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
note = Notes.get_note!(id)
{:ok, _note} = Notes.delete_note(note)
conn
|> put_flash(:info, "Note deleted successfully.")
|> redirect(to: Routes.note_path(conn, :index))
end
end

2
lib/mirage_web/router.ex

@ -21,6 +21,8 @@ defmodule MirageWeb.Router do
pipe_through :browser
live "/", PageLive, :index
resources "/notes", NoteController
end
# Other scopes may use custom stacks.

5
lib/mirage_web/templates/note/edit.html.eex

@ -0,0 +1,5 @@
<h1>Edit Note</h1>
<%= render "form.html", Map.put(assigns, :action, Routes.note_path(@conn, :update, @note)) %>
<span><%= link "Back", to: Routes.note_path(@conn, :index) %></span>

15
lib/mirage_web/templates/note/form.html.eex

@ -0,0 +1,15 @@
<%= form_for @changeset, @action, fn f -> %>
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>
<%= label f, :content %>
<%= text_input f, :content %>
<%= error_tag f, :content %>
<div>
<%= submit "Save" %>
</div>
<% end %>

26
lib/mirage_web/templates/note/index.html.eex

@ -0,0 +1,26 @@
<h1>Listing Notes</h1>
<table>
<thead>
<tr>
<th>Content</th>
<th></th>
</tr>
</thead>
<tbody>
<%= for note <- @notes do %>
<tr>
<td><%= note.content %></td>
<td>
<span><%= link "Show", to: Routes.note_path(@conn, :show, note) %></span>
<span><%= link "Edit", to: Routes.note_path(@conn, :edit, note) %></span>
<span><%= link "Delete", to: Routes.note_path(@conn, :delete, note), method: :delete, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= link "New Note", to: Routes.note_path(@conn, :new) %></span>

5
lib/mirage_web/templates/note/new.html.eex

@ -0,0 +1,5 @@
<h1>New Note</h1>
<%= render "form.html", Map.put(assigns, :action, Routes.note_path(@conn, :create)) %>
<span><%= link "Back", to: Routes.note_path(@conn, :index) %></span>

13
lib/mirage_web/templates/note/show.html.eex

@ -0,0 +1,13 @@
<h1>Show Note</h1>
<ul>
<li>
<strong>Content:</strong>
<%= @note.content %>
</li>
</ul>
<span><%= link "Edit", to: Routes.note_path(@conn, :edit, @note) %></span>
<span><%= link "Back", to: Routes.note_path(@conn, :index) %></span>

3
lib/mirage_web/views/note_view.ex

@ -0,0 +1,3 @@
defmodule MirageWeb.NoteView do
use MirageWeb, :view
end

12
priv/repo/migrations/20210108235219_create_notes.exs

@ -0,0 +1,12 @@
defmodule Mirage.Repo.Migrations.CreateNotes do
use Ecto.Migration
def change do
create table(:notes) do
add :content, :string
timestamps()
end
end
end

64
test/mirage/notes_test.exs

@ -0,0 +1,64 @@
defmodule Mirage.NotesTest do
use Mirage.DataCase
alias Mirage.Notes
describe "notes" do
alias Mirage.Notes.Note
@valid_attrs %{content: "some content"}
@update_attrs %{content: "some updated content"}
@invalid_attrs %{content: nil}
def note_fixture(attrs \\ %{}) do
{:ok, note} =
attrs
|> Enum.into(@valid_attrs)
|> Notes.create_note()
note
end
test "list_notes/0 returns all notes" do
note = note_fixture()
assert Notes.list_notes() == [note]
end
test "get_note!/1 returns the note with given id" do
note = note_fixture()
assert Notes.get_note!(note.id) == note
end
test "create_note/1 with valid data creates a note" do
assert {:ok, %Note{} = note} = Notes.create_note(@valid_attrs)
assert note.content == "some content"
end
test "create_note/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Notes.create_note(@invalid_attrs)
end
test "update_note/2 with valid data updates the note" do
note = note_fixture()
assert {:ok, %Note{} = note} = Notes.update_note(note, @update_attrs)
assert note.content == "some updated content"
end
test "update_note/2 with invalid data returns error changeset" do
note = note_fixture()
assert {:error, %Ecto.Changeset{}} = Notes.update_note(note, @invalid_attrs)
assert note == Notes.get_note!(note.id)
end
test "delete_note/1 deletes the note" do
note = note_fixture()
assert {:ok, %Note{}} = Notes.delete_note(note)
assert_raise Ecto.NoResultsError, fn -> Notes.get_note!(note.id) end
end
test "change_note/1 returns a note changeset" do
note = note_fixture()
assert %Ecto.Changeset{} = Notes.change_note(note)
end
end
end

88
test/mirage_web/controllers/note_controller_test.exs

@ -0,0 +1,88 @@
defmodule MirageWeb.NoteControllerTest do
use MirageWeb.ConnCase
alias Mirage.Notes
@create_attrs %{content: "some content"}
@update_attrs %{content: "some updated content"}
@invalid_attrs %{content: nil}
def fixture(:note) do
{:ok, note} = Notes.create_note(@create_attrs)
note
end
describe "index" do
test "lists all notes", %{conn: conn} do
conn = get(conn, Routes.note_path(conn, :index))
assert html_response(conn, 200) =~ "Listing Notes"
end
end
describe "new note" do
test "renders form", %{conn: conn} do
conn = get(conn, Routes.note_path(conn, :new))
assert html_response(conn, 200) =~ "New Note"
end
end
describe "create note" do
test "redirects to show when data is valid", %{conn: conn} do
conn = post(conn, Routes.note_path(conn, :create), note: @create_attrs)
assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == Routes.note_path(conn, :show, id)
conn = get(conn, Routes.note_path(conn, :show, id))
assert html_response(conn, 200) =~ "Show Note"
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, Routes.note_path(conn, :create), note: @invalid_attrs)
assert html_response(conn, 200) =~ "New Note"
end
end
describe "edit note" do
setup [:create_note]
test "renders form for editing chosen note", %{conn: conn, note: note} do
conn = get(conn, Routes.note_path(conn, :edit, note))
assert html_response(conn, 200) =~ "Edit Note"
end
end
describe "update note" do
setup [:create_note]
test "redirects when data is valid", %{conn: conn, note: note} do
conn = put(conn, Routes.note_path(conn, :update, note), note: @update_attrs)
assert redirected_to(conn) == Routes.note_path(conn, :show, note)
conn = get(conn, Routes.note_path(conn, :show, note))
assert html_response(conn, 200) =~ "some updated content"
end
test "renders errors when data is invalid", %{conn: conn, note: note} do
conn = put(conn, Routes.note_path(conn, :update, note), note: @invalid_attrs)
assert html_response(conn, 200) =~ "Edit Note"
end
end
describe "delete note" do
setup [:create_note]
test "deletes chosen note", %{conn: conn, note: note} do
conn = delete(conn, Routes.note_path(conn, :delete, note))
assert redirected_to(conn) == Routes.note_path(conn, :index)
assert_error_sent 404, fn ->
get(conn, Routes.note_path(conn, :show, note))
end
end
end
defp create_note(_) do
note = fixture(:note)
%{note: note}
end
end
Loading…
Cancel
Save