feat(pastes): set up redirection routes

Signed-off-by: SphericalKat <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2021-08-13 07:54:07 +05:30
parent 750d62cc73
commit f411017280
No known key found for this signature in database
GPG Key ID: ED5C54FBBB920E51
2 changed files with 18 additions and 2 deletions

View File

@ -12,6 +12,16 @@ defmodule KetbinWeb.PageController do
def show(conn, %{"id" => id}) do def show(conn, %{"id" => id}) do
paste = Pastes.get_paste!(id) paste = Pastes.get_paste!(id)
if paste.is_url do
redirect(conn, external: paste.content)
else
render(conn, "show.html", paste: paste)
end
end
def showlink(conn, %{"id" => id}) do
paste = Pastes.get_paste!(id)
render(conn, "show.html", paste: paste) render(conn, "show.html", paste: paste)
end end
@ -28,8 +38,13 @@ defmodule KetbinWeb.PageController do
case Pastes.create_paste(paste_params) do case Pastes.create_paste(paste_params) do
{:ok, paste} -> {:ok, paste} ->
unless is_url do
conn conn
|> redirect(to: Routes.page_path(conn, :show, paste)) |> redirect(to: Routes.page_path(conn, :show, paste))
else
conn
|> redirect(to: Routes.page_path(conn, :showlink, paste))
end
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
render(conn, "index.html", changeset: changeset) render(conn, "index.html", changeset: changeset)

View File

@ -21,6 +21,7 @@ defmodule KetbinWeb.Router do
get "/", PageController, :index get "/", PageController, :index
get "/:id", PageController, :show get "/:id", PageController, :show
get "/v/:id", PageController, :showlink
post "/", PageController, :create post "/", PageController, :create
end end