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
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)
end
@ -28,8 +38,13 @@ defmodule KetbinWeb.PageController do
case Pastes.create_paste(paste_params) do
{:ok, paste} ->
conn
|> redirect(to: Routes.page_path(conn, :show, paste))
unless is_url do
conn
|> redirect(to: Routes.page_path(conn, :show, paste))
else
conn
|> redirect(to: Routes.page_path(conn, :showlink, paste))
end
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "index.html", changeset: changeset)

View File

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