Skip to content

Commit 955ba5f

Browse files
committed
fix flash messages
1 parent 20fee34 commit 955ba5f

File tree

9 files changed

+84
-37
lines changed

9 files changed

+84
-37
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dependencies {
3737
implementation("com.zaxxer:HikariCP:6.3.0")
3838
implementation("org.postgresql:postgresql:42.7.7")
3939
implementation("com.konghq:unirest-java-core:4.4.7")
40+
implementation("org.jsoup:jsoup:1.21.1")
4041

4142
compileOnly("org.projectlombok:lombok:$lombokVersion")
4243
annotationProcessor("org.projectlombok:lombok:$lombokVersion")

app/src/main/java/hexlet/code/controller/UrlChecksController.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
import hexlet.code.util.NamedRoutes;
77
import io.javalin.http.Context;
88
import io.javalin.http.NotFoundResponse;
9+
import kong.unirest.core.HttpResponse;
910
import kong.unirest.core.Unirest;
11+
import org.jsoup.Jsoup;
12+
import org.jsoup.nodes.Document;
13+
import org.jsoup.nodes.Element;
1014

1115
import java.sql.SQLException;
1216

@@ -15,12 +19,24 @@ public static void create(Context ctx) throws SQLException {
1519

1620
var urlId = ctx.pathParamAsClass("id", Long.class).get();
1721
var url = UrlRepository.find(urlId).orElseThrow(NotFoundResponse::new);
18-
var status = Unirest.get(url.getName())
19-
.asString()
20-
.getStatus();
21-
var check = new UrlCheck(status, urlId);
2222

23+
HttpResponse<String> response = Unirest.get(url.getName()).asString();
24+
var status = response.getStatus();
25+
26+
Document body = Jsoup.parse(response.getBody());
27+
var title = body.title();
28+
29+
Element h1Element = body.selectFirst("h1");
30+
var h1 = h1Element != null ? h1Element.text() : null;
31+
32+
Element descriptionElement = body.selectFirst("meta[name=description]");
33+
var description = descriptionElement != null ? descriptionElement.attr("content") : null;
34+
35+
var check = new UrlCheck(status, title, h1, description, urlId);
2336
UrlCheckRepository.save(check);
37+
38+
ctx.sessionAttribute("flash", "Страница успешно проверена");
39+
ctx.sessionAttribute("flash-type", "alert alert-success");
2440
ctx.redirect(NamedRoutes.urlPath(urlId));
2541
}
2642
}

app/src/main/java/hexlet/code/controller/UrlsController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static void index(Context ctx) throws SQLException {
2929
}
3030

3131
var page = new UrlsPage(urls, latestChecks);
32+
3233
page.setFlash(ctx.consumeSessionAttribute("flash"));
3334
page.setFlashType(ctx.consumeSessionAttribute("flash-type"));
3435
ctx.render("urls/index.jte", model("page", page));
@@ -42,6 +43,9 @@ public static void show(Context ctx) throws SQLException {
4243
.toList();
4344

4445
var page = new UrlPage(url, checks);
46+
47+
page.setFlash(ctx.consumeSessionAttribute("flash"));
48+
page.setFlashType(ctx.consumeSessionAttribute("flash-type"));
4549
ctx.render("urls/show.jte", model("page", page));
4650
}
4751

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
package hexlet.code.model;
22

3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
35
import lombok.Getter;
46
import lombok.Setter;
57

6-
import java.sql.Timestamp;
8+
import java.time.LocalDateTime;
79

810
@Getter
911
@Setter
12+
@Builder
13+
@AllArgsConstructor
1014
public class Url {
1115
private Long id;
1216
private String name;
13-
private Timestamp createdAt;
17+
private LocalDateTime createdAt;
1418

1519
public Url(String name) {
1620
this.name = name;
17-
this.createdAt = new Timestamp(System.currentTimeMillis());
18-
}
19-
20-
public Url(Long id, String name, Timestamp createdAt) {
21-
this.id = id;
22-
this.name = name;
23-
this.createdAt = createdAt;
21+
this.createdAt = LocalDateTime.now();
2422
}
2523
}

