forked from mercadona/postoffice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add topic views * Add tests for get topics * Add topic creationg test Co-authored-by: Imanol Cea <[email protected]>
- Loading branch information
Showing
7 changed files
with
105 additions
and
2 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,27 @@ | ||
defmodule PostofficeWeb.TopicController do | ||
use PostofficeWeb, :controller | ||
|
||
alias Postoffice.Messaging | ||
alias Postoffice.Messaging.Topic | ||
|
||
def index(conn, _params) do | ||
topics = Messaging.list_topics() | ||
render(conn, "index.html", topics: topics) | ||
end | ||
|
||
def new(conn, _params) do | ||
changeset = Topic.changeset(%Topic{}, %{}) | ||
|
||
render(conn, "new.html", changeset: changeset) | ||
end | ||
|
||
def create(conn, %{"topic" => topic_params}) do | ||
{:ok, topic} = Postoffice.create_topic(topic_params) | ||
|
||
conn | ||
|> put_flash(:info, "Topic #{topic.id} created!") | ||
|> redirect(to: Routes.topic_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
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 @@ | ||
<div class="row"> | ||
<div class="col-md-12"> | ||
<a class="button"><%= link "Añadir", to: Routes.topic_path(@conn, :new) %></a> | ||
</div> | ||
<div class="col-md-12"> | ||
<table class="table table-sm"> | ||
<thead> | ||
<tr> | ||
<th scope="col">#</th> | ||
<th scope="col">Name</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<%= for topic <- @topics do %> | ||
<tr> | ||
<td><%= topic.id %></td> | ||
<td><%= topic.name %></td> | ||
</tr> | ||
<% end %> | ||
</div> | ||
</div> |
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 @@ | ||
<h2>Add new topic</h2> | ||
<div class="row"> | ||
<div class="col-md-3"> | ||
<%= form_for @changeset, Routes.topic_path(@conn, :create), fn f -> %> | ||
<div class="form-group"> | ||
<label>Topic</label> | ||
<%= text_input f, :name, class: "form-control" %> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= submit "Submit", class: "btn btn-primary" %> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> |
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 PostofficeWeb.TopicView do | ||
use PostofficeWeb, :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,35 @@ | ||
defmodule PostofficeWeb.TopicControllerTest do | ||
use PostofficeWeb.ConnCase | ||
|
||
alias Postoffice.Fixtures | ||
|
||
setup do | ||
{:ok, conn: Phoenix.ConnTest.build_conn()} | ||
end | ||
|
||
describe "list topics" do | ||
test "can access to topics list", %{conn: conn} do | ||
|
||
conn | ||
|> get(Routes.topic_path(conn, :index)) | ||
|> html_response(200) | ||
end | ||
|
||
test "all created topics are listed", %{conn: conn} do | ||
topic = Fixtures.create_topic() | ||
|
||
conn | ||
|> get(Routes.topic_path(conn, :index)) | ||
|> html_response(200) =~ topic.name | ||
end | ||
end | ||
|
||
describe "create topics" do | ||
test "can access to topics list", %{conn: conn} do | ||
|
||
conn | ||
|> post(Routes.topic_path(conn, :create, [topic: %{name: "Test Topic"}])) | ||
|> redirected_to() == "/topics" | ||
end | ||
end | ||
end |