Skip to content

Commit 6c2ebaa

Browse files
author
Maksim Litvinov
committed
add routes
1 parent f600b4d commit 6c2ebaa

File tree

8 files changed

+107
-10
lines changed

8 files changed

+107
-10
lines changed

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

src/main/java/org/example/hexlet/HelloWorld.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.example.hexlet.controller.UsersController;
1919
import org.example.hexlet.dto.MainPage;
2020
import org.example.hexlet.dto.courses.CoursesPage;
21+
import org.example.hexlet.dto.courses.CoursePage;
2122
import org.example.hexlet.dto.users.BuildUserPage;
2223
import org.example.hexlet.dto.users.UsersPage;
2324
import org.example.hexlet.model.Course;
@@ -32,6 +33,7 @@
3233

3334
import io.javalin.Javalin;
3435
import io.javalin.validation.ValidationException;
36+
import io.javalin.http.NotFoundResponse;
3537
import lombok.extern.slf4j.Slf4j;
3638

3739
@Slf4j
@@ -157,6 +159,14 @@ public static Javalin getApp() throws IOException, SQLException {
157159
ctx.render("courses/index.jte", model("page", page));
158160
});
159161

162+
app.get(NamedRoutes.coursePath("{id}"), ctx -> {
163+
var id = ctx.pathParamAsClass("id", Long.class).get();
164+
var course = CourseRepository.find(id)
165+
.orElseThrow(() -> new NotFoundResponse("Entity with id = " + id + " not found"));
166+
var page = new CoursePage(course);
167+
ctx.render("courses/show.jte", model("page", page));
168+
});
169+
160170
app.post(NamedRoutes.coursesPath(), ctx -> {
161171
var name = ctx.formParam("name");
162172
var description = ctx.formParam("description");

src/main/java/org/example/hexlet/repository/CourseRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,9 @@ public static Optional<Course> find(Long id) {
3333
public static List<Course> getEntities() {
3434
return entities;
3535
}
36+
37+
public static void removeAll() {
38+
entities = new ArrayList<Course>();
39+
}
3640
}
3741

src/main/java/org/example/hexlet/repository/PostRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ public static List<Post> getEntities() {
3535
}
3636

3737
public static void delete(Long id) {
38+
entities.removeIf(post -> post.getId() == id);
39+
}
3840

41+
public static void removeAll() {
42+
entities = new ArrayList<Post>();
3943
}
4044
}
4145

src/main/java/org/example/hexlet/repository/UserRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public static Optional<User> find(Long id) {
3131
}
3232

3333
public static void delete(Long id) {
34+
entities.removeIf(user -> user.getId() == id);
35+
}
3436

37+
public static void removeAll() {
38+
entities = new ArrayList<User>();
3539
}
3640

3741
public static List<User> getEntities() {

src/main/jte/courses/show.jte

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@import org.example.hexlet.dto.courses.CoursePage
2+
@param CoursePage page
3+
4+
@template.layout.page(
5+
content = @`
6+
<h1>${page.getCourse().getName()}</h1>
7+
<p>${page.getCourse().getDescription()}</p>
8+
`
9+
)

src/main/jte/posts/show.jte

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@import org.example.hexlet.dto.posts.PostPage
2+
@param PostPage page
3+
4+
@template.layout.page(
5+
content = @`
6+
<h1>${page.getPost().getTitle()}</h1>
7+
<p>${page.getPost().getBody()}</p>
8+
`
9+
)

src/test/java/org/example/hexlet/AppTest.java

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
import org.example.hexlet.model.Car;
99
import org.example.hexlet.repository.CarRepository;
10+
import org.example.hexlet.model.User;
11+
import org.example.hexlet.repository.UserRepository;
12+
import org.example.hexlet.model.Post;
13+
import org.example.hexlet.repository.PostRepository;
14+
import org.example.hexlet.model.Course;
15+
import org.example.hexlet.repository.CourseRepository;
1016
import org.junit.jupiter.api.BeforeEach;
1117
import org.junit.jupiter.api.Test;
1218

@@ -15,11 +21,14 @@
1521

1622
public class AppTest {
1723

18-
Javalin app;
24+
private Javalin app;
1925

2026
@BeforeEach
2127
public final void setUp() throws IOException, SQLException {
2228
app = HelloWorld.getApp();
29+
UserRepository.removeAll();
30+
PostRepository.removeAll();
31+
CourseRepository.removeAll();
2332
}
2433

2534
@Test
@@ -47,6 +56,42 @@ public void testUsersPage() {
4756
});
4857
}
4958

59+
@Test
60+
public void testUserPage() {
61+
JavalinTest.test(app, (server, client) -> {
62+
var user = new User("John", "john@mail.com", "strong");
63+
UserRepository.save(user);
64+
var response = client.get("/users/" +user.getId());
65+
assertThat(response.code()).isEqualTo(200);
66+
});
67+
}
68+
69+
@Test
70+
void testUserNotFound() throws Exception {
71+
JavalinTest.test(app, (server, client) -> {
72+
var response = client.get("/users/999999");
73+
assertThat(response.code()).isEqualTo(404);
74+
});
75+
}
76+
77+
@Test
78+
public void testBuildUser() {
79+
JavalinTest.test(app, (server, client) -> {
80+
var response = client.get("/users/build");
81+
assertThat(response.code()).isEqualTo(200);
82+
});
83+
}
84+
85+
@Test
86+
public void testCreateUser() {
87+
JavalinTest.test(app, (server, client) -> {
88+
var requestBody = "name=John&email=john@mail.com&password=strong";
89+
var response = client.post("/users", requestBody);
90+
assertThat(response.code()).isEqualTo(200);
91+
assertThat(response.body().string()).contains("John");
92+
});
93+
}
94+
5095
@Test
5196
public void testCoursesPage() {
5297
JavalinTest.test(app, (server, client) -> {
@@ -55,6 +100,16 @@ public void testCoursesPage() {
55100
});
56101
}
57102

103+
@Test
104+
public void testCoursePage() {
105+
var course = new Course("java", "best java course");
106+
CourseRepository.save(course);
107+
JavalinTest.test(app, (server, client) -> {
108+
var response = client.get("/courses/" + course.getId());
109+
assertThat(response.code()).isEqualTo(200);
110+
});
111+
}
112+
58113
@Test
59114
public void testBuildCourse() {
60115
JavalinTest.test(app, (server, client) -> {
@@ -81,6 +136,16 @@ public void testPostsPage() {
81136
});
82137
}
83138

139+
@Test
140+
public void testPostPage() {
141+
var post = new Post("test post", "test body");
142+
PostRepository.save(post);
143+
JavalinTest.test(app, (server, client) -> {
144+
var response = client.get("/posts/" + post.getId());
145+
assertThat(response.code()).isEqualTo(200);
146+
});
147+
}
148+
84149
@Test
85150
public void testCarsPage() {
86151
JavalinTest.test(app, (server, client) -> {
@@ -114,12 +179,4 @@ void testCarNotFound() throws Exception {
114179
assertThat(response.code()).isEqualTo(404);
115180
});
116181
}
117-
118-
@Test
119-
void testUserNotFound() throws Exception {
120-
JavalinTest.test(app, (server, client) -> {
121-
var response = client.get("/users/999999");
122-
assertThat(response.code()).isEqualTo(404);
123-
});
124-
}
125182
}

0 commit comments

Comments
 (0)