From b0e7f1ee5724cbe3f5433a5af359e0801ec71bf4 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Thu, 3 Nov 2022 16:53:04 -0500 Subject: [PATCH] Added logging number of hits --- src/main/java/tk/draganczuk/url/Routes.java | 2 +- .../java/tk/draganczuk/url/UrlRepository.java | 19 ++++++++++++++++--- src/main/resources/public/index.html | 3 ++- src/main/resources/public/js/main.js | 5 ++++- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/main/java/tk/draganczuk/url/Routes.java b/src/main/java/tk/draganczuk/url/Routes.java index 8a6dd99..5ce2c2d 100644 --- a/src/main/java/tk/draganczuk/url/Routes.java +++ b/src/main/java/tk/draganczuk/url/Routes.java @@ -48,7 +48,7 @@ public class Routes { res.status(404); return ""; } - + urlRepository.addHit(shortUrl); res.redirect(longUrlOpt.get(), HttpStatus.PERMANENT_REDIRECT_308); return ""; diff --git a/src/main/java/tk/draganczuk/url/UrlRepository.java b/src/main/java/tk/draganczuk/url/UrlRepository.java index b4b3b06..cc87503 100644 --- a/src/main/java/tk/draganczuk/url/UrlRepository.java +++ b/src/main/java/tk/draganczuk/url/UrlRepository.java @@ -7,12 +7,14 @@ import java.util.Optional; public class UrlRepository { - private static final String INSERT_ROW_SQL = "INSERT INTO urls (long_url, short_url) VALUES (?, ?)"; + private static final String INSERT_ROW_SQL = "INSERT INTO urls (long_url, short_url, hits) VALUES (?, ?, ?)"; + private static final String ADD_HIT_SQL = "UPDATE urls SET hits = hits + 1 WHERE short_url = ?"; private static final String CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS urls\n" + "(\n" + " id INTEGER PRIMARY KEY AUTOINCREMENT,\n" + " long_url TEXT NOT NULL,\n" + - " short_url TEXT NOT NULL\n" + + " short_url TEXT NOT NULL,\n" + + " hits INTEGER NOT NULL\n" + ");"; private static final String SELECT_FOR_SHORT_SQL = "SELECT long_url FROM urls WHERE short_url = ?"; private static final String DELETE_ROW_SQL = "DELETE FROM urls WHERE short_url = ?"; @@ -50,7 +52,7 @@ public class UrlRepository { List result = new ArrayList<>(); while (rs.next()) { - result.add(String.format("%s,%s", rs.getString("short_url"), rs.getString("long_url"))); + result.add(String.format("%s,%s,%s", rs.getString("short_url"), rs.getString("long_url"), String.valueOf(rs.getInt("hits")))); } return result; @@ -66,6 +68,7 @@ public class UrlRepository { final var stmt = con.prepareStatement(INSERT_ROW_SQL); stmt.setString(1, longURL); stmt.setString(2, shortUrl); + stmt.setInt(3, 0); if (stmt.execute()) { return String.format("%s,%s", shortUrl, longURL); } @@ -75,6 +78,16 @@ public class UrlRepository { return ""; } + public void addHit(String shortURL) { + try (final var con = DriverManager.getConnection(databaseUrl)) { + final var stmt = con.prepareStatement(ADD_HIT_SQL); + stmt.setString(1, shortURL); + stmt.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + public Optional findForShortUrl(String shortUrl) { try (final var con = DriverManager.getConnection(databaseUrl)) { final var stmt = con.prepareStatement(SELECT_FOR_SHORT_SQL); diff --git a/src/main/resources/public/index.html b/src/main/resources/public/index.html index efe57b7..30a8d3e 100644 --- a/src/main/resources/public/index.html +++ b/src/main/resources/public/index.html @@ -58,8 +58,9 @@ - + + diff --git a/src/main/resources/public/js/main.js b/src/main/resources/public/js/main.js index 9db802c..c9a8190 100644 --- a/src/main/resources/public/js/main.js +++ b/src/main/resources/public/js/main.js @@ -5,8 +5,9 @@ const refreshData = async () => { .filter(line => line !== "") .map(line => line.split(",")) .map(arr => ({ + short: arr[0], long: arr[1], - short: arr[0] + hits: arr[2] })); displayData(data); @@ -23,10 +24,12 @@ const TR = (row) => { const tr = document.createElement("tr"); const longTD = TD(A(row.long)); const shortTD = TD(A_INT(row.short)); + const hitsTD = TD(row.hits); const btn = deleteButton(row.short); tr.appendChild(shortTD); tr.appendChild(longTD); + tr.appendChild(hitsTD); tr.appendChild(btn); return tr;
Short urlShort URL Long URLHits