mirror of
https://github.com/SinTan1729/chhoto-url
synced 2024-12-26 23:58:35 -06:00
Added logging number of hits
This commit is contained in:
parent
0c101a55bf
commit
b0e7f1ee57
4 changed files with 23 additions and 6 deletions
|
@ -48,7 +48,7 @@ public class Routes {
|
||||||
res.status(404);
|
res.status(404);
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
urlRepository.addHit(shortUrl);
|
||||||
res.redirect(longUrlOpt.get(), HttpStatus.PERMANENT_REDIRECT_308);
|
res.redirect(longUrlOpt.get(), HttpStatus.PERMANENT_REDIRECT_308);
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
|
|
|
@ -7,12 +7,14 @@ import java.util.Optional;
|
||||||
|
|
||||||
|
|
||||||
public class UrlRepository {
|
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" +
|
private static final String CREATE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS urls\n" +
|
||||||
"(\n" +
|
"(\n" +
|
||||||
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
|
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
|
||||||
" long_url TEXT NOT NULL,\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 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 = ?";
|
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<>();
|
List<String> result = new ArrayList<>();
|
||||||
|
|
||||||
while (rs.next()) {
|
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;
|
return result;
|
||||||
|
@ -66,6 +68,7 @@ public class UrlRepository {
|
||||||
final var stmt = con.prepareStatement(INSERT_ROW_SQL);
|
final var stmt = con.prepareStatement(INSERT_ROW_SQL);
|
||||||
stmt.setString(1, longURL);
|
stmt.setString(1, longURL);
|
||||||
stmt.setString(2, shortUrl);
|
stmt.setString(2, shortUrl);
|
||||||
|
stmt.setInt(3, 0);
|
||||||
if (stmt.execute()) {
|
if (stmt.execute()) {
|
||||||
return String.format("%s,%s", shortUrl, longURL);
|
return String.format("%s,%s", shortUrl, longURL);
|
||||||
}
|
}
|
||||||
|
@ -75,6 +78,16 @@ public class UrlRepository {
|
||||||
return "";
|
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) {
|
public Optional<String> findForShortUrl(String shortUrl) {
|
||||||
try (final var con = DriverManager.getConnection(databaseUrl)) {
|
try (final var con = DriverManager.getConnection(databaseUrl)) {
|
||||||
final var stmt = con.prepareStatement(SELECT_FOR_SHORT_SQL);
|
final var stmt = con.prepareStatement(SELECT_FOR_SHORT_SQL);
|
||||||
|
|
|
@ -58,8 +58,9 @@
|
||||||
<table class="pure-table">
|
<table class="pure-table">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Short url</td>
|
<td>Short URL</td>
|
||||||
<td>Long URL</td>
|
<td>Long URL</td>
|
||||||
|
<td>Hits</td>
|
||||||
<td></td>
|
<td></td>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
|
|
|
@ -5,8 +5,9 @@ const refreshData = async () => {
|
||||||
.filter(line => line !== "")
|
.filter(line => line !== "")
|
||||||
.map(line => line.split(","))
|
.map(line => line.split(","))
|
||||||
.map(arr => ({
|
.map(arr => ({
|
||||||
|
short: arr[0],
|
||||||
long: arr[1],
|
long: arr[1],
|
||||||
short: arr[0]
|
hits: arr[2]
|
||||||
}));
|
}));
|
||||||
|
|
||||||
displayData(data);
|
displayData(data);
|
||||||
|
@ -23,10 +24,12 @@ const TR = (row) => {
|
||||||
const tr = document.createElement("tr");
|
const tr = document.createElement("tr");
|
||||||
const longTD = TD(A(row.long));
|
const longTD = TD(A(row.long));
|
||||||
const shortTD = TD(A_INT(row.short));
|
const shortTD = TD(A_INT(row.short));
|
||||||
|
const hitsTD = TD(row.hits);
|
||||||
const btn = deleteButton(row.short);
|
const btn = deleteButton(row.short);
|
||||||
|
|
||||||
tr.appendChild(shortTD);
|
tr.appendChild(shortTD);
|
||||||
tr.appendChild(longTD);
|
tr.appendChild(longTD);
|
||||||
|
tr.appendChild(hitsTD);
|
||||||
tr.appendChild(btn);
|
tr.appendChild(btn);
|
||||||
|
|
||||||
return tr;
|
return tr;
|
||||||
|
|
Loading…
Reference in a new issue