Skip to content

Commit c8968ce

Browse files
committed
fix crucial bugs
1 parent 97239a4 commit c8968ce

File tree

6 files changed

+20
-24
lines changed

6 files changed

+20
-24
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dependencies {
2626
implementation("org.slf4j:slf4j-simple:2.0.17")
2727
implementation("io.javalin:javalin-rendering:6.7.0")
2828
implementation("gg.jte:jte:3.2.1")
29+
implementation("gg.jte:jte-watcher:3.2.1")
2930

3031
compileOnly("org.projectlombok:lombok:1.18.38")
3132
annotationProcessor("org.projectlombok:lombok:1.18.38")

app/src/main/java/hexlet/code/App.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ private static Javalin getApp() throws SQLException, IOException {
8787
ctx.sessionAttribute("currentUser"), "Гость"
8888
);
8989
String url = ctx.sessionAttribute("url");
90-
var page = new MainPage(currentUser, url);
9190

91+
var page = new MainPage(currentUser, url);
92+
page.setFlash(ctx.consumeSessionAttribute("flash"));
93+
page.setFlashType(ctx.consumeSessionAttribute("flash-type"));
9294
ctx.render("index.jte", model("page", page));
9395
});
9496

app/src/main/java/hexlet/code/controller/UrlsController.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.net.MalformedURLException;
1313
import java.net.URI;
14+
import java.net.URISyntaxException;
1415
import java.sql.SQLException;
1516

1617
import static io.javalin.rendering.template.TemplateUtil.model;
@@ -32,10 +33,10 @@ public static void show(Context ctx) throws SQLException {
3233
ctx.render("urls/show.jte", model("page", page));
3334
}
3435

35-
public static void create(Context ctx) throws SQLException {
36+
public static void create(Context ctx) throws SQLException, URISyntaxException {
3637

3738
try {
38-
var url = ctx.formParamAsClass("url", URI.class).get().normalize().toURL();
39+
var url = new URI(ctx.formParam("url")).normalize().toURL();
3940
var protocol = url.getProtocol();
4041
var authority = url.getAuthority();
4142
var urlName = String.format("%s://%s", protocol, authority);
@@ -51,19 +52,15 @@ public static void create(Context ctx) throws SQLException {
5152
ctx.redirect(NamedRoutes.rootPath());
5253

5354
} catch (MalformedURLException | IllegalArgumentException e) {
54-
var page = new MainPage(
55-
ctx.sessionAttribute("currentUser"),
56-
ctx.formParam("url")
57-
);
5855

5956
if (e instanceof MalformedURLException) {
60-
page.setFlash("Некорректный URL");
57+
ctx.sessionAttribute("flash", "Некорректный URL");
6158
} else {
62-
page.setFlash(e.getMessage());
59+
ctx.sessionAttribute("flash", e.getMessage());
6360
}
6461

65-
page.setFlashType("alert alert-danger");
66-
ctx.render(NamedRoutes.rootPath(), model("page", page));
62+
ctx.sessionAttribute("flash-type", "alert alert-danger");
63+
ctx.redirect(NamedRoutes.rootPath());
6764
}
6865
}
6966
}

app/src/main/resources/templates/index.jte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
@import hexlet.code.util.NamedRoutes
21
@import hexlet.code.dto.MainPage
2+
@import hexlet.code.util.NamedRoutes
33

44
@param MainPage page
55

@@ -9,8 +9,8 @@
99
content = @`
1010
<h1>Привет, ${page.getCurrentUser()}!</h1>
1111
<h3>Введите ссылку на сайт</h3>
12-
<form>
13-
<input type="url" name="url" value="${page.getUrl()}">
12+
<form action ="${NamedRoutes.urlsPath()}" method="post">
13+
<input type="url" name="url" value="${page.getUrl()}" required>
1414
<input type="submit" value="Проверить">
1515
</form>
1616
`

app/src/main/resources/templates/layout/page.jte

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
@param BasePage page = null
77

88
<!doctype html>
9-
<html lang="en">
9+
<html lang="ru">
1010
<head>
1111
<meta charset="utf-8" />
1212
<meta name="viewport" content="width=device-width, initial-scale=1" />
1313
<title>${headTitle}</title>
1414

15-
<link rel="stylesheet" href="../../bootstrap/css/bootstrap-grid.min.css">
16-
<link rel="stylesheet" href="../../bootstrap/css/bootstrap-utilities.min.css">
15+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-LN+7fdVzj6u52u30Kp6M/trliBMCMKTyK833zpbD+pXdCLuTusPj697FH4R/5mcr" crossorigin="anonymous">
16+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ndDqU0Gzau9qJ1lfW4pNLlhNTkCfHzAVBReH9diLvGRem5+R9g2FzA8ZGN954O5Q" crossorigin="anonymous"></script>
1717
</head>
1818
<body>
1919
@if(page != null && page.getFlash() != null)
@@ -23,14 +23,10 @@
2323
@endif
2424
<header>
2525
<nav>
26-
<h1><a href="${NamedRoutes.rootPath()}">На главную</a></h1>
26+
<h4><a href="${NamedRoutes.rootPath()}">На главную</a></h4>
27+
<h4><a href="${NamedRoutes.urlsPath()}">Сайты</a></h4>
2728
</nav>
2829
</header>
29-
30-
@if(page != null && page.getFlash() != null)
31-
<p>${page.getFlash()}</p>
32-
@endif
33-
3430
${content}
3531
</body>
3632
</html>

app/src/main/resources/templates/urls/index.jte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@else
1313
@for(var url : page.getUrls())
1414
<div>
15-
<h3><a href="${NamedRoutes.urlPath(url.getId())}">${url.getName()}</a></h3>
15+
<h4><a href="${NamedRoutes.urlPath(url.getId())}">${url.getName()}</a></h4>
1616
</div>
1717
@endfor
1818
@endif

0 commit comments

Comments
 (0)