Browse Source

refactor subjects to layout dictated by phoenix

Steven Jacobs 7 months ago
parent
commit
bc2b0913cf

+ 104 - 0
lib/incidentctl/incident.ex

@@ -0,0 +1,104 @@
+defmodule Incidentctl.Incident do
+  @moduledoc """
+  The Incident context.
+  """
+
+  import Ecto.Query, warn: false
+  alias Incidentctl.Repo
+
+  alias Incidentctl.Incident.Subject
+
+  @doc """
+  Returns the list of subjects.
+
+  ## Examples
+
+      iex> list_subjects()
+      [%Subject{}, ...]
+
+  """
+  def list_subjects do
+    Repo.all(Subject)
+  end
+
+  @doc """
+  Gets a single subject.
+
+  Raises `Ecto.NoResultsError` if the Subject does not exist.
+
+  ## Examples
+
+      iex> get_subject!(123)
+      %Subject{}
+
+      iex> get_subject!(456)
+      ** (Ecto.NoResultsError)
+
+  """
+  def get_subject!(id), do: Repo.get!(Subject, id)
+
+  @doc """
+  Creates a subject.
+
+  ## Examples
+
+      iex> create_subject(%{field: value})
+      {:ok, %Subject{}}
+
+      iex> create_subject(%{field: bad_value})
+      {:error, %Ecto.Changeset{}}
+
+  """
+  def create_subject(attrs \\ %{}) do
+    %Subject{}
+    |> Subject.changeset(attrs)
+    |> Repo.insert()
+  end
+
+  @doc """
+  Updates a subject.
+
+  ## Examples
+
+      iex> update_subject(subject, %{field: new_value})
+      {:ok, %Subject{}}
+
+      iex> update_subject(subject, %{field: bad_value})
+      {:error, %Ecto.Changeset{}}
+
+  """
+  def update_subject(%Subject{} = subject, attrs) do
+    subject
+    |> Subject.changeset(attrs)
+    |> Repo.update()
+  end
+
+  @doc """
+  Deletes a Subject.
+
+  ## Examples
+
+      iex> delete_subject(subject)
+      {:ok, %Subject{}}
+
+      iex> delete_subject(subject)
+      {:error, %Ecto.Changeset{}}
+
+  """
+  def delete_subject(%Subject{} = subject) do
+    Repo.delete(subject)
+  end
+
+  @doc """
+  Returns an `%Ecto.Changeset{}` for tracking subject changes.
+
+  ## Examples
+
+      iex> change_subject(subject)
+      %Ecto.Changeset{source: %Subject{}}
+
+  """
+  def change_subject(%Subject{} = subject) do
+    Subject.changeset(subject, %{})
+  end
+end

+ 1 - 1
lib/incidentctl/subject.ex

@@ -1,4 +1,4 @@
-defmodule Incidentctl.Subject do
+defmodule Incidentctl.Incident.Subject do
   use Ecto.Schema
   import Ecto.Changeset
 

+ 2 - 1
lib/incidentctl/slack.ex

@@ -109,7 +109,8 @@ defmodule Incidentctl.Slack do
 
   def send_subjects(message, slack) do
     import Ecto.Query
-    alias Incidentctl.{Repo, Subject}
+    alias Incidentctl.Repo
+    alias Incidentctl.Incident.Subject
 
     reply =
       Repo.all(from s in Subject, select: s.name)

+ 62 - 0
lib/incidentctl_web/controllers/subject_controller.ex

@@ -0,0 +1,62 @@
+defmodule IncidentctlWeb.SubjectController do
+  use IncidentctlWeb, :controller
+
+  alias Incidentctl.Incident
+  alias Incidentctl.Incident.Subject
+
+  def index(conn, _params) do
+    subjects = Incident.list_subjects()
+    render(conn, "index.html", subjects: subjects)
+  end
+
+  def new(conn, _params) do
+    changeset = Incident.change_subject(%Subject{})
+    render(conn, "new.html", changeset: changeset)
+  end
+
+  def create(conn, %{"subject" => subject_params}) do
+    case Incident.create_subject(subject_params) do
+      {:ok, subject} ->
+        conn
+        |> put_flash(:info, "Subject created successfully.")
+        |> redirect(to: Routes.subject_path(conn, :show, subject))
+
+      {:error, %Ecto.Changeset{} = changeset} ->
+        render(conn, "new.html", changeset: changeset)
+    end
+  end
+
+  def show(conn, %{"id" => id}) do
+    subject = Incident.get_subject!(id)
+    render(conn, "show.html", subject: subject)
+  end
+
+  def edit(conn, %{"id" => id}) do
+    subject = Incident.get_subject!(id)
+    changeset = Incident.change_subject(subject)
+    render(conn, "edit.html", subject: subject, changeset: changeset)
+  end
+
+  def update(conn, %{"id" => id, "subject" => subject_params}) do
+    subject = Incident.get_subject!(id)
+
+    case Incident.update_subject(subject, subject_params) do
+      {:ok, subject} ->
+        conn
+        |> put_flash(:info, "Subject updated successfully.")
+        |> redirect(to: Routes.subject_path(conn, :show, subject))
+
+      {:error, %Ecto.Changeset{} = changeset} ->
+        render(conn, "edit.html", subject: subject, changeset: changeset)
+    end
+  end
+
+  def delete(conn, %{"id" => id}) do
+    subject = Incident.get_subject!(id)
+    {:ok, _subject} = Incident.delete_subject(subject)
+
+    conn
+    |> put_flash(:info, "Subject deleted successfully.")
+    |> redirect(to: Routes.subject_path(conn, :index))
+  end
+end

