Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,19 @@ dependencies {
implementation("com.h2database:h2:2.3.232")
implementation("com.zaxxer:HikariCP:6.3.0")

// Unirest Java
implementation("com.konghq:unirest-java:3.14.5")

// Tests
testImplementation("org.junit.jupiter:junit-jupiter:6.0.0-M1")
testImplementation(platform("org.junit:junit-bom:6.0.0-M1"))
testImplementation("io.javalin:javalin-testtools:6.7.0")
testImplementation("org.assertj:assertj-core:4.0.0-M1")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:6.0.0-M1")

// MockWebServer » 5.1.0
// testImplementation("com.squareup.okhttp3:mockwebserver:5.1.0")
testImplementation("com.squareup.okhttp3:mockwebserver:4.12.0")
}

checkstyle {
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/hexlet/code/App.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hexlet.code;

import hexlet.code.controller.UrlCheckController;
import hexlet.code.repository.BaseRepository;
import hexlet.code.controller.UrlController;
import hexlet.code.util.NamedRoutes;
Expand Down Expand Up @@ -64,7 +65,7 @@ public static Javalin getApp() throws IOException, SQLException { //
Statement statement = connection.createStatement()) {
statement.execute(sql);
}
BaseRepository.dataSource = dataSource;
BaseRepository.setDataSource(dataSource);

Javalin app = Javalin.create(config -> {
config.bundledPlugins.enableDevLogging();
Expand All @@ -78,6 +79,8 @@ public static Javalin getApp() throws IOException, SQLException { //
app.get(NamedRoutes.buildPath(), UrlController::build);
app.get(NamedRoutes.urlsPath(), UrlController::index);
app.get(NamedRoutes.urlPath("{id}"), UrlController::show);
// checks
app.post(NamedRoutes.urlPathForChecks("{id}"), UrlCheckController::check);

return app;
}
Expand Down
61 changes: 61 additions & 0 deletions app/src/main/java/hexlet/code/controller/UrlCheckController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package hexlet.code.controller;

import hexlet.code.model.Url;
import hexlet.code.model.UrlCheck;
import hexlet.code.repository.UrlCheckRepository;
import hexlet.code.repository.UrlRepository;
import hexlet.code.util.NamedRoutes;

import static hexlet.code.util.AppSettings.FLASH_TYPE;
import static hexlet.code.util.AppSettings.FLASH_DANGER;
import static hexlet.code.util.AppSettings.FLASH_SUCCESS;

import static hexlet.code.util.AppSettings.FLASH;
import static hexlet.code.util.AppSettings.CHECK_ERROR;
import static hexlet.code.util.AppSettings.PAGE_OK;
import static hexlet.code.util.AppSettings.URL_BAD;

import io.javalin.http.Context;
import io.javalin.http.NotFoundResponse;

import java.sql.SQLException;

import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import kong.unirest.UnirestException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public final class UrlCheckController {
private UrlCheckController() {
// Prevent instantiation - Sonar Warning
}

public static void check(Context ctx) throws SQLException {
Long urlId = ctx.pathParamAsClass("id", Long.class).get();
Url url = UrlRepository.find(urlId)
.orElseThrow(() -> new NotFoundResponse("Entity with id = " + urlId + " not found"));
log.info("Получен ID: {}", urlId);
try {
HttpResponse<String> response = Unirest.get(url.getName()).asString();
int statusCode = response.getStatus();

UrlCheck urlCheck = new UrlCheck(urlId, statusCode, "title", "h1", "descrip");
UrlCheckRepository.save(urlCheck);

log.info("check saved");
} catch (UnirestException e) {
ctx.sessionAttribute(FLASH, URL_BAD);
ctx.sessionAttribute(FLASH_TYPE, FLASH_DANGER);
ctx.redirect(NamedRoutes.urlPath(urlId));

} catch (Exception e) {
ctx.sessionAttribute(FLASH, CHECK_ERROR + e.getMessage());
ctx.sessionAttribute(FLASH_TYPE, FLASH_DANGER);
ctx.redirect(NamedRoutes.urlPath(urlId));
}
ctx.sessionAttribute(FLASH, PAGE_OK);
ctx.sessionAttribute(FLASH_TYPE, FLASH_SUCCESS);
ctx.redirect(NamedRoutes.urlPath(urlId));
}
}
61 changes: 43 additions & 18 deletions app/src/main/java/hexlet/code/controller/UrlController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,22 @@
import hexlet.code.dto.urls.UrlPage;
import hexlet.code.dto.urls.UrlsPage;
import hexlet.code.model.Url;
import hexlet.code.model.UrlCheck;
import hexlet.code.repository.UrlCheckRepository;
import hexlet.code.repository.UrlRepository;
import hexlet.code.util.NamedRoutes;

import static hexlet.code.util.AppSettings.FLASH;
import static hexlet.code.util.AppSettings.FLASH_DANGER;
import static hexlet.code.util.AppSettings.FLASH_INFO;
import static hexlet.code.util.AppSettings.FLASH_SUCCESS;
import static hexlet.code.util.AppSettings.FLASH_TYPE;
import static hexlet.code.util.AppSettings.FLASH_WARNING;
import static hexlet.code.util.AppSettings.PAGE_ADDED;
import static hexlet.code.util.AppSettings.PAGE_EXIST;
import static hexlet.code.util.AppSettings.URL_BAD;
import static hexlet.code.util.AppSettings.URL_EMPTY;

import io.javalin.http.Context;
import io.javalin.http.NotFoundResponse;

Expand All @@ -20,34 +33,40 @@
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.utils.URIBuilder;

@Slf4j
public class UrlController {
public final class UrlController {
private UrlController() {
// Sonar warning
// Prevent instantiation
}

public static void build(Context ctx) {
BasePage page = new BasePage();
page.setFlash(ctx.consumeSessionAttribute("flash"));
page.setFlashType(ctx.consumeSessionAttribute("flash-type"));
page.setFlash(ctx.consumeSessionAttribute(FLASH));
page.setFlashType(ctx.consumeSessionAttribute(FLASH_TYPE));
ctx.render("urls/build.jte", model("page", page));
}

public static void create(Context ctx) throws SQLException, URISyntaxException, MalformedURLException {
String name = ctx.formParamAsClass("url", String.class).get();
URL unifmResourceId = null;
URL unifmResourceId;
try {
unifmResourceId = new URI(name).toURL();
} catch (Exception e) {
ctx.sessionAttribute("flash", "Некорректный URL");
ctx.sessionAttribute("flash-type", "danger");
ctx.sessionAttribute(FLASH, URL_BAD);
ctx.sessionAttribute(FLASH_TYPE, FLASH_DANGER);
ctx.redirect(NamedRoutes.buildPath());
return;
}

if (name == null || name.isEmpty()) {
ctx.sessionAttribute("flash", "Поле URL не должно быть пустым");
ctx.sessionAttribute("flash-type", "warning");
if (name.isEmpty()) {
ctx.sessionAttribute(FLASH, URL_EMPTY);
ctx.sessionAttribute(FLASH_TYPE, FLASH_WARNING);
ctx.redirect(NamedRoutes.buildPath());
return;
}
Expand All @@ -62,32 +81,38 @@ public static void create(Context ctx) throws SQLException, URISyntaxException,
Url newUrl = new Url(String.valueOf(url), LocalDateTime.now());
UrlRepository.save(newUrl);
} else {
ctx.sessionAttribute("flash", "Страница уже существует");
ctx.sessionAttribute("flash-type", "info");
ctx.sessionAttribute(FLASH, PAGE_EXIST);
ctx.sessionAttribute(FLASH_TYPE, FLASH_INFO);
ctx.redirect(NamedRoutes.urlsPath());
return;
}

ctx.sessionAttribute("flash", "Страница успешно добавлена");
ctx.sessionAttribute("flash-type", "success");
ctx.sessionAttribute(FLASH, PAGE_ADDED);
ctx.sessionAttribute(FLASH_TYPE, FLASH_SUCCESS);
ctx.redirect(NamedRoutes.urlsPath());
}

public static void index(Context ctx) throws SQLException {
List<Url> urls = UrlRepository.getEntities();
UrlsPage page = new UrlsPage(urls);
page.setFlash(ctx.consumeSessionAttribute("flash"));
page.setFlashType(ctx.consumeSessionAttribute("flash-type"));
Map<Long, UrlCheck> latestChecks = UrlCheckRepository.getLastestChecks();

UrlsPage page = new UrlsPage(urls, latestChecks);
page.setFlash(ctx.consumeSessionAttribute(FLASH));
page.setFlashType(ctx.consumeSessionAttribute(FLASH_TYPE));
ctx.render("urls/indexList.jte", model("page", page));
}

public static void show(Context ctx) throws SQLException {
Long id = ctx.pathParamAsClass("id", Long.class).get();
Url url = UrlRepository.find(id)
.orElseThrow(() -> new NotFoundResponse("Entity with id = " + id + " not found"));
List<UrlCheck> urlCheckList = UrlCheckRepository.findById(id);
if (!urlCheckList.isEmpty()) {
url.setUrlCheckList(urlCheckList);
}
UrlPage page = new UrlPage(url);
page.setFlash(ctx.consumeSessionAttribute("flash"));
page.setFlashType(ctx.consumeSessionAttribute("flash-type"));
page.setFlash(ctx.consumeSessionAttribute(FLASH));
page.setFlashType(ctx.consumeSessionAttribute(FLASH_TYPE));
ctx.render("urls/show.jte", model("page", page));
}
}
3 changes: 3 additions & 0 deletions app/src/main/java/hexlet/code/dto/urls/UrlsPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import hexlet.code.model.Url;

import java.util.List;
import java.util.Map;

import hexlet.code.model.UrlCheck;
import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class UrlsPage extends BasePage {
private List<Url> urls;
private Map<Long, UrlCheck> latestChecks;
}
4 changes: 4 additions & 0 deletions app/src/main/java/hexlet/code/model/Url.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.ToString;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
Expand All @@ -13,9 +15,11 @@ public class Url {
private Long id;
private String name;
private LocalDateTime createdAt;
private List<UrlCheck> urlCheckList;

public Url(String name, LocalDateTime createdAt) {
this.name = name;
this.createdAt = createdAt;
this.urlCheckList = new ArrayList<>();
}
}
25 changes: 25 additions & 0 deletions app/src/main/java/hexlet/code/model/UrlCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package hexlet.code.model;

import java.time.LocalDateTime;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class UrlCheck {
private Long id;
private Long urlId;
private int statusCode;
private String title;
private String h1;
private String description;
private LocalDateTime createdAt;

public UrlCheck(Long urlId, int statusCode, String title, String h1, String description) {
this.urlId = urlId;
this.title = title;
this.h1 = h1;
this.description = description;
this.statusCode = statusCode;
}
}
10 changes: 9 additions & 1 deletion app/src/main/java/hexlet/code/repository/BaseRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@
import com.zaxxer.hikari.HikariDataSource;

public class BaseRepository {
public static HikariDataSource dataSource;
private static HikariDataSource dataSource;

public static HikariDataSource getDataSource() {
return dataSource;
}

public static void setDataSource(HikariDataSource dataSource) {
BaseRepository.dataSource = dataSource;
}
}
Loading
Loading