From f33a369331612e876430a813c7ae32874b30107c Mon Sep 17 00:00:00 2001 From: Santiago Lanus <1985181+sanntt@users.noreply.github.com> Date: Tue, 4 Feb 2020 17:41:27 +0100 Subject: [PATCH] Add topic views (#38) * Add topic views * Add tests for get topics * Add topic creationg test Co-authored-by: Imanol Cea --- .../controllers/topic_controller.ex | 27 ++++++++++++++ lib/postoffice_web/router.ex | 2 ++ .../templates/layout/app.html.eex | 4 +-- .../templates/topic/index.html.eex | 21 +++++++++++ .../templates/topic/new.html.eex | 15 ++++++++ lib/postoffice_web/views/topic_view.ex | 3 ++ .../controllers/topic_controller_test.exs | 35 +++++++++++++++++++ 7 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 lib/postoffice_web/controllers/topic_controller.ex create mode 100644 lib/postoffice_web/templates/topic/index.html.eex create mode 100644 lib/postoffice_web/templates/topic/new.html.eex create mode 100644 lib/postoffice_web/views/topic_view.ex create mode 100644 test/postoffice_web/controllers/topic_controller_test.exs diff --git a/lib/postoffice_web/controllers/topic_controller.ex b/lib/postoffice_web/controllers/topic_controller.ex new file mode 100644 index 00000000..10e19476 --- /dev/null +++ b/lib/postoffice_web/controllers/topic_controller.ex @@ -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 diff --git a/lib/postoffice_web/router.ex b/lib/postoffice_web/router.ex index e6dff585..4f3908eb 100644 --- a/lib/postoffice_web/router.ex +++ b/lib/postoffice_web/router.ex @@ -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] diff --git a/lib/postoffice_web/templates/layout/app.html.eex b/lib/postoffice_web/templates/layout/app.html.eex index 6492d53f..c79cf2dc 100644 --- a/lib/postoffice_web/templates/layout/app.html.eex +++ b/lib/postoffice_web/templates/layout/app.html.eex @@ -57,7 +57,7 @@