Skip to content

Commit

Permalink
adding total clicks and urls
Browse files Browse the repository at this point in the history
  • Loading branch information
dersonsena committed Sep 19, 2021
1 parent b0284c8 commit cfef863
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 20 deletions.
8 changes: 2 additions & 6 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions public_html/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
15 changes: 8 additions & 7 deletions src/Controllers/AccessUrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
41 changes: 41 additions & 0 deletions src/Controllers/HomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace App\Controllers;

use PDO;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Views\Twig;

final class HomeController
{
private PDO $db;
private Twig $view;

public function __construct(ContainerInterface $container)
{
$this->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)
]);
}
}
12 changes: 6 additions & 6 deletions src/Controllers/ShortenUrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
"));
Expand All @@ -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)
]
]));
Expand Down
2 changes: 1 addition & 1 deletion templates/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<div class="row">
<div class="col-md-12 statistics">
<script type='text/javascript' src='https://storage.ko-fi.com/cdn/widget/Widget_2.js'></script><script type='text/javascript'>kofiwidget2.init('Make a donation', '#29abe0', 'S6S1673T9');kofiwidget2.draw();</script>
<p class="mt-3"><i class="fas fa-fire"></i> Mais de <strong>200</strong> URL's encurtadas e mais de <strong>300</strong> cliques.</p>
<p class="mt-3"><i class="fas fa-fire"></i> Mais de <strong>{{ totalUrls }}</strong> URL's encurtadas e mais de <strong>{{ totalClicks }}</strong> cliques.</p>
</div>
</div>
<div class="row history-wrapper">
Expand Down

0 comments on commit cfef863

Please sign in to comment.