Skip to content

Commit

Permalink
Add topic views (mercadona#38)
Browse files Browse the repository at this point in the history
* Add topic views

* Add tests for get topics

* Add topic creationg test

Co-authored-by: Imanol Cea <[email protected]>
  • Loading branch information
sanntt and lonamiaec authored Feb 4, 2020
1 parent 6d0b3d1 commit f33a369
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 2 deletions.
27 changes: 27 additions & 0 deletions lib/postoffice_web/controllers/topic_controller.ex
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
2 changes: 2 additions & 0 deletions lib/postoffice_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ defmodule PostofficeWeb.Router do

get "/", IndexController, :index, as: :dashboard

resources "/topics", TopicController, only: [:index, :new, :create]

resources "/publishers", PublisherController, only: [:index, :new, :create, :edit, :update]

resources "/messages", MessageController, only: [:index, :show]
Expand Down
4 changes: 2 additions & 2 deletions lib/postoffice_web/templates/layout/app.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<a href="<%= Routes.topic_path(@conn, :index) %>" class="nav-link">
<i class="nav-icon fa fa-bullhorn"></i>
<p>
Topics
Expand All @@ -80,4 +80,4 @@
<script src="/js/app.js"></script>
</body>

</html>
</html>
21 changes: 21 additions & 0 deletions lib/postoffice_web/templates/topic/index.html.eex
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>
15 changes: 15 additions & 0 deletions lib/postoffice_web/templates/topic/new.html.eex
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>
3 changes: 3 additions & 0 deletions lib/postoffice_web/views/topic_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule PostofficeWeb.TopicView do
use PostofficeWeb, :view
end
35 changes: 35 additions & 0 deletions test/postoffice_web/controllers/topic_controller_test.exs
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

0 comments on commit f33a369

Please sign in to comment.