diff --git a/.current.json b/.current.json index 8d3ee7c..fb979de 100644 --- a/.current.json +++ b/.current.json @@ -1 +1 @@ -{"assignment":"java-web-ru/crud"} +{"assignment":"java-web-ru/mvc"} diff --git a/java-web-ru/mvc/gradlew b/java-web-ru/mvc/gradlew old mode 100644 new mode 100755 diff --git a/java-web-ru/mvc/gradlew.bat b/java-web-ru/mvc/gradlew.bat index 93e3f59..6689b85 100644 --- a/java-web-ru/mvc/gradlew.bat +++ b/java-web-ru/mvc/gradlew.bat @@ -1,92 +1,92 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem - -@if "%DEBUG%"=="" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%"=="" set DIRNAME=. -@rem This is normally unused -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if %ERRORLEVEL% equ 0 goto execute - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto execute - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* - -:end -@rem End local scope for the variables with windows NT shell -if %ERRORLEVEL% equ 0 goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -set EXIT_CODE=%ERRORLEVEL% -if %EXIT_CODE% equ 0 set EXIT_CODE=1 -if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% -exit /b %EXIT_CODE% - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/java-web-ru/mvc/src/main/java/exercise/App.java b/java-web-ru/mvc/src/main/java/exercise/App.java index 59a1f84..d6c9277 100644 --- a/java-web-ru/mvc/src/main/java/exercise/App.java +++ b/java-web-ru/mvc/src/main/java/exercise/App.java @@ -1,36 +1,36 @@ -package exercise; - -import io.javalin.Javalin; -import exercise.controller.PostsController; -import exercise.controller.RootController; -import exercise.util.NamedRoutes; - -public final class App { - - public static Javalin getApp() { - - var app = Javalin.create(config -> { - config.plugins.enableDevLogging(); - }); - - app.get(NamedRoutes.rootPath(), RootController::index); - - app.get(NamedRoutes.buildPostPath(), PostsController::build); - app.post(NamedRoutes.postsPath(), PostsController::create); - - app.get(NamedRoutes.postsPath(), PostsController::index); - app.get(NamedRoutes.postPath("{id}"), PostsController::show); - - // BEGIN - app.get(NamedRoutes.editPostPath(), PostsController::edit); - app.post(NamedRoutes.postPath("{id}"), PostsController::create); - // END - - return app; - } - - public static void main(String[] args) { - Javalin app = getApp(); - app.start(7070); - } -} +package exercise; + +import io.javalin.Javalin; +import exercise.controller.PostsController; +import exercise.controller.RootController; +import exercise.util.NamedRoutes; + +public final class App { + + public static Javalin getApp() { + + var app = Javalin.create(config -> { + config.plugins.enableDevLogging(); + }); + + app.get(NamedRoutes.rootPath(), RootController::index); + + app.get(NamedRoutes.buildPostPath(), PostsController::build); + app.post(NamedRoutes.postsPath(), PostsController::create); + + app.get(NamedRoutes.postsPath(), PostsController::index); + app.get(NamedRoutes.postPath("{id}"), PostsController::show); + + // BEGIN + app.get(NamedRoutes.editPostPath(), PostsController::edit); + app.post(NamedRoutes.postPath("{id}"), PostsController::create); + // END + + return app; + } + + public static void main(String[] args) { + Javalin app = getApp(); + app.start(7070); + } +} diff --git a/java-web-ru/mvc/src/main/java/exercise/controller/PostsController.java b/java-web-ru/mvc/src/main/java/exercise/controller/PostsController.java index ae4f784..6b8d2e5 100644 --- a/java-web-ru/mvc/src/main/java/exercise/controller/PostsController.java +++ b/java-web-ru/mvc/src/main/java/exercise/controller/PostsController.java @@ -1,79 +1,79 @@ -package exercise.controller; - -import java.util.Collections; -import java.util.Objects; - -import exercise.dto.posts.PostsPage; -import exercise.dto.posts.PostPage; -import exercise.model.Post; -import exercise.repository.PostRepository; -import exercise.dto.posts.BuildPostPage; -//import exercise.dto.posts.EditPostPage; -import exercise.util.NamedRoutes; - -import io.javalin.http.Context; -import io.javalin.validation.ValidationException; -import io.javalin.http.NotFoundResponse; -import org.jetbrains.annotations.NotNull; - -public class PostsController { - - public static void build(Context ctx) { - var page = new BuildPostPage(); - ctx.render("posts/build.jte", Collections.singletonMap("page", page)); - } - - public static void create(Context ctx) { - try { - var name = ctx.formParamAsClass("name", String.class) - .check(value -> value.length() >= 2, "Название не должно быть короче двух символов") - .get(); - - var body = ctx.formParamAsClass("body", String.class) - .check(value -> value.length() >= 10, "Пост должен быть не короче 10 символов") - .get(); - if (ctx.formParam("_method") != null) { - if ("PATCH".equals(ctx.formParam("_method"))) { - var id = ctx.pathParamAsClass("{id}", Long.class).get(); - var post = PostRepository.find(id).get(); - post.setName(name); - post.setBody(body); - } - } else { - PostRepository.save(new Post(name, body)); - } - ctx.redirect(NamedRoutes.postsPath()); - - } catch (ValidationException e) { - var name = ctx.formParam("name"); - var body = ctx.formParam("body"); - var page = new BuildPostPage(name, body, e.getErrors()); - ctx.render("posts/build.jte", Collections.singletonMap("page", page)).status(422); - } - } - - public static void index(Context ctx) { - var posts = PostRepository.getEntities(); - var postPage = new PostsPage(posts); - ctx.render("posts/index.jte", Collections.singletonMap("page", postPage)); - } - - public static void show(Context ctx) { - var id = ctx.pathParamAsClass("id", Long.class).get(); - var post = PostRepository.find(id) - .orElseThrow(() -> new NotFoundResponse("Post not found")); - - var page = new PostPage(post); - ctx.render("posts/show.jte", Collections.singletonMap("page", page)); - } - - // BEGIN - public static void edit(Context ctx) { - var id = ctx.pathParamAsClass("id", Long.class).get(); - var post = PostRepository.find(id) - .orElseThrow(() -> new NotFoundResponse("Entity with id = " + id + " not found")); - var page = new PostPage(post); - ctx.render("posts/edit.jte", Collections.singletonMap("page", page)); - } - // END -} +package exercise.controller; + +import java.util.Collections; +import java.util.Objects; + +import exercise.dto.posts.PostsPage; +import exercise.dto.posts.PostPage; +import exercise.model.Post; +import exercise.repository.PostRepository; +import exercise.dto.posts.BuildPostPage; +//import exercise.dto.posts.EditPostPage; +import exercise.util.NamedRoutes; + +import io.javalin.http.Context; +import io.javalin.validation.ValidationException; +import io.javalin.http.NotFoundResponse; +import org.jetbrains.annotations.NotNull; + +public class PostsController { + + public static void build(Context ctx) { + var page = new BuildPostPage(); + ctx.render("posts/build.jte", Collections.singletonMap("page", page)); + } + + public static void create(Context ctx) { + try { + var name = ctx.formParamAsClass("name", String.class) + .check(value -> value.length() >= 2, "Название не должно быть короче двух символов") + .get(); + + var body = ctx.formParamAsClass("body", String.class) + .check(value -> value.length() >= 10, "Пост должен быть не короче 10 символов") + .get(); + if (ctx.formParam("_method") != null) { + if ("PATCH".equals(ctx.formParam("_method"))) { + var id = ctx.pathParamAsClass("{id}", Long.class).get(); + var post = PostRepository.find(id).get(); + post.setName(name); + post.setBody(body); + } + } else { + PostRepository.save(new Post(name, body)); + } + ctx.redirect(NamedRoutes.postsPath()); + + } catch (ValidationException e) { + var name = ctx.formParam("name"); + var body = ctx.formParam("body"); + var page = new BuildPostPage(name, body, e.getErrors()); + ctx.render("posts/build.jte", Collections.singletonMap("page", page)).status(422); + } + } + + public static void index(Context ctx) { + var posts = PostRepository.getEntities(); + var postPage = new PostsPage(posts); + ctx.render("posts/index.jte", Collections.singletonMap("page", postPage)); + } + + public static void show(Context ctx) { + var id = ctx.pathParamAsClass("id", Long.class).get(); + var post = PostRepository.find(id) + .orElseThrow(() -> new NotFoundResponse("Post not found")); + + var page = new PostPage(post); + ctx.render("posts/show.jte", Collections.singletonMap("page", page)); + } + + // BEGIN + public static void edit(Context ctx) { + var id = ctx.pathParamAsClass("id", Long.class).get(); + var post = PostRepository.find(id) + .orElseThrow(() -> new NotFoundResponse("Entity with id = " + id + " not found")); + var page = new PostPage(post); + ctx.render("posts/edit.jte", Collections.singletonMap("page", page)); + } + // END +} diff --git a/java-web-ru/mvc/src/main/java/exercise/util/NamedRoutes.java b/java-web-ru/mvc/src/main/java/exercise/util/NamedRoutes.java index a0acba7..b0bb709 100644 --- a/java-web-ru/mvc/src/main/java/exercise/util/NamedRoutes.java +++ b/java-web-ru/mvc/src/main/java/exercise/util/NamedRoutes.java @@ -1,30 +1,30 @@ -package exercise.util; - -public class NamedRoutes { - - public static String rootPath() { - return "/"; - } - - public static String postsPath() { - return "/posts"; - } - - public static String buildPostPath() { - return "/posts/build"; - } - - public static String postPath(Long id) { - return postPath(String.valueOf(id)); - } - - public static String postPath(String id) { - return "/posts/" + id; - } - - // BEGIN - public static String editPostPath() { - return "/posts/{id}/edit"; - } - // END -} +package exercise.util; + +public class NamedRoutes { + + public static String rootPath() { + return "/"; + } + + public static String postsPath() { + return "/posts"; + } + + public static String buildPostPath() { + return "/posts/build"; + } + + public static String postPath(Long id) { + return postPath(String.valueOf(id)); + } + + public static String postPath(String id) { + return "/posts/" + id; + } + + // BEGIN + public static String editPostPath() { + return "/posts/{id}/edit"; + } + // END +} diff --git a/java-web-ru/mvc/src/main/jte/posts/edit.jte b/java-web-ru/mvc/src/main/jte/posts/edit.jte index f424d63..f51fa03 100644 --- a/java-web-ru/mvc/src/main/jte/posts/edit.jte +++ b/java-web-ru/mvc/src/main/jte/posts/edit.jte @@ -1,24 +1,24 @@ -@import exercise.dto.posts.PostPage -@import exercise.util.NamedRoutes -@import gg.jte.Content -@param PostPage page -@param Content content - -@template.layout.page( -content = @` -
- -
- -
-
- - -
- -
-` +@import exercise.dto.posts.PostPage +@import exercise.util.NamedRoutes +@import gg.jte.Content +@param PostPage page +@param Content content + +@template.layout.page( +content = @` +
+ +
+ +
+
+ + +
+ +
+` ) \ No newline at end of file diff --git a/java-web-ru/mvc/src/main/jte/posts/show.jte b/java-web-ru/mvc/src/main/jte/posts/show.jte index fece8d7..a1f0bcb 100644 --- a/java-web-ru/mvc/src/main/jte/posts/show.jte +++ b/java-web-ru/mvc/src/main/jte/posts/show.jte @@ -1,22 +1,22 @@ -@import exercise.dto.posts.PostPage -@import exercise.util.NamedRoutes -@param PostPage page - -@template.layout.page( - content = @` -
-
-

${page.getPost().getName()}

-
- -
- ${page.getPost().getBody()} -
- -
- Изменить -
- -
- ` -) +@import exercise.dto.posts.PostPage +@import exercise.util.NamedRoutes +@param PostPage page + +@template.layout.page( + content = @` +
+
+

${page.getPost().getName()}

+
+ +
+ ${page.getPost().getBody()} +
+ +
+ Изменить +
+ +
+ ` +)