Skip to content

Commit

Permalink
Add Topics node.
Browse files Browse the repository at this point in the history
  • Loading branch information
zven21 committed Jul 2, 2018
1 parent aa4e1ce commit 5407796
Show file tree
Hide file tree
Showing 13 changed files with 455 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/mipha/topics/node.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Mipha.Topics.Node do
@moduledoc false
use Ecto.Schema
import Ecto.Changeset

schema "nodes" do
field :name, :string
field :parent_id, :integer
field :position, :integer
field :summary, :string

timestamps()
end

@doc false
def changeset(node, attrs) do
node
|> cast(attrs, [:name, :summary, :position, :parent_id])
|> validate_required([:name, :summary, :position, :parent_id])
end
end
96 changes: 96 additions & 0 deletions lib/mipha/topics/topics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,100 @@ defmodule Mipha.Topics do
def change_topic(%Topic{} = topic) do
Topic.changeset(topic, %{})
end

alias Mipha.Topics.Node

@doc """
Returns the list of nodes.
## Examples
iex> list_nodes()
[%Node{}, ...]
"""
def list_nodes do
Repo.all(Node)
end

@doc """
Gets a single node.
Raises `Ecto.NoResultsError` if the Node does not exist.
## Examples
iex> get_node!(123)
%Node{}
iex> get_node!(456)
** (Ecto.NoResultsError)
"""
def get_node!(id), do: Repo.get!(Node, id)