+ 1 - 0
lib/incidentctl_web/router.ex

@@ -17,6 +17,7 @@ defmodule IncidentctlWeb.Router do
     pipe_through :browser
 
     get "/", PageController, :index
+    resources "/subjects", SubjectController
   end
 
   # Other scopes may use custom stacks.

+ 5 - 0
lib/incidentctl_web/templates/subject/edit.html.eex

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

+ 15 - 0
lib/incidentctl_web/templates/subject/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, :name %>
+  <%= text_input f, :name %>
+  <%= error_tag f, :name %>
+
+  <div>
+    <%= submit "Save" %>
+  </div>
+<% end %>

+ 26 - 0
lib/incidentctl_web/templates/subject/index.html.eex

@@ -0,0 +1,26 @@
+<h1>Listing Subjects</h1>
+
+<table>
+  <thead>
+    <tr>
+      <th>Name</th>
+
+      <th></th>
+    </tr>
+  </thead>
+  <tbody>
+<%= for subject <- @subjects do %>
+    <tr>
+      <td><%= subject.name %></td>
+
+      <td>
+        <%= link "Show", to: Routes.subject_path(@conn, :show, subject) %>
+        <%= link "Edit", to: Routes.subject_path(@conn, :edit, subject) %>
+        <%= link "Delete", to: Routes.subject_path(@conn, :delete, subject), method: :delete, data: [confirm: "Are you sure?"] %>
+      </td>
+    </tr>
+<% end %>
+  </tbody>
+</table>
+
+<span><%= link "New Subject", to: Routes.subject_path(@conn, :new) %></span>

+ 5 - 0
lib/incidentctl_web/templates/subject/new.html.eex

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

+ 13 - 0
lib/incidentctl_web/templates/subject/show.html.eex

@@ -0,0 +1,13 @@
+<h1>Show Subject</h1>
+
+<ul>
+
+  <li>
+    <strong>Name:</strong>
+    <%= @subject.name %>
+  </li>
+
+</ul>
+
+<span><%= link "Edit", to: Routes.subject_path(@conn, :edit, @subject) %></span>
+<span><%= link "Back", to: Routes.subject_path(@conn, :index) %></span>

+ 3 - 0
lib/incidentctl_web/views/subject_view.ex

@@ -0,0 +1,3 @@
+defmodule IncidentctlWeb.SubjectView do
+  use IncidentctlWeb, :view
+end

priv/repo/migrations/20190420153251_create_subjects.exs → priv/repo/migrations/20190420155905_create_subjects.exs


+ 64 - 0
test/incidentctl/incident/incident_test.exs

