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