From e02128a09d17442787bd3e72d0d65a09c53dc8a5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Thu, 23 Jun 2022 01:17:40 +0200
Subject: [PATCH] Podpora custom url

---
 TODO                               |  6 +++---
 VERSION                            |  2 +-
 lib/PZ/Controller/Shortcut.pm      | 26 +++++++++++++++++++++++---
 templates/includes/form.html.ep    | 29 +++++++++++++++++++----------
 templates/shortcut/invalid.html.ep |  2 +-
 5 files changed, 47 insertions(+), 18 deletions(-)

diff --git a/TODO b/TODO
index feebcfc..5966e7f 100644
--- a/TODO
+++ b/TODO
@@ -1,4 +1,4 @@
-- custom url
-- 302 nebo informacni stranka
 - statistika
-- smazani
+- editace/smazani
+- loop detect
+- 302 nebo informacni stranka
diff --git a/VERSION b/VERSION
index d917d3e..0ea3a94 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.1.2
+0.2.0
diff --git a/lib/PZ/Controller/Shortcut.pm b/lib/PZ/Controller/Shortcut.pm
index c453e66..792af36 100644
--- a/lib/PZ/Controller/Shortcut.pm
+++ b/lib/PZ/Controller/Shortcut.pm
@@ -8,11 +8,31 @@ use Image::PNG::QRCode 'qrpng';
 sub create ($c) {
     my $url = $c->param('url');
 
-    if( ! is_uri($url) ){
-        $c->render('shortcut/invalid');
+    if( ! is_uri($url) ) {
+        $c->render('shortcut/invalid', error => 'Chybná URL adresa!');
         return;
     }
 
+    my $custom = lc($c->param('shortcut'));
+
+    if ( ! $custom =~ /^[a..z0..9]{1,8}$/i ) {
+        $c->render('shortcut/invalid', error => 'Neplatná zkratka');
+        return;
+    }
+
+    if ( $custom ) {
+        my $exists = $c->schema->resultset('Shortcut')->search({
+            is_active => 1,
+            deleted   => undef,
+            shortcut  => $custom,
+        })->count;
+
+        if( $exists ) {
+            $c->render('shortcut/invalid', error => "Zkratka $custom už je použitá");
+            return;
+        }
+    }
+
     my %data = (
         user_id => $c->current_user->{id},
         deleted => undef,
@@ -23,7 +43,7 @@ sub create ($c) {
 
     $shortcut ||= $c->schema->resultset('Shortcut')->create({
         %data,
-        shortcut => $c->schema->resultset('Shortcut')->generate(),
+        shortcut => $custom // $c->schema->resultset('Shortcut')->generate(),
     });
 
     $url = 'https://' . $c->config->{domain} . '/' . $shortcut->shortcut;
diff --git a/templates/includes/form.html.ep b/templates/includes/form.html.ep
index 2135824..cd97686 100644
--- a/templates/includes/form.html.ep
+++ b/templates/includes/form.html.ep
@@ -1,23 +1,32 @@
-<p>
-<div class="card elevation-4">
-  <div class="card__body">
 <form hx-post="/" hx-target="#response" hx-params="*">
 
-<div class="form-field">
-  <label class="form-field__label" for="field">URL</label>
+<div class="card elevation-4 space-y-4">
+ <div class="card__body">
+   <div class="grid grid-cols-12 gap-4 row-gap-6">
+
+<div class="form-field col-span-9">
+  <label class="form-field__label" for="url">URL</label>
   <div class="form-field__wrapper form-field__wrapper--shadowed">
     <input type="text" name="url" class="text-input form-field__control" value="" placeholder="https://www.pirati.cz/program/dlouhodoby/psychotropni-latky/" required="required" />
 
-    <button class="btn btn--grey-125 btn--hoveractive">
+  </div>
+</div>
+
+<div class="form-field col-span-3">
+  <label class="form-field__label" for="shortcut">Zkratka</label>
+  <div class="form-field__wrapper form-field__wrapper--shadowed">
+    <input type="text" name="shortcut" class="text-input form-field__control w-40" size="8" maxlength="8" value="" placeholder="thc" />
+
+    <button class="btn btn--grey-125 btn--hoveractive ml-4">
         <div class="btn__body">Zkrátit</div>
     </button>
-
   </div>
 </div>
 
-</form>
-  </div>
 </div>
-</p>
+</div>
+</div>
+
+</form>
 
 <div id="response"></div>
diff --git a/templates/shortcut/invalid.html.ep b/templates/shortcut/invalid.html.ep
index 2802bc6..d21bf2b 100644
--- a/templates/shortcut/invalid.html.ep
+++ b/templates/shortcut/invalid.html.ep
@@ -1,4 +1,4 @@
 <span class="alert alert--red-600 alert--faded">
-  <span>Chybná URL adresa!</span>
+  <span><%= $error %></span>
 </span>
 
-- 
GitLab