Added logging number of hits

This commit is contained in:
SinTan1729 2022-11-03 16:53:04 -05:00
parent 0c101a55bf
commit b0e7f1ee57
4 changed files with 23 additions and 6 deletions

View File

@ -48,7 +48,7 @@ public class Routes {
res.status(404);
return "";
}
urlRepository.addHit(shortUrl);
res.redirect(longUrlOpt.get(), HttpStatus.PERMANENT_REDIRECT_308);
return "";

View File

@ -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<String> 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<String> findForShortUrl(String shortUrl) {
try (final var con = DriverManager.getConnection(databaseUrl)) {
final var stmt = con.prepareStatement(SELECT_FOR_SHORT_SQL);

View File

@ -58,8 +58,9 @@
<table class="pure-table">
<thead>
<tr>
<td>Short url</td>
<td>Short URL</td>
<td>Long URL</td>
<td>Hits</td>
<td></td>
</tr>
</thead>

View File

@ -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;