-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
455 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule MiphaWeb.Admin.NodeView do | ||
use MiphaWeb, :view | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.