2021-08-09 08:48:23 +00:00
|
|
|
defmodule KetbinWeb.PageController do
|
2021-08-14 21:49:03 +00:00
|
|
|
require Logger
|
|
|
|
|
2021-08-09 08:48:23 +00:00
|
|
|
use KetbinWeb, :controller
|
|
|
|
|
2021-08-13 00:17:13 +00:00
|
|
|
alias Ketbin.Pastes
|
|
|
|
alias Ketbin.Pastes.Paste
|
2021-08-13 02:16:10 +00:00
|
|
|
alias Ketbin.Pastes.Utils
|
2021-08-13 00:17:13 +00:00
|
|
|
|
2021-08-09 08:48:23 +00:00
|
|
|
def index(conn, _params) do
|
2021-08-13 01:03:43 +00:00
|
|
|
changeset = Pastes.change_paste(%Paste{})
|
|
|
|
render(conn, "index.html", changeset: changeset)
|
2021-08-09 08:48:23 +00:00
|
|
|
end
|
2021-08-13 00:17:13 +00:00
|
|
|
|
2021-08-14 23:16:06 +00:00
|
|
|
def show(%{assigns: %{show_edit: show_edit}} = conn, %{"id" => id}) do
|
2021-08-13 06:43:59 +00:00
|
|
|
paste = Pastes.get_paste!(id) # fetch paste from db
|
2021-08-13 02:24:07 +00:00
|
|
|
|
2021-08-13 06:43:59 +00:00
|
|
|
if paste.is_url do # paste is a url, redirect
|
2021-08-13 02:24:07 +00:00
|
|
|
redirect(conn, external: paste.content)
|
2021-08-13 06:43:59 +00:00
|
|
|
else # regular paste, show content
|
2021-08-14 22:21:31 +00:00
|
|
|
render(conn, "show.html", paste: paste, show_edit: show_edit)
|
2021-08-13 02:24:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-14 23:16:06 +00:00
|
|
|
def showlink(%{assigns: %{show_edit: show_edit}} = conn, %{"id" => id}) do
|
2021-08-13 02:24:07 +00:00
|
|
|
paste = Pastes.get_paste!(id)
|
2021-08-14 22:21:31 +00:00
|
|
|
render(conn, "show.html", paste: paste, show_edit: show_edit)
|
2021-08-13 00:17:13 +00:00
|
|
|
end
|
2021-08-13 01:03:43 +00:00
|
|
|
|
2021-08-14 09:49:16 +00:00
|
|
|
def raw(conn, %{"id" => id}) do
|
|
|
|
paste = Pastes.get_paste!(id)
|
|
|
|
text(conn, paste.content)
|
|
|
|
end
|
|
|
|
|
2021-08-14 23:16:06 +00:00
|
|
|
def create(%{assigns: %{current_user: current_user}} = conn, %{"paste" => paste_params}) do
|
2021-08-13 06:43:59 +00:00
|
|
|
# generate phonetic key
|
2021-08-13 02:16:10 +00:00
|
|
|
id = Utils.generate_key()
|
|
|
|
|
2021-08-13 06:43:59 +00:00
|
|
|
# check if content is a url
|
2021-08-13 02:16:10 +00:00
|
|
|
is_url =
|
|
|
|
Map.get(paste_params, "content")
|
|
|
|
|> Utils.is_url?()
|
|
|
|
|
2021-08-13 06:43:59 +00:00
|
|
|
# put id and is_url values into changeset
|
2021-08-13 02:16:10 +00:00
|
|
|
paste_params =
|
|
|
|
Map.put(paste_params, "id", id)
|
|
|
|
|> Map.put("is_url", is_url)
|
2021-08-14 22:21:31 +00:00
|
|
|
|> Map.put("belongs_to", current_user && current_user.id)
|
2021-08-13 02:16:10 +00:00
|
|
|
|
2021-08-13 06:43:59 +00:00
|
|
|
# attempt to create a paste
|
2021-08-13 01:03:43 +00:00
|
|
|
case Pastes.create_paste(paste_params) do
|
2021-08-13 06:43:59 +00:00
|
|
|
{:ok, paste} -> # all good, redirect
|
2021-08-13 02:24:07 +00:00
|
|
|
unless is_url do
|
|
|
|
conn
|
2021-08-13 06:43:59 +00:00
|
|
|
|> redirect(to: Routes.page_path(conn, :show, paste)) # is a regular paste, take to regular route
|
2021-08-13 02:24:07 +00:00
|
|
|
else
|
|
|
|
conn
|
2021-08-13 06:43:59 +00:00
|
|
|
|> redirect(to: Routes.page_path(conn, :showlink, paste)) # is a url, take to route with /v/ prefix
|
2021-08-13 02:24:07 +00:00
|
|
|
end
|
2021-08-13 01:03:43 +00:00
|
|
|
|
2021-08-13 06:43:59 +00:00
|
|
|
{:error, %Ecto.Changeset{} = changeset} -> # something went wrong, bail
|
2021-08-13 01:03:43 +00:00
|
|
|
render(conn, "index.html", changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
2021-08-14 22:21:31 +00:00
|
|
|
|
|
|
|
def edit(conn, %{"id" => id}) do
|
|
|
|
paste = Pastes.get_paste!(id)
|
|
|
|
changeset = Pastes.change_paste(paste)
|
|
|
|
render(conn, "edit.html", paste: paste, changeset: changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"id" => id, "paste" => paste_params}) do
|
|
|
|
paste = Pastes.get_paste!(id)
|
|
|
|
|
|
|
|
case Pastes.update_paste(paste, paste_params) do
|
|
|
|
{:ok, paste} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Paste updated successfully.")
|
|
|
|
|> redirect(to: Routes.page_path(conn, :show, paste))
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
render(conn, "edit.html", paste: paste, changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
2021-08-09 08:48:23 +00:00
|
|
|
end
|