Skip to content

Commit 81e6567

Browse files
committed
adding economy rate in shortened url
1 parent eb06f14 commit 81e6567

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

public_html/css/style.css

+7-1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ div.shortened-url-result button.copy-button {
149149
width: 100%;
150150
}
151151

152-
div.shortened-url-result a {
152+
div.shortened-url-result a span.url-text {
153153
font-size: 1.7em;
154+
}
155+
156+
div.shortened-url-result span.badge {
157+
position: absolute;
158+
margin: -1.6em 1em 1em -3.1em;
159+
font-size: 0.9em;
154160
}

public_html/js/script.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ $(document).ready(function () {
8585

8686
const $divResult = $('div.shortened-url-result');
8787
$divResult.css('display', 'flex');
88-
$divResult.find('a').attr('href', payload.data.shortened).html(payload.data.shortened);
88+
$divResult.find('a').attr('href', payload.data.shortened);
89+
$divResult.find('a span.url-text').html(payload.data.shortened);
90+
$divResult.find('a span.badge').html(`-${payload.data.economyRate}%`);
8991
$divResult.find('button').attr('data-url', payload.data.shortened);
9092
}).fail(function(jqXHR, textStatus, msg) {
9193
if (jqXHR.status === 400) {

src/Controllers/ShortenUrlController.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public function __invoke(Request $request, Response $response, array $args): Res
6767
}
6868

6969
$stmt = $this->db->prepare(trim("
70-
INSERT INTO `urls` (`id`, `uuid`, `long_url`, `short_url_path`, `created_at`)
71-
VALUES (:id, :uuid, :long_url, :short_url_path, :created_at)
70+
INSERT INTO `urls` (`id`, `uuid`, `long_url`, `short_url_path`, `economy_rate`, `created_at`)
71+
VALUES (:id, :uuid, :long_url, :short_url_path, :economy_rate, :created_at)
7272
"));
7373

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

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

9294
$stmt->execute([
9395
'id' => $uuid->getBytes(),
9496
'uuid' => $uuid->toString(),
9597
'long_url' => $contents['huge_url'],
9698
'short_url_path' => $shortUrlPath,
99+
'economy_rate' => $economyRate,
97100
'created_at' => $createdAt->format('Y-m-d H:i:s')
98101
]);
99102

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

templates/index.html.twig

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
</div>
2929
</form>
3030
<div class="shortened-url-result">
31-
<a href="#" target="_blank">&nbsp;</a>
31+
<a href="#" target="_blank">
32+
<span class="url-text">&nbsp;</span>
33+
<span class="badge rounded-pill bg-success">&nbsp;</span>
34+
</a>
3235
<button data-url="https://glo.bo/2VE6uLN" class="btn btn-light copy-button">
3336
<i class="far fa-copy"></i> Copiar
3437
</button>

0 commit comments

Comments
 (0)