Skip to content

Commit 1fa0c45

Browse files
committed
change the way of finding checks by url id
1 parent 5cf965e commit 1fa0c45

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import hexlet.code.dto.urls.UrlPage;
44
import hexlet.code.dto.urls.UrlsPage;
55
import hexlet.code.model.Url;
6-
import hexlet.code.model.UrlCheck;
76
import hexlet.code.repository.UrlCheckRepository;
87
import hexlet.code.repository.UrlRepository;
98
import hexlet.code.util.NamedRoutes;
@@ -33,9 +32,7 @@ public static void index(Context ctx) throws SQLException {
3332
public static void show(Context ctx) throws SQLException {
3433
var id = ctx.pathParamAsClass("id", Long.class).get();
3534
var url = UrlRepository.find(id).orElseThrow(NotFoundResponse::new);
36-
var checks = UrlCheckRepository.getEntities().stream()
37-
.filter(check -> check.getUrlId().equals(id))
38-
.toList();
35+
var checks = UrlCheckRepository.findByUrlId(id);
3936

4037
var page = new UrlPage(url, checks);
4138

@@ -44,7 +41,7 @@ public static void show(Context ctx) throws SQLException {
4441
ctx.render("urls/show.jte", model("page", page));
4542
}
4643

47-
public static void create(Context ctx) throws SQLException, URISyntaxException {
44+
public static void create(Context ctx) throws SQLException {
4845

4946
try {
5047
var urlName = normalizeUrlName(ctx.formParam("url"));

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

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,16 @@ public static void save(UrlCheck check) throws SQLException {
6262
// }
6363
// }
6464

65-
public static Map<Long, UrlCheck> getLatestChecks() throws SQLException {
66-
var sql = """
67-
SELECT DISTINCT ON (url_id) *
68-
FROM url_checks ORDER BY url_id DESC, id DESC
69-
""";
65+
public static List<UrlCheck> findByUrlId(Long urlId) throws SQLException {
66+
var sql = "SELECT * FROM url_checks WHERE url_id = ? ORDER BY id";
7067
try (
7168
var conn = dataSource.getConnection();
7269
var stmt = conn.prepareStatement(sql)
7370
) {
71+
stmt.setLong(1, urlId);
7472
var rs = stmt.executeQuery();
75-
var result = new HashMap<Long, UrlCheck>();
73+
var result = new ArrayList<UrlCheck>();
7674
while (rs.next()) {
77-
var urlId = rs.getLong("url_id");
7875
var check = UrlCheck.builder()
7976
.id(rs.getLong("id"))
8077
.statusCode(rs.getInt("status_code"))
@@ -84,35 +81,64 @@ SELECT DISTINCT ON (url_id) *
8481
.urlId(urlId)
8582
.createdAt(rs.getTimestamp("created_at").toLocalDateTime())
8683
.build();
87-
result.put(urlId, check);
84+
result.add(check);
8885
}
8986

9087
return result;
9188
}
9289
}
9390

94-
public static List<UrlCheck> getEntities() throws SQLException {
95-
var sql = "SELECT * FROM url_checks";
91+
public static Map<Long, UrlCheck> getLatestChecks() throws SQLException {
92+
var sql = """
93+
SELECT DISTINCT ON (url_id) *
94+
FROM url_checks ORDER BY url_id DESC, id DESC
95+
""";
9696
try (
9797
var conn = dataSource.getConnection();
9898
var stmt = conn.prepareStatement(sql)
9999
) {
100100
var rs = stmt.executeQuery();
101-
var result = new ArrayList<UrlCheck>();
101+
var result = new HashMap<Long, UrlCheck>();
102102
while (rs.next()) {
103+
var urlId = rs.getLong("url_id");
103104
var check = UrlCheck.builder()
104105
.id(rs.getLong("id"))
105106
.statusCode(rs.getInt("status_code"))
106107
.title(rs.getString("title"))
107108
.h1(rs.getString("h1"))
108109
.description(rs.getString("description"))
109-
.urlId(rs.getLong("url_id"))
110+
.urlId(urlId)
110111
.createdAt(rs.getTimestamp("created_at").toLocalDateTime())
111112
.build();
112-
result.add(check);
113+
result.put(urlId, check);
113114
}
114115

115116
return result;
116117
}
117118
}
119+
120+
// public static List<UrlCheck> getEntities() throws SQLException {
121+
// var sql = "SELECT * FROM url_checks";
122+
// try (
123+
// var conn = dataSource.getConnection();
124+
// var stmt = conn.prepareStatement(sql)
125+
// ) {
126+
// var rs = stmt.executeQuery();
127+
// var result = new ArrayList<UrlCheck>();
128+
// while (rs.next()) {
129+
// var check = UrlCheck.builder()
130+
// .id(rs.getLong("id"))
131+
// .statusCode(rs.getInt("status_code"))
132+
// .title(rs.getString("title"))
133+
// .h1(rs.getString("h1"))
134+
// .description(rs.getString("description"))
135+
// .urlId(rs.getLong("url_id"))
136+
// .createdAt(rs.getTimestamp("created_at").toLocalDateTime())
137+
// .build();
138+
// result.add(check);
139+
// }
140+
//
141+
// return result;
142+
// }
143+
// }
118144
}

0 commit comments

Comments
 (0)