-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding check logic and consiquent test
- Loading branch information
1 parent
938e0f0
commit 39d07bb
Showing
17 changed files
with
348 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
package hexlet.code.dto; | ||
import hexlet.code.model.Url; | ||
import hexlet.code.model.UrlCheck; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
import java.util.List; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public class UrlPage { | ||
private Url url; | ||
private List<UrlCheck> urlChecks; | ||
private String flash; | ||
private String flashStatus; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package hexlet.code.model; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.sql.Timestamp; | ||
import java.time.LocalDateTime; | ||
|
||
@NoArgsConstructor | ||
@Getter | ||
@Setter | ||
public class UrlCheck { | ||
private Long id; | ||
private Integer statusCode; | ||
private String title; | ||
private String h1; | ||
private String description; | ||
private Timestamp createdAt; | ||
private Long urlId; | ||
|
||
public UrlCheck(Long urlId) { | ||
this.urlId = urlId; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/main/java/hexlet/code/repository/UrlCheckRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package hexlet.code.repository; | ||
|
||
import hexlet.code.model.UrlCheck; | ||
|
||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class UrlCheckRepository extends BaseRepository { | ||
//save | ||
public static void save(UrlCheck urlCheck) throws SQLException { | ||
if (urlCheck.getId() == null) { | ||
var sql = "INSERT INTO url_checks (status_code, title, h1, description, created_at, url_id) VALUES (?, ?, ?, ?, ?, ?)"; | ||
try (var connection = dataSource.getConnection(); | ||
var preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { | ||
preparedStatement.setInt(1, urlCheck.getStatusCode()); | ||
preparedStatement.setString(2, urlCheck.getTitle()); | ||
preparedStatement.setString(3, urlCheck.getH1()); | ||
preparedStatement.setString(4, urlCheck.getDescription()); | ||
preparedStatement.setTimestamp(5, urlCheck.getCreatedAt()); | ||
preparedStatement.setLong(6, urlCheck.getUrlId()); | ||
preparedStatement.executeUpdate(); | ||
var generatedKeys = preparedStatement.getGeneratedKeys(); | ||
if (generatedKeys.next()) { | ||
var id = generatedKeys.getLong("id"); | ||
urlCheck.setId(id); | ||
} else { | ||
throw new SQLException("DB didn't return any index"); | ||
} | ||
} | ||
} else { | ||
throw new SQLException("Such urlCheck id already exists!"); | ||
} | ||
} | ||
//getChecks | ||
public static List<UrlCheck> getChecks(Long urlId) throws SQLException { | ||
var urlChecks = new LinkedList<UrlCheck>(); | ||
var sql = "SELECT * FROM url_checks WHERE url_id = ? ORDER BY created_at DESC"; | ||
try (var connection = dataSource.getConnection(); | ||
var preparedStatement = connection.prepareStatement(sql)) { | ||
preparedStatement.setLong(1, urlId); | ||
var resultSet = preparedStatement.executeQuery(); | ||
while (resultSet.next()) { | ||
var urlCheck = new UrlCheck(urlId); | ||
urlCheck.setId(resultSet.getLong("id")); | ||
urlCheck.setStatusCode(resultSet.getInt("status_code")); | ||
urlCheck.setTitle(resultSet.getString("title")); | ||
urlCheck.setH1(resultSet.getString("h1")); | ||
urlCheck.setDescription(resultSet.getString("description")); | ||
urlCheck.setCreatedAt(resultSet.getTimestamp("created_at")); | ||
urlChecks.add(urlCheck); | ||
} | ||
} | ||
return urlChecks; | ||
} | ||
//findLast | ||
public static Optional<UrlCheck> findLast(Long urlId) { | ||
try { | ||
var urlChecks = getChecks(urlId); | ||
if (!urlChecks.isEmpty()) { | ||
return Optional.of(urlChecks.getFirst()); | ||
} | ||
return Optional.empty(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException("For some reason can't find last check of url"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.