Skip to content

Commit

Permalink
adding economy rate in shortened url
Browse files Browse the repository at this point in the history
  • Loading branch information
dersonsena committed Sep 19, 2021
1 parent eb06f14 commit 81e6567
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
8 changes: 7 additions & 1 deletion public_html/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ div.shortened-url-result button.copy-button {
width: 100%;
}

div.shortened-url-result a {
div.shortened-url-result a span.url-text {
font-size: 1.7em;
}

div.shortened-url-result span.badge {
position: absolute;
margin: -1.6em 1em 1em -3.1em;
font-size: 0.9em;
}
4 changes: 3 additions & 1 deletion public_html/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ $(document).ready(function () {

const $divResult = $('div.shortened-url-result');
$divResult.css('display', 'flex');
$divResult.find('a').attr('href', payload.data.shortened).html(payload.data.shortened);
$divResult.find('a').attr('href', payload.data.shortened);
$divResult.find('a span.url-text').html(payload.data.shortened);
$divResult.find('a span.badge').html(`-${payload.data.economyRate}%`);
$divResult.find('button').attr('data-url', payload.data.shortened);
}).fail(function(jqXHR, textStatus, msg) {
if (jqXHR.status === 400) {
Expand Down
12 changes: 8 additions & 4 deletions src/Controllers/ShortenUrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public function __invoke(Request $request, Response $response, array $args): Res
}

$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)
INSERT INTO `urls` (`id`, `uuid`, `long_url`, `short_url_path`, `economy_rate`, `created_at`)
VALUES (:id, :uuid, :long_url, :short_url_path, :economy_rate, :created_at)
"));

$shortUrlPath = substr(sha1(uniqid((string)rand(), true)), 0, 5);
Expand All @@ -86,14 +86,17 @@ public function __invoke(Request $request, Response $response, array $args): Res
break;
}

$fullShortenUrl = $this->config['baseUrl'] . '/' . $shortUrlPath;
$uuid = Uuid::uuid4();
$createdAt = new DateTimeImmutable();
$economyRate = ceil(100 - ((strlen($fullShortenUrl) * 100) / strlen($contents['huge_url'])));

$stmt->execute([
'id' => $uuid->getBytes(),
'uuid' => $uuid->toString(),
'long_url' => $contents['huge_url'],
'short_url_path' => $shortUrlPath,
'economy_rate' => $economyRate,
'created_at' => $createdAt->format('Y-m-d H:i:s')
]);

Expand All @@ -105,8 +108,9 @@ public function __invoke(Request $request, Response $response, array $args): Res
'status' => 'success',
'data' => [
'huge' => $contents['huge_url'],
'shortened' => $this->config['baseUrl'] . '/' . $shortUrlPath,
'created_at' => $createdAt->format(DateTimeInterface::ATOM)
'shortened' => $fullShortenUrl,
'created_at' => $createdAt->format(DateTimeInterface::ATOM),
'economyRate' => $economyRate
]
]));

Expand Down
5 changes: 4 additions & 1 deletion templates/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
</div>
</form>
<div class="shortened-url-result">
<a href="#" target="_blank">&nbsp;</a>
<a href="#" target="_blank">
<span class="url-text">&nbsp;</span>
<span class="badge rounded-pill bg-success">&nbsp;</span>
</a>
<button data-url="https://glo.bo/2VE6uLN" class="btn btn-light copy-button">
<i class="far fa-copy"></i> Copiar
</button>
Expand Down

0 comments on commit 81e6567

Please sign in to comment.