diff --git a/README.md b/README.md index b1e7f39..ab28a55 100644 --- a/README.md +++ b/README.md @@ -3,5 +3,35 @@ [![Maintainability](https://api.codeclimate.com/v1/badges/9479b8078822b06bf594/maintainability)](https://codeclimate.com/github/CicadaN/java-project-72/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/9479b8078822b06bf594/test_coverage)](https://codeclimate.com/github/CicadaN/java-project-72/test_coverage) -### Deploy -[My first website](https://java-project-72-p00i.onrender.com/) \ No newline at end of file +# Page Analyzer: + +This is a demo application designed for checking the SEO suitability of a website. + + +## Deploy + +[Deploy on Render.com](https://java-project-72-p00i.onrender.com/) + +## Setup + +```bash +make build +``` + +## Run + +```bash +make run +``` + +## Run checkstyle + +```bash +make lint +``` + +## Run tests + +```bash +make test +``` \ No newline at end of file diff --git a/app/Dockerfile b/app/Dockerfile index 7ef7836..20e8e18 100644 --- a/app/Dockerfile +++ b/app/Dockerfile @@ -6,7 +6,6 @@ COPY /app . RUN chmod +x gradlew - RUN ./gradlew installDist CMD ./build/install/app/bin/app diff --git a/app/src/main/java/hexlet/code/controller/UrlController.java b/app/src/main/java/hexlet/code/controller/UrlController.java index 273529b..652e4a3 100644 --- a/app/src/main/java/hexlet/code/controller/UrlController.java +++ b/app/src/main/java/hexlet/code/controller/UrlController.java @@ -21,7 +21,6 @@ import java.net.URISyntaxException; import java.net.URL; import java.sql.SQLException; -import java.sql.Timestamp; import java.util.Collections; import java.util.List; import java.util.Map; @@ -31,12 +30,6 @@ public class UrlController { public static void create(Context ctx) throws SQLException { String url = ctx.formParam("url"); - if (url == null || url.isEmpty()) { - ctx.sessionAttribute("flash", "URL не может быть пустым"); - ctx.sessionAttribute("flashType", "danger"); - ctx.redirect(NamedRoutes.rootPath()); - return; - } URI uri; try { uri = new URL(url).toURI(); @@ -48,7 +41,7 @@ public static void create(Context ctx) throws SQLException { } String name = uri.getScheme() + "://" + uri.getHost(); if (uri.getPort() != -1) { - name += ":" + uri.getPort(); // только если порт явно указан + name += ":" + uri.getPort(); } if (UrlRepository.findByName(name).isPresent()) { ctx.sessionAttribute("flash", "Страница уже существует"); @@ -97,7 +90,6 @@ public static void check(Context ctx) throws SQLException { UrlCheck urlCheck = new UrlCheck(statusCode, title, h1, description); urlCheck.setUrlId(urlId); - urlCheck.setCreatedAt(new Timestamp(System.currentTimeMillis())); UrlCheckRepository.save(urlCheck); ctx.sessionAttribute("flash", "URL check was successful!"); diff --git a/app/src/main/java/hexlet/code/model/Url.java b/app/src/main/java/hexlet/code/model/Url.java index ff5ea48..bbde18d 100644 --- a/app/src/main/java/hexlet/code/model/Url.java +++ b/app/src/main/java/hexlet/code/model/Url.java @@ -4,18 +4,10 @@ import lombok.NonNull; import java.time.Instant; -import java.time.ZoneId; -import java.time.format.DateTimeFormatter; @Data public final class Url { private int id; @NonNull private String name; private Instant createdAt; - - public String getFormattedCreatedAt() { - return DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm") - .withZone(ZoneId.systemDefault()) - .format(this.createdAt); - } } diff --git a/app/src/main/java/hexlet/code/model/UrlCheck.java b/app/src/main/java/hexlet/code/model/UrlCheck.java index 838caf4..16a7669 100644 --- a/app/src/main/java/hexlet/code/model/UrlCheck.java +++ b/app/src/main/java/hexlet/code/model/UrlCheck.java @@ -3,7 +3,7 @@ import lombok.Data; -import java.sql.Timestamp; +import java.time.Instant; @Data public class UrlCheck { @@ -13,7 +13,7 @@ public class UrlCheck { private String h1; private String description; private int urlId; - private Timestamp createdAt; + private Instant createdAt; public UrlCheck(int statusCode, String title, String h1, String description) { this.statusCode = statusCode; diff --git a/app/src/main/java/hexlet/code/repository/BaseRepository.java b/app/src/main/java/hexlet/code/repository/BaseRepository.java index f7122fe..9ecf5f2 100644 --- a/app/src/main/java/hexlet/code/repository/BaseRepository.java +++ b/app/src/main/java/hexlet/code/repository/BaseRepository.java @@ -5,4 +5,3 @@ public class BaseRepository { public static HikariDataSource dataSource; } - diff --git a/app/src/main/java/hexlet/code/repository/UrlCheckRepository.java b/app/src/main/java/hexlet/code/repository/UrlCheckRepository.java index b003a69..d01fdec 100644 --- a/app/src/main/java/hexlet/code/repository/UrlCheckRepository.java +++ b/app/src/main/java/hexlet/code/repository/UrlCheckRepository.java @@ -6,6 +6,7 @@ import java.sql.Statement; import java.sql.Timestamp; +import java.time.Instant; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -23,7 +24,8 @@ public static void save(UrlCheck urlCheck) throws SQLException { stmt.setString(3, urlCheck.getH1()); stmt.setString(4, urlCheck.getDescription()); stmt.setInt(5, urlCheck.getUrlId()); - stmt.setTimestamp(6, new Timestamp(System.currentTimeMillis())); + Instant instant = Instant.now(); + stmt.setTimestamp(6, Timestamp.from(instant)); stmt.executeUpdate(); var rs = stmt.getGeneratedKeys(); @@ -53,7 +55,7 @@ public static Map findLatestCheckUrl() throws SQLException { UrlCheck urlCheck = new UrlCheck(statusCode, title, h1, description); urlCheck.setId(id); urlCheck.setUrlId(urlId); - urlCheck.setCreatedAt(timestamp); + urlCheck.setCreatedAt(timestamp.toInstant()); result.put(urlId, urlCheck); } return result; @@ -79,7 +81,7 @@ public static List findbyId(int urlId) throws SQLException { UrlCheck urlCheck = new UrlCheck(statusCode, title, h1, description); urlCheck.setId(id); urlCheck.setUrlId(urlId); - urlCheck.setCreatedAt(timestamp); + urlCheck.setCreatedAt(timestamp.toInstant()); result.add(urlCheck); } } diff --git a/app/src/main/java/hexlet/code/repository/UrlRepository.java b/app/src/main/java/hexlet/code/repository/UrlRepository.java index 63ce0b6..b41efef 100644 --- a/app/src/main/java/hexlet/code/repository/UrlRepository.java +++ b/app/src/main/java/hexlet/code/repository/UrlRepository.java @@ -23,13 +23,11 @@ public static void save(Url url) throws SQLException { Instant instant = Instant.now(); url.setCreatedAt(instant); stmt.setTimestamp(2, Timestamp.from(instant)); - int affectedRows = stmt.executeUpdate(); -// System.out.println("Affected rows: " + affectedRows); // logg + stmt.executeUpdate(); ResultSet rs = stmt.getGeneratedKeys(); if (rs.next()) { url.setId(rs.getInt(1)); -// System.out.println("Generated ID: " + url.getId()); // logg } else { throw new SQLException("Failed to save url"); } @@ -38,12 +36,28 @@ public static void save(Url url) throws SQLException { public static Optional findById(int id) throws SQLException { String sql = "SELECT * FROM URLS WHERE ID = ?"; - return findUrl(sql, stmt -> stmt.setInt(1, id)); + try (Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, id); + ResultSet rs = stmt.executeQuery(); + if (rs.next()) { + return Optional.of(mapResultSetToUrl(rs)); + } + return Optional.empty(); + } } public static Optional findByName(String name) throws SQLException { String sql = "SELECT * FROM URLS WHERE NAME = ?"; - return findUrl(sql, stmt -> stmt.setString(1, name)); + try (Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, name); + ResultSet rs = stmt.executeQuery(); + if (rs.next()) { + return Optional.of(mapResultSetToUrl(rs)); + } + return Optional.empty(); + } } public static List findAll() throws SQLException { @@ -52,39 +66,13 @@ public static List findAll() throws SQLException { try (Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql); ResultSet rs = stmt.executeQuery()) { - while (rs.next()) { - int id = rs.getInt("ID"); - String name = rs.getString("NAME"); - Timestamp timestamp = rs.getTimestamp("CREATED_AT"); - Url url = new Url(name); - url.setId(id); - url.setCreatedAt(timestamp.toInstant()); - urls.add(url); + urls.add(mapResultSetToUrl(rs)); } } - System.out.println("Fetched URLs: " + urls.size()); // Проверка количества полученных URL return urls; } - @FunctionalInterface - private interface SqlConsumer { - void accept(T t) throws SQLException; - } - - public static Optional findUrl(String sql, SqlConsumer pss) throws SQLException { - try (Connection conn = dataSource.getConnection(); - PreparedStatement stmt = conn.prepareStatement(sql)) { - pss.accept(stmt); - ResultSet rs = stmt.executeQuery(); - if (rs.next()) { - Url url = mapResultSetToUrl(rs); - return Optional.of(url); - } - } - return Optional.empty(); - } - private static Url mapResultSetToUrl(ResultSet rs) throws SQLException { int id = rs.getInt("ID"); String name = rs.getString("NAME"); @@ -94,5 +82,4 @@ private static Url mapResultSetToUrl(ResultSet rs) throws SQLException { url.setCreatedAt(timestamp.toInstant()); return url; } - } diff --git a/app/src/main/java/hexlet/code/util/Utils.java b/app/src/main/java/hexlet/code/util/Utils.java new file mode 100644 index 0000000..1dec25f --- /dev/null +++ b/app/src/main/java/hexlet/code/util/Utils.java @@ -0,0 +1,13 @@ +package hexlet.code.util; + +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; + +public class Utils { + public static String getFormattedCreatedAt(Instant createdAt) { + return DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm") + .withZone(ZoneId.systemDefault()) + .format(createdAt); + } +} diff --git a/app/src/main/resources/templates/urls/index.jte b/app/src/main/resources/templates/urls/index.jte index 38fec9f..ed0d5ef 100644 --- a/app/src/main/resources/templates/urls/index.jte +++ b/app/src/main/resources/templates/urls/index.jte @@ -1,6 +1,6 @@ @import hexlet.code.dto.url.UrlsPage @import hexlet.code.model.Url -@import hexlet.code.model.UrlCheck +@import hexlet.code.util.Utils @import hexlet.code.util.NamedRoutes @import java.time.format.DateTimeFormatter @import static hexlet.code.util.NamedRoutes.urlCheck @@ -35,7 +35,7 @@ content = @` @if(urlCheck != null) - ${urlCheck.getCreatedAt().toLocalDateTime().format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"))} + ${Utils.getFormattedCreatedAt(urlCheck.getCreatedAt())} @endif diff --git a/app/src/main/resources/templates/urls/show.jte b/app/src/main/resources/templates/urls/show.jte index 7e104b8..ee958a9 100644 --- a/app/src/main/resources/templates/urls/show.jte +++ b/app/src/main/resources/templates/urls/show.jte @@ -1,7 +1,6 @@ @import hexlet.code.dto.url.UrlPage -@import hexlet.code.model.Url @import hexlet.code.util.NamedRoutes -@import java.time.format.DateTimeFormatter +@import hexlet.code.util.Utils @param UrlPage urlPage @@ -28,15 +27,15 @@ content = @` Date Created - ${urlPage.getUrl().getFormattedCreatedAt()} + ${Utils.getFormattedCreatedAt(urlPage.getUrl().getCreatedAt())}

- Проверки + Check

- +
@@ -66,7 +65,7 @@ content = @` ${checkUrl.getDescription()} @endfor diff --git a/app/src/test/java/hexlet/code/AppTest.java b/app/src/test/java/hexlet/code/AppTest.java index 5b681d4..bba1193 100644 --- a/app/src/test/java/hexlet/code/AppTest.java +++ b/app/src/test/java/hexlet/code/AppTest.java @@ -122,8 +122,10 @@ public void testMock() { var checkUrl = UrlCheckRepository.findbyId(url.getId()); var title = checkUrl.getFirst().getTitle(); var h1 = checkUrl.getFirst().getH1(); + var description = checkUrl.getFirst().getDescription(); assertThat(title).isEqualTo("MockWebServer"); assertThat(h1).isEqualTo("Hello World Server !"); + assertThat(description).isEqualTo("This is a mock web server"); }); } } diff --git a/app/src/test/resources/MockWebServer.html b/app/src/test/resources/MockWebServer.html index bc1e0b6..742f124 100644 --- a/app/src/test/resources/MockWebServer.html +++ b/app/src/test/resources/MockWebServer.html @@ -4,7 +4,7 @@ MockWebServer - +

Hello World Server !

- ${checkUrl.getCreatedAt().toLocalDateTime().format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"))} + ${Utils.getFormattedCreatedAt(checkUrl.getCreatedAt())}