Skip to content

Commit

Permalink
Merge pull request #1 from CicadaN/detached
Browse files Browse the repository at this point in the history
Detached
  • Loading branch information
CicadaN authored Oct 24, 2024
2 parents 9eea504 + 7dc389c commit 3b4a87b
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 68 deletions.
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
# 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
```
1 change: 0 additions & 1 deletion app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ COPY /app .

RUN chmod +x gradlew


RUN ./gradlew installDist

CMD ./build/install/app/bin/app
Expand Down
10 changes: 1 addition & 9 deletions app/src/main/java/hexlet/code/controller/UrlController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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", "Страница уже существует");
Expand Down Expand Up @@ -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!");
Expand Down
8 changes: 0 additions & 8 deletions app/src/main/java/hexlet/code/model/Url.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/hexlet/code/model/UrlCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import lombok.Data;

import java.sql.Timestamp;
import java.time.Instant;

@Data
public class UrlCheck {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
public class BaseRepository {
public static HikariDataSource dataSource;
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -53,7 +55,7 @@ public static Map<Integer, UrlCheck> 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;
Expand All @@ -79,7 +81,7 @@ public static List<UrlCheck> 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);
}
}
Expand Down
53 changes: 20 additions & 33 deletions app/src/main/java/hexlet/code/repository/UrlRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -38,12 +36,28 @@ public static void save(Url url) throws SQLException {

public static Optional<Url> 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<Url> 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<Url> findAll() throws SQLException {
Expand All @@ -52,39 +66,13 @@ public static List<Url> 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<T> {
void accept(T t) throws SQLException;
}

public static Optional<Url> findUrl(String sql, SqlConsumer<PreparedStatement> 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");
Expand All @@ -94,5 +82,4 @@ private static Url mapResultSetToUrl(ResultSet rs) throws SQLException {
url.setCreatedAt(timestamp.toInstant());
return url;
}

}
13 changes: 13 additions & 0 deletions app/src/main/java/hexlet/code/util/Utils.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 2 additions & 2 deletions app/src/main/resources/templates/urls/index.jte
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -35,7 +35,7 @@ content = @`
</td>
<td>
@if(urlCheck != null)
${urlCheck.getCreatedAt().toLocalDateTime().format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"))}
${Utils.getFormattedCreatedAt(urlCheck.getCreatedAt())}
@endif
</td>
<td>
Expand Down
11 changes: 5 additions & 6 deletions app/src/main/resources/templates/urls/show.jte
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -28,15 +27,15 @@ content = @`
</tr>
<tr>
<td>Date Created</td>
<td>${urlPage.getUrl().getFormattedCreatedAt()}</td>
<td>${Utils.getFormattedCreatedAt(urlPage.getUrl().getCreatedAt())}</td>
</tr>
</tbody>
</table>
<h2>
Проверки
Check
</h2>
<form method="post" action="${NamedRoutes.urlCheck(urlPage.getUrl().getId())}">
<button type="submit" class="btn btn-primary">Запустить проверку</button>
<button type="submit" class="btn btn-primary">Run the analysis</button>
</form>
<table class="table table-bordered table-hover mt-3">
<thead>
Expand Down Expand Up @@ -66,7 +65,7 @@ content = @`
${checkUrl.getDescription()}
</td>
<td>
${checkUrl.getCreatedAt().toLocalDateTime().format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"))}
${Utils.getFormattedCreatedAt(checkUrl.getCreatedAt())}
</td>
</tr>
@endfor
Expand Down
2 changes: 2 additions & 0 deletions app/src/test/java/hexlet/code/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
}
}
2 changes: 1 addition & 1 deletion app/src/test/resources/MockWebServer.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<title>MockWebServer</title>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="description" content="fake description">
<meta name="description" content="This is a mock web server">
</head>
<body>
<div><h1>Hello World Server !</h1></div>
Expand Down

0 comments on commit 3b4a87b

Please sign in to comment.