From 106ae8398e123aa35e2b1497ebeb77fd5696ecbb Mon Sep 17 00:00:00 2001 From: Amogh Lele Date: Sat, 28 May 2022 10:29:47 +0530 Subject: [PATCH] feat: implement custom URLs Signed-off-by: Amogh Lele --- lib/ketbin/pastes/paste.ex | 1 + lib/ketbin_web/controllers/page_controller.ex | 16 +++++++++++++--- lib/ketbin_web/templates/page/edit.html.heex | 2 +- lib/ketbin_web/templates/page/form.html.heex | 11 +++++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/ketbin/pastes/paste.ex b/lib/ketbin/pastes/paste.ex index a54b552..593800c 100644 --- a/lib/ketbin/pastes/paste.ex +++ b/lib/ketbin/pastes/paste.ex @@ -16,5 +16,6 @@ defmodule Ketbin.Pastes.Paste do paste |> cast(attrs, [:is_url, :content, :id, :belongs_to]) |> validate_required([:is_url, :content]) + |> unique_constraint(:id, name: :pastes_pkey) end end diff --git a/lib/ketbin_web/controllers/page_controller.ex b/lib/ketbin_web/controllers/page_controller.ex index 0c087e9..c4bfcee 100644 --- a/lib/ketbin_web/controllers/page_controller.ex +++ b/lib/ketbin_web/controllers/page_controller.ex @@ -50,8 +50,12 @@ defmodule KetbinWeb.PageController do end def create(%{assigns: %{current_user: current_user}} = conn, %{"paste" => paste_params}) do - # generate phonetic key - id = Utils.generate_key() + # if custom url exists, use it as id, else generate phonetic id + id = + case Map.get(paste_params, "custom_url", "") do + custom_url when custom_url != "" -> custom_url + _ -> Utils.generate_key() + end # check if content is a url is_url = @@ -80,7 +84,13 @@ defmodule KetbinWeb.PageController do # something went wrong, bail {:error, %Ecto.Changeset{} = changeset} -> - render(conn, "index.html", changeset: changeset) + case changeset.errors[:id] do + {"has already been taken", _} -> + render(conn, "index.html", changeset: changeset, already_taken: true) + + _ -> + render(conn, "index.html", changeset: changeset) + end end end diff --git a/lib/ketbin_web/templates/page/edit.html.heex b/lib/ketbin_web/templates/page/edit.html.heex index ed47b51..3184732 100644 --- a/lib/ketbin_web/templates/page/edit.html.heex +++ b/lib/ketbin_web/templates/page/edit.html.heex @@ -1 +1 @@ -<%= render "form.html", Map.put(assigns, :action, Routes.page_path(@conn, :update, @paste)) %> +<%= render "form.html", Map.put(assigns, :action, Routes.page_path(@conn, :update, @paste)) |> Map.put(:is_edit, true) %> diff --git a/lib/ketbin_web/templates/page/form.html.heex b/lib/ketbin_web/templates/page/form.html.heex index 3a45ee6..a26f5a7 100644 --- a/lib/ketbin_web/templates/page/form.html.heex +++ b/lib/ketbin_web/templates/page/form.html.heex @@ -13,9 +13,20 @@ <% end %> + <%= if assigns[:already_taken] do %> +
+

This custom URL has already been taken.

+
+ <% end %> +
<%= textarea f, :content, [class: "w-full h-full px-6 py-4 outline-none bg-light-grey font-bold resize-none", placeholder: "> Paste, save, share! (Pasting just a URL will shorten it!)"] %>
+ <%= if @current_user && !assigns[:is_edit] do %> +
+ <%= text_input f, :custom_url, [class: "px-2 mr-2 outline-none text-black px-2 py-1", placeholder: "Custom URL"] %> +
+ <% end %>