app/src/main/java/hexlet/code/model/UrlCheck.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,29 @@
55
import lombok.Getter;
66
import lombok.Setter;
77

8-
import java.sql.Timestamp;
8+
import java.time.LocalDateTime;
99

1010
@Getter
1111
@Setter
12-
@AllArgsConstructor
1312
@Builder
13+
@AllArgsConstructor
1414
public class UrlCheck {
1515
private Long id;
1616
private Integer statusCode;
1717
private String title;
1818
private String h1;
1919
private String description;
2020
private Long urlId;
21-
private Timestamp createdAt;
21+
private LocalDateTime createdAt;
2222

2323
private Url url;
2424

25-
public UrlCheck(Integer statusCode, Long urlId) {
25+
public UrlCheck(Integer statusCode, String title, String h1, String description, Long urlId) {
2626
this.statusCode = statusCode;
2727
this.urlId = urlId;
28-
this.createdAt = new Timestamp(System.currentTimeMillis());
28+
this.title = title;
29+
this.h1 = h1;
30+
this.description = description;
31+
this.createdAt = LocalDateTime.now();
2932
}
3033
}

app/src/main/java/hexlet/code/repository/UrlCheckRepository.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,28 @@
44

55
import java.sql.SQLException;
66
import java.sql.Statement;
7+
import java.sql.Timestamp;
78
import java.util.ArrayList;
89
import java.util.List;
910

1011
public class UrlCheckRepository extends BaseRepository {
1112

1213
public static void save(UrlCheck check) throws SQLException {
13-
var sql = "INSERT INTO url_checks (status_code, url_id, created_at) VALUES (?, ?, ?)";
14+
var sql = """
15+
INSERT INTO url_checks
16+
(status_code, title, h1, description, url_id, created_at)
17+
VALUES
18+
(?, ?, ?, ?, ?, ?)
19+
""";
1420
try (var conn = dataSource.getConnection();
1521
var stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
1622
) {
1723
stmt.setInt(1, check.getStatusCode());
18-
stmt.setLong(2, check.getUrlId());
19-
stmt.setTimestamp(3, check.getCreatedAt());
24+
stmt.setString(2, check.getTitle());
25+
stmt.setString(3, check.getH1());
26+
stmt.setString(4, check.getDescription());
27+
stmt.setLong(5, check.getUrlId());
28+
stmt.setTimestamp(6, Timestamp.valueOf(check.getCreatedAt()));
2029
stmt.executeUpdate();
2130

2231
var keys = stmt.getGeneratedKeys();
@@ -63,8 +72,11 @@ public static List<UrlCheck> getEntities() throws SQLException {
6372
var check = UrlCheck.builder()
6473
.id(rs.getLong("id"))
6574
.statusCode(rs.getInt("status_code"))
75+
.title(rs.getString("title"))
76+
.h1(rs.getString("h1"))
77+
.description(rs.getString("description"))
6678
.urlId(rs.getLong("url_id"))
67-
.createdAt(rs.getTimestamp("created_at"))
79+
.createdAt(rs.getTimestamp("created_at").toLocalDateTime())
6880
.build();
6981
result.add(check);
7082
}

app/src/main/java/hexlet/code/repository/UrlRepository.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.sql.SQLException;
66
import java.sql.Statement;
7+
import java.sql.Timestamp;
78
import java.util.ArrayList;
89
import java.util.List;
910
import java.util.Optional;
@@ -16,7 +17,7 @@ public static void save(Url url) throws SQLException {
1617
var stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
1718
) {
1819
stmt.setString(1, url.getName());
19-
stmt.setTimestamp(2, url.getCreatedAt());
20+
stmt.setTimestamp(2, Timestamp.valueOf(url.getCreatedAt()));
2021
stmt.executeUpdate();
2122

2223
var keys = stmt.getGeneratedKeys();
@@ -38,9 +39,11 @@ public static Optional<Url> find(Long id) throws SQLException {
3839

3940
var rs = stmt.executeQuery();
4041
if (rs.next()) {
41-
var name = rs.getString("name");
42-
var createdAt = rs.getTimestamp("created_at");
43-
var url = new Url(id, name, createdAt);
42+
var url = Url.builder()
43+
.id(id)
44+
.name(rs.getString("name"))
45+
.createdAt(rs.getTimestamp("created_at").toLocalDateTime())
46+
.build();
4447
return Optional.of(url);
4548
} else {
4649
return Optional.empty();
@@ -58,9 +61,11 @@ public static Optional<Url> findByName(String name) throws SQLException {
5861

5962
var rs = stmt.executeQuery();
6063
if (rs.next()) {
61-
var id = rs.getLong("id");
62-
var createdAt = rs.getTimestamp("created_at");
63-
var url = new Url(id, name, createdAt);
64+
var url = Url.builder()
65+
.id(rs.getLong("id"))
66+
.name(name)
67+
.createdAt(rs.getTimestamp("created_at").toLocalDateTime())
68+
.build();
6469
return Optional.of(url);
6570
} else {
6671
return Optional.empty();
@@ -77,11 +82,11 @@ public static List<Url> getEntities() throws SQLException {
7782
var rs = stmt.executeQuery();
7883
var result = new ArrayList<Url>();
7984
while (rs.next()) {
80-
var id = rs.getLong("id");
81-
var name = rs.getString("name");
82-
var createdAt = rs.getTimestamp("created_at");
83-
var url = new Url(id, name, createdAt);
84-
url.setId(id);
85+
var url = Url.builder()
86+
.id(rs.getLong("id"))
87+
.name(rs.getString("name"))
88+
.createdAt(rs.getTimestamp("created_at").toLocalDateTime())
89+
.build();
8590
result.add(url);
8691
}
8792

app/src/main/resources/templates/urls/index.jte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@import hexlet.code.util.NamedRoutes
22
@import hexlet.code.dto.urls.UrlsPage
3+
@import java.time.format.DateTimeFormatter
34
@param UrlsPage page
45

56
@template.layout.page(
@@ -25,7 +26,7 @@ content = @`
2526
<td>${url.getId()}</td>
2627
<td><a href="${NamedRoutes.urlPath(url.getId())}">${url.getName()}</a></td>
2728
@if (page.getLatestChecks() != null && page.getLatestChecks().containsKey(url.getId()))
28-
<td>${String.valueOf(page.getLatestChecks().get(url.getId()).getCreatedAt())}</td>
29+
<td>${DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm").format(page.getLatestChecks().get(url.getId()).getCreatedAt())}</td>
2930
<td>${page.getLatestChecks().get(url.getId()).getStatusCode()}</td>
3031
@endif
3132
</tr>

app/src/main/resources/templates/urls/show.jte

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@import hexlet.code.dto.urls.UrlPage
22
@import hexlet.code.util.NamedRoutes
3+
@import java.time.format.DateTimeFormatter
34
@param UrlPage page
45

56
@template.layout.page(
@@ -19,7 +20,7 @@ content = @`
1920
</tr>
2021
<tr>
2122
<td>Дата создания</td>
22-
<td>${String.valueOf(page.getUrl().getCreatedAt())}</td>
23+
<td>${DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm").format(page.getUrl().getCreatedAt())}</td>
2324
</tr>
2425
</table>
2526
<h2>Проверки</h2>
@@ -32,6 +33,9 @@ content = @`
3233
<tr>
3334
<th>ID</th>
3435
<th>Код ответа</th>
36+
<th>&lt;title&gt;</th>
37+
<th>&lt;h1&gt;</th>
38+
<th>&lt;description&gt;</th>
3539
<th>Дата проверки</th>
3640
</tr>
3741
</thead>
@@ -40,7 +44,10 @@ content = @`
4044
<tr>
4145
<th>${check.getId()}</th>
4246
<th>${check.getStatusCode()}</th>
43-
<th>${String.valueOf(check.getCreatedAt())}</th>
47+
<th>${check.getTitle()}</th>
48+
<th>${check.getH1()}</th>
49+
<th>${check.getDescription()}</th>
50+
<th>${DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm").format(check.getCreatedAt())}</th>
4451
</tr>
4552
@endfor
4653
</tbody>

0 commit comments

Comments
 (0)