@doc """
Creates a node.
## Examples
iex> create_node(%{field: value})
{:ok, %Node{}}
iex> create_node(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_node(attrs \\ %{}) do
%Node{}
|> Node.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a node.
## Examples
iex> update_node(node, %{field: new_value})
{:ok, %Node{}}
iex> update_node(node, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_node(%Node{} = node, attrs) do
node
|> Node.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a Node.
## Examples
iex> delete_node(node)
{:ok, %Node{}}
iex> delete_node(node)
{:error, %Ecto.Changeset{}}
"""
def delete_node(%Node{} = node) do
Repo.delete(node)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking node changes.
## Examples
iex> change_node(node)
%Ecto.Changeset{source: %Node{}}
"""
def change_node(%Node{} = node) do
Node.changeset(node, %{})
end
end
60 changes: 60 additions & 0 deletions lib/mipha_web/controllers/admin/node_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
defmodule MiphaWeb.Admin.NodeController do
use MiphaWeb, :controller

alias Mipha.Topics
alias Mipha.Topics.Node

def index(conn, _params) do
nodes = Topics.list_nodes()
render(conn, "index.html", nodes: nodes)
end

def new(conn, _params) do
changeset = Topics.change_node(%Node{})
render(conn, "new.html", changeset: changeset)
end

def create(conn, %{"node" => node_params}) do
case Topics.create_node(node_params) do
{:ok, node} ->
conn
|> put_flash(:info, "Node created successfully.")
|> redirect(to: admin_node_path(conn, :show, node))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end

def show(conn, %{"id" => id}) do
node = Topics.get_node!(id)
render(conn, "show.html", node: node)
end

def edit(conn, %{"id" => id}) do
node = Topics.get_node!(id)
changeset = Topics.change_node(node)
render(conn, "edit.html", node: node, changeset: changeset)
end

def update(conn, %{"id" => id, "node" => node_params}) do
node = Topics.get_node!(id)

case Topics.update_node(node, node_params) do
{:ok, node} ->
conn
|> put_flash(:info, "Node updated successfully.")
|> redirect(to: admin_node_path(conn, :show, node))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", node: node, changeset: changeset)
end
end

def delete(conn, %{"id" => id}) do
node = Topics.get_node!(id)
{:ok, _node} = Topics.delete_node(node)

conn
|> put_flash(:info, "Node deleted successfully.")
|> redirect(to: admin_node_path(conn, :index))
end
end
1 change: 1 addition & 0 deletions lib/mipha_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ defmodule MiphaWeb.Router do

get "/", PageController, :index
resources "/users", UserController
resources "/nodes", NodeController
resources "/topics", TopicController
resources "/repies", ReplyController
end
Expand Down
5 changes: 5 additions & 0 deletions lib/mipha_web/templates/admin/node/edit.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h2>Edit Node</h2>

<%= render "form.html", Map.put(assigns, :action, admin_node_path(@conn, :update, @node)) %>

<span><%= link "Back", to: admin_node_path(@conn, :index) %></span>
35 changes: 35 additions & 0 deletions lib/mipha_web/templates/admin/node/form.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<%= 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 %>

<div class="form-group">
<%= label f, :name, class: "control-label" %>
<%= text_input f, :name, class: "form-control" %>
<%= error_tag f, :name %>
</div>

<div class="form-group">
<%= label f, :summary, class: "control-label" %>
<%= text_input f, :summary, class: "form-control" %>
<%= error_tag f, :summary %>
</div>

<div class="form-group">
<%= label f, :position, class: "control-label" %>
<%= number_input f, :position, class: "form-control" %>
<%= error_tag f, :position %>
</div>

<div class="form-group">
<%= label f, :parent_id, class: "control-label" %>
<%= number_input f, :parent_id, class: "form-control" %>
<%= error_tag f, :parent_id %>
</div>

<div class="form-group">
<%= submit "Submit", class: "btn btn-primary" %>
</div>
<% end %>
32 changes: 32 additions & 0 deletions lib/mipha_web/templates/admin/node/index.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<h2>Listing Nodes</h2>

<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Summary</th>
<th>Position</th>
<th>Parent</th>

<th></th>
</tr>
</thead>
<tbody>
<%= for node <- @nodes do %>
<tr>
<td><%= node.name %></td>
<td><%= node.summary %></td>
<td><%= node.position %></td>
<td><%= node.parent_id %></td>

<td class="text-right">
<span><%= link "Show", to: admin_node_path(@conn, :show, node), class: "btn btn-default btn-xs" %></span>
<span><%= link "Edit", to: admin_node_path(@conn, :edit, node), class: "btn btn-default btn-xs" %></span>
<span><%= link "Delete", to: admin_node_path(@conn, :delete, node), method: :delete, data: [confirm: "Are you sure?"], class: "btn btn-danger btn-xs" %></span>
</td>
</tr>
<% end %>
</tbody>
</table>

<span><%= link "New Node", to: admin_node_path(@conn, :new) %></span>
5 changes: 5 additions & 0 deletions lib/mipha_web/templates/admin/node/new.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h2>New Node</h2>

<%= render "form.html", Map.put(assigns, :action, admin_node_path(@conn, :create)) %>

<span><%= link "Back", to: admin_node_path(@conn, :index) %></span>
28 changes: 28 additions & 0 deletions lib/mipha_web/templates/admin/node/show.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h2>Show Node</h2>

<ul>

<li>
<strong>Name:</strong>
<%= @node.name %>
</li>

<li>
<strong>Summary:</strong>
<%= @node.summary %>
</li>

<li>
<strong>Position:</strong>
<%= @node.position %>
</li>

<li>
<strong>Parent:</strong>
<%= @node.parent_id %>
</li>

</ul>

<span><%= link "Edit", to: admin_node_path(@conn, :edit, @node) %></span>
<span><%= link "Back", to: admin_node_path(@conn, :index) %></span>
3 changes: 3 additions & 0 deletions lib/mipha_web/views/admin/node_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule MiphaWeb.Admin.NodeView do
use MiphaWeb, :view
end
15 changes: 15 additions & 0 deletions priv/repo/migrations/20180702081845_create_nodes.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Mipha.Repo.Migrations.CreateNodes do
use Ecto.Migration

def change do
create table(:nodes) do
add :name, :string
add :summary, :string
add :position, :integer
add :parent_id, :integer

timestamps()
end

end
end
66 changes: 66 additions & 0 deletions test/mipha/topics/topics_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,70 @@ defmodule Mipha.TopicsTest do
assert %Ecto.Changeset{} = Topics.change_topic(topic)
end
end

describe "nodes" do
alias Mipha.Topics.Node

@valid_attrs %{name: "some name", parent_id: 42, position: 42, summary: "some summary"}
@update_attrs %{name: "some updated name", parent_id: 43, position: 43, summary: "some updated summary"}
@invalid_attrs %{name: nil, parent_id: nil, position: nil, summary: nil}

def node_fixture(attrs \\ %{}) do
{:ok, node} =
attrs
|> Enum.into(@valid_attrs)
|> Topics.create_node()

node
end

test "list_nodes/0 returns all nodes" do
node = node_fixture()
assert Topics.list_nodes() == [node]
end

test "get_node!/1 returns the node with given id" do
node = node_fixture()
assert Topics.get_node!(node.id) == node
end

test "create_node/1 with valid data creates a node" do
assert {:ok, %Node{} = node} = Topics.create_node(@valid_attrs)
assert node.name == "some name"
assert node.parent_id == 42
assert node.position == 42
assert node.summary == "some summary"
end

test "create_node/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Topics.create_node(@invalid_attrs)
end

test "update_node/2 with valid data updates the node" do
node = node_fixture()
assert {:ok, node} = Topics.update_node(node, @update_attrs)
assert %Node{} = node
assert node.name == "some updated name"
assert node.parent_id == 43
assert node.position == 43
assert node.summary == "some updated summary"
end

test "update_node/2 with invalid data returns error changeset" do
node = node_fixture()
assert {:error, %Ecto.Changeset{}} = Topics.update_node(node, @invalid_attrs)
assert node == Topics.get_node!(node.id)
end

test "delete_node/1 deletes the node" do
node = node_fixture()
assert {:ok, %Node{}} = Topics.delete_node(node)
assert_raise Ecto.NoResultsError, fn -> Topics.get_node!(node.id) end
end

test "change_node/1 returns a node changeset" do
node = node_fixture()
assert %Ecto.Changeset{} = Topics.change_node(node)
end
end
end
Loading

0 comments on commit 5407796

Please sign in to comment.