From cfef863982bf46871d9a9537c33da54fa1ba094b Mon Sep 17 00:00:00 2001 From: Kilderson Sena Date: Sun, 19 Sep 2021 11:43:19 -0300 Subject: [PATCH] adding total clicks and urls --- config/routes.php | 8 ++--- public_html/js/script.js | 19 +++++++++++ src/Controllers/AccessUrlController.php | 15 +++++---- src/Controllers/HomeController.php | 41 ++++++++++++++++++++++++ src/Controllers/ShortenUrlController.php | 12 +++---- templates/index.html.twig | 2 +- 6 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 src/Controllers/HomeController.php diff --git a/config/routes.php b/config/routes.php index f2cbd0a..b2607d5 100644 --- a/config/routes.php +++ b/config/routes.php @@ -3,17 +3,13 @@ declare(strict_types=1); use App\Controllers\AccessUrlController; +use App\Controllers\HomeController; use App\Controllers\ShortenUrlController; -use Psr\Http\Message\RequestInterface as Request; -use Psr\Http\Message\ResponseInterface as Response; use Slim\App; use Slim\Routing\RouteCollectorProxy; return function (App $app) { - $app->get('/', function (Request $request, Response $response, array $args) { - return $this->get('view')->render($response, 'index.html.twig', []); - }); - + $app->get('/', HomeController::class); $app->get('/{path}', AccessUrlController::class); $app->group('/api/public', function (RouteCollectorProxy $group) { diff --git a/public_html/js/script.js b/public_html/js/script.js index 9c6f955..6c38aba 100644 --- a/public_html/js/script.js +++ b/public_html/js/script.js @@ -88,6 +88,25 @@ $(document).ready(function () { $divResult.find('a').attr('href', payload.data.shortened).html(payload.data.shortened); $divResult.find('button').attr('data-url', payload.data.shortened); }).fail(function(jqXHR, textStatus, msg) { + if (jqXHR.status === 400) { + const payload = jqXHR.responseJSON; + let message = ''; + switch (payload.data.huge_url) { + case 'invalid-url': + message = 'Insira ua URL válida com "http://" ou "https://" para poder encurtar.'; + break; + case 'empty-value': + message = 'Url longa não pode ser vazia.'; + break; + } + alert(message); + $inputUrl.select(); + } + + if (jqXHR.status === 500) { + alert('Ops, ocorreu algum problema pra gerar sua URL. Por favor, tenta novamente.'); + } + $btnShorten.html(originalContent); $btnShorten.removeAttr('disabled'); $inputUrl.removeAttr('disabled'); diff --git a/src/Controllers/AccessUrlController.php b/src/Controllers/AccessUrlController.php index fec7251..94054e1 100644 --- a/src/Controllers/AccessUrlController.php +++ b/src/Controllers/AccessUrlController.php @@ -10,30 +10,31 @@ use Psr\Http\Message\RequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; use Ramsey\Uuid\Uuid; +use Slim\Views\Twig; final class AccessUrlController { - private ContainerInterface $container; + private PDO $db; + private Twig $view; public function __construct(ContainerInterface $container) { - $this->container = $container; + $this->db = $container->get('db'); + $this->view = $container->get('view'); } public function __invoke(Request $request, Response $response, array $args): Response { - /** @var PDO $db */ - $db = $this->container->get('db'); - $stmt = $db->prepare("SELECT `id`, `long_url` FROM `urls` WHERE `short_url_path` = :path"); + $stmt = $this->db->prepare("SELECT `id`, `long_url` FROM `urls` WHERE `short_url_path` = :path"); $stmt->execute(['path' => $args['path']]); $row = $stmt->fetch(); if (!$row) { - return $this->container->get('view')->render($response, 'notfound.html.twig', []); + return $this->view->render($response, 'notfound.html.twig', []); } $sql = "INSERT INTO `urls_logs` (`id`, `uuid`, `url_id`, `created_at`, `meta`) VALUES (:id, :uuid, :url_id, :created_at, :meta)"; - $stmt = $db->prepare($sql); + $stmt = $this->db->prepare($sql); $uuid = Uuid::uuid4(); diff --git a/src/Controllers/HomeController.php b/src/Controllers/HomeController.php new file mode 100644 index 0000000..eb0d737 --- /dev/null +++ b/src/Controllers/HomeController.php @@ -0,0 +1,41 @@ +db = $container->get('db'); + $this->view = $container->get('view'); + } + + public function __invoke(Request $request, Response $response, array $args): Response + { + $stmt = $this->db->prepare(trim(" + select count(*) as total_urls from urls + union all + select count(*) as total_clicks from urls_logs + ")); + + $stmt->execute(); + + [$totalUrls, $totalClicks] = $stmt->fetchAll(PDO::FETCH_COLUMN); + + return $this->view->render($response, 'index.html.twig', [ + 'totalUrls' => ceil($totalUrls), + 'totalClicks' => ceil($totalClicks) + ]); + } +} \ No newline at end of file diff --git a/src/Controllers/ShortenUrlController.php b/src/Controllers/ShortenUrlController.php index e1b7c77..f65e189 100644 --- a/src/Controllers/ShortenUrlController.php +++ b/src/Controllers/ShortenUrlController.php @@ -14,11 +14,13 @@ final class ShortenUrlController { - private ContainerInterface $container; + private array $config; + private PDO $db; public function __construct(ContainerInterface $container) { - $this->container = $container; + $this->db = $container->get('db'); + $this->config = $container->get('config'); } public function __invoke(Request $request, Response $response, array $args): Response @@ -64,9 +66,7 @@ public function __invoke(Request $request, Response $response, array $args): Res return $newResponse; } - /** @var PDO $db */ - $db = $this->container->get('db'); - $stmt = $db->prepare(trim(" + $stmt = $this->db->prepare(trim(" INSERT INTO `urls` (`id`, `uuid`, `long_url`, `short_url_path`, `created_at`) VALUES (:id, :uuid, :long_url, :short_url_path, :created_at) ")); @@ -91,7 +91,7 @@ public function __invoke(Request $request, Response $response, array $args): Res 'status' => 'success', 'data' => [ 'huge' => $contents['huge_url'], - 'shortened' => $this->container->get('config')['baseUrl'] . '/' . $shortUrlPath, + 'shortened' => $this->config['baseUrl'] . '/' . $shortUrlPath, 'created_at' => $createdAt->format(DateTimeInterface::ATOM) ] ])); diff --git a/templates/index.html.twig b/templates/index.html.twig index 824947d..611edfc 100644 --- a/templates/index.html.twig +++ b/templates/index.html.twig @@ -39,7 +39,7 @@
-

Mais de 200 URL's encurtadas e mais de 300 cliques.

+

Mais de {{ totalUrls }} URL's encurtadas e mais de {{ totalClicks }} cliques.