@@ -0,0 +1,64 @@
+defmodule Incidentctl.IncidentTest do
+  use Incidentctl.DataCase
+
+  alias Incidentctl.Incident
+
+  describe "subjects" do
+    alias Incidentctl.Incident.Subject
+
+    @valid_attrs %{name: "some name"}
+    @update_attrs %{name: "some updated name"}
+    @invalid_attrs %{name: nil}
+
+    def subject_fixture(attrs \\ %{}) do
+      {:ok, subject} =
+        attrs
+        |> Enum.into(@valid_attrs)
+        |> Incident.create_subject()
+
+      subject
+    end
+
+    test "list_subjects/0 returns all subjects" do
+      subject = subject_fixture()
+      assert Incident.list_subjects() == [subject]
+    end
+
+    test "get_subject!/1 returns the subject with given id" do
+      subject = subject_fixture()
+      assert Incident.get_subject!(subject.id) == subject
+    end
+
+    test "create_subject/1 with valid data creates a subject" do
+      assert {:ok, %Subject{} = subject} = Incident.create_subject(@valid_attrs)
+      assert subject.name == "some name"
+    end
+
+    test "create_subject/1 with invalid data returns error changeset" do
+      assert {:error, %Ecto.Changeset{}} = Incident.create_subject(@invalid_attrs)
+    end
+
+    test "update_subject/2 with valid data updates the subject" do
+      subject = subject_fixture()
+      assert {:ok, %Subject{} = subject} = Incident.update_subject(subject, @update_attrs)
+      assert subject.name == "some updated name"
+    end
+
+    test "update_subject/2 with invalid data returns error changeset" do
+      subject = subject_fixture()
+      assert {:error, %Ecto.Changeset{}} = Incident.update_subject(subject, @invalid_attrs)
+      assert subject == Incident.get_subject!(subject.id)
+    end
+
+    test "delete_subject/1 deletes the subject" do
+      subject = subject_fixture()
+      assert {:ok, %Subject{}} = Incident.delete_subject(subject)
+      assert_raise Ecto.NoResultsError, fn -> Incident.get_subject!(subject.id) end
+    end
+
+    test "change_subject/1 returns a subject changeset" do
+      subject = subject_fixture()
+      assert %Ecto.Changeset{} = Incident.change_subject(subject)
+    end
+  end
+end

+ 89 - 0
test/incidentctl_web/controllers/subject_controller_test.exs

@@ -0,0 +1,89 @@
+defmodule IncidentctlWeb.SubjectControllerTest do
+  use IncidentctlWeb.ConnCase
+
+  alias Incidentctl.Incident
+
+  @create_attrs %{name: "some name"}
+  @update_attrs %{name: "some updated name"}
+  @invalid_attrs %{name: nil}
+
+  def fixture(:subject) do
+    {:ok, subject} = Incident.create_subject(@create_attrs)
+    subject
+  end
+
+  describe "index" do
+    test "lists all subjects", %{conn: conn} do
+      conn = get(conn, Routes.subject_path(conn, :index))
+      assert html_response(conn, 200) =~ "Listing Subjects"
+    end
+  end
+
+  describe "new subject" do
+    test "renders form", %{conn: conn} do
+      conn = get(conn, Routes.subject_path(conn, :new))
+      assert html_response(conn, 200) =~ "New Subject"
+    end
+  end
+
+  describe "create subject" do
+    test "redirects to show when data is valid", %{conn: conn} do
+      conn = post(conn, Routes.subject_path(conn, :create), subject: @create_attrs)
+
+      assert %{id: id} = redirected_params(conn)
+      assert redirected_to(conn) == Routes.subject_path(conn, :show, id)
+
+      conn = get(conn, Routes.subject_path(conn, :show, id))
+      assert html_response(conn, 200) =~ "Show Subject"
+    end
+
+    test "renders errors when data is invalid", %{conn: conn} do
+      conn = post(conn, Routes.subject_path(conn, :create), subject: @invalid_attrs)
+      assert html_response(conn, 200) =~ "New Subject"
+    end
+  end
+
+  describe "edit subject" do
+    setup [:create_subject]
+
+    test "renders form for editing chosen subject", %{conn: conn, subject: subject} do
+      conn = get(conn, Routes.subject_path(conn, :edit, subject))
+      assert html_response(conn, 200) =~ "Edit Subject"
+    end
+  end
+
+  describe "update subject" do
+    setup [:create_subject]
+
+    test "redirects when data is valid", %{conn: conn, subject: subject} do
+      conn = put(conn, Routes.subject_path(conn, :update, subject), subject: @update_attrs)
+      assert redirected_to(conn) == Routes.subject_path(conn, :show, subject)
+
+      conn = get(conn, Routes.subject_path(conn, :show, subject))
+      assert html_response(conn, 200) =~ "some updated name"
+    end
+
+    test "renders errors when data is invalid", %{conn: conn, subject: subject} do
+      conn = put(conn, Routes.subject_path(conn, :update, subject), subject: @invalid_attrs)
+      assert html_response(conn, 200) =~ "Edit Subject"
+    end
+  end
+
+  describe "delete subject" do
+    setup [:create_subject]
+
+    test "deletes chosen subject", %{conn: conn, subject: subject} do
+      conn = delete(conn, Routes.subject_path(conn, :delete, subject))
+      assert redirected_to(conn) == Routes.subject_path(conn, :index)
+
+      assert_error_sent 404, fn ->
+        get(conn, Routes.subject_path(conn, :show, subject))
+      end
+    end
+  end
+
+  defp create_subject(_) do
+    subject = fixture(:subject)
+    {:ok, subject: subject}
+  end
+end