22
33import hexlet .code .model .UrlCheck ;
44
5+ import java .sql .ResultSet ;
56import java .sql .SQLException ;
67import java .sql .Statement ;
78import java .sql .Timestamp ;
@@ -29,7 +30,7 @@ public static void save(UrlCheck urlCheck) throws SQLException {
2930 preparedStatement .setString (3 , urlCheck .getH1 ());
3031 preparedStatement .setString (4 , urlCheck .getTitle ());
3132 preparedStatement .setString (5 , urlCheck .getDescription ());
32- preparedStatement .setTimestamp (6 , Timestamp .valueOf (urlCheck . getCreatedAt ()));
33+ preparedStatement .setTimestamp (6 , Timestamp .valueOf (LocalDateTime . now ()));
3334 preparedStatement .executeUpdate ();
3435
3536 var generatedKeys = preparedStatement .getGeneratedKeys ();
@@ -50,18 +51,8 @@ public static List<UrlCheck> find(Long urlId) throws SQLException {
5051 var resultSet = stmt .executeQuery ();
5152
5253 var results = new ArrayList <UrlCheck >();
53- var currentListId = 1L ;
54-
5554 while (resultSet .next ()) {
56- var statusCode = resultSet .getInt ("status_code" );
57- var h1 = resultSet .getString ("h1" );
58- var title = resultSet .getString ("title" );
59- var description = resultSet .getString ("description" );
60- var createdAt = resultSet .getTimestamp ("created_at" ).toLocalDateTime ();
61- var urlCheck = new UrlCheck (statusCode , h1 , title , description , createdAt );
62- urlCheck .setId (currentListId );
63-
64- currentListId ++;
55+ var urlCheck = mapRowToUrlCheck (resultSet );
6556 results .add (urlCheck );
6657 }
6758 return results ;
@@ -77,9 +68,7 @@ public static Optional<UrlCheck> findLatestCheck(Long urlId) throws SQLException
7768 var resultSet = stmt .executeQuery ();
7869
7970 if (resultSet .next ()) {
80- var statusCode = resultSet .getInt ("status_code" );
81- var createdAt = resultSet .getTimestamp ("created_at" ).toLocalDateTime ();
82- var check = new UrlCheck (statusCode , createdAt );
71+ var check = mapRowToUrlCheck (resultSet );
8372 return Optional .of (check );
8473 }
8574 return Optional .empty ();
@@ -102,20 +91,24 @@ public static Map<Long, UrlCheck> findLatestChecksByUrlIds(List<Long> urlIds) th
10291 var resultSet = stmt .executeQuery ();
10392 var result = new HashMap <Long , UrlCheck >();
10493 while (resultSet .next ()) {
105- var id = resultSet .getLong ("id" );
106- var urlId = resultSet .getLong ("url_id" );
107- var statusCode = resultSet .getInt ("status_code" );
108- var h1 = resultSet .getString ("h1" );
109- var title = resultSet .getString ("title" );
110- var description = resultSet .getString ("description" );
111- var createdAt = resultSet .getTimestamp ("created_at" ).toLocalDateTime ();
112- var check = new UrlCheck (statusCode , h1 , title , description , createdAt );
113- check .setId (id );
114- check .setUrlId (urlId );
115- check .setCreatedAt (createdAt );
116- result .put (urlId , check );
94+ var check = mapRowToUrlCheck (resultSet );
95+ Long urlIdFromDb = check .getUrlId ();
96+ result .put (check .getUrlId (), check );
11797 }
11898 return result ;
11999 }
120100 }
101+
102+ private static UrlCheck mapRowToUrlCheck (ResultSet rs ) throws SQLException {
103+ var check = new UrlCheck (
104+ rs .getInt ("status_code" ),
105+ rs .getString ("h1" ),
106+ rs .getString ("title" ),
107+ rs .getString ("description" ),
108+ rs .getTimestamp ("created_at" ).toLocalDateTime ()
109+ );
110+ check .setId (rs .getLong ("id" ));
111+ check .setUrlId (rs .getLong ("url_id" ));
112+ return check ;
113+ }
121114}
0 commit comments