From d4763d00988438a8ad0d14d7a60e32974b8b918f Mon Sep 17 00:00:00 2001 From: kms Date: Tue, 13 Sep 2022 00:28:21 +0900 Subject: [PATCH] feat: user signup page, main page feature and api communication success --- build.gradle | 3 + .../com/io/realworld/api/MainController.java | 25 ++++++ .../realworld/api/users/UserController.java | 10 ++- .../com/io/realworld/config/WebConfig.java | 3 +- src/main/resources/application.properties | 1 - src/main/resources/static/js/userAjax.js | 25 ++++++ .../resources/templates/framents/footer.html | 14 +++ .../resources/templates/framents/header.html | 47 ++++++++++ .../resources/templates/framents/home.html | 88 +++++++++++++++++++ src/main/resources/templates/index.html | 6 ++ .../resources/templates/users/signup.html | 39 ++++++++ web/index.jsp | 16 ---- 12 files changed, 257 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/io/realworld/api/MainController.java create mode 100644 src/main/resources/static/js/userAjax.js create mode 100644 src/main/resources/templates/framents/footer.html create mode 100644 src/main/resources/templates/framents/header.html create mode 100644 src/main/resources/templates/framents/home.html create mode 100644 src/main/resources/templates/index.html create mode 100644 src/main/resources/templates/users/signup.html delete mode 100644 web/index.jsp diff --git a/build.gradle b/build.gradle index 73d5cfc..04b39ec 100644 --- a/build.gradle +++ b/build.gradle @@ -20,6 +20,9 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-test' implementation 'org.projectlombok:lombok:1.18.24' + + // render + implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:3.1.0' annotationProcessor 'org.projectlombok:lombok' // https://mvnrepository.com/artifact/org.postgresql/postgresql diff --git a/src/main/java/com/io/realworld/api/MainController.java b/src/main/java/com/io/realworld/api/MainController.java new file mode 100644 index 0000000..5d318bc --- /dev/null +++ b/src/main/java/com/io/realworld/api/MainController.java @@ -0,0 +1,25 @@ +package com.io.realworld.api; + +import com.io.realworld.DTO.UserSignupRequest; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.servlet.ModelAndView; + +@Slf4j +@Controller +public class MainController { + + @GetMapping("") + public String mainHome(){ + return "index"; + } + + @GetMapping("/register") + public String signupView(){ + return "/users/signup"; + } + +} diff --git a/src/main/java/com/io/realworld/api/users/UserController.java b/src/main/java/com/io/realworld/api/users/UserController.java index 4d423ca..a3da444 100644 --- a/src/main/java/com/io/realworld/api/users/UserController.java +++ b/src/main/java/com/io/realworld/api/users/UserController.java @@ -5,12 +5,17 @@ import com.io.realworld.DTO.UserResponse; import com.io.realworld.repository.User; import com.io.realworld.service.UserService; import com.io.realworld.service.UserServiceImpl; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; import javax.validation.Valid; @RestController +@Slf4j +@RequestMapping("/api") public class UserController { @@ -20,14 +25,15 @@ public class UserController { this.userService = userService; } - @PostMapping("/users") + @PostMapping(value = "/users") public UserResponse signup(@Valid @RequestBody UserSignupRequest userSignupRequest) { User user = userService.signup(userSignupRequest); + log.info("register"); + return UserResponse.builder().username(user.getUsername()) .email(user.getEmail()) .bio(user.getBio()) .image(user.getImage()) .build(); - } } diff --git a/src/main/java/com/io/realworld/config/WebConfig.java b/src/main/java/com/io/realworld/config/WebConfig.java index e6faa05..4f1a6a2 100644 --- a/src/main/java/com/io/realworld/config/WebConfig.java +++ b/src/main/java/com/io/realworld/config/WebConfig.java @@ -27,7 +27,7 @@ public class WebConfig { http.csrf() .disable() .authorizeRequests() - .antMatchers("/users/**","/h2-console/**").permitAll() + .antMatchers("/api/users/**", "/h2-console/**", "/**").permitAll() .anyRequest().authenticated() .and().headers().frameOptions().sameOrigin() .and() @@ -37,4 +37,5 @@ public class WebConfig { .authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED)); return http.build(); } + } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e9dd9be..79f1502 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,3 @@ -server.servlet.contextPath=/api # H2 spring.h2.console.enabled=true spring.h2.console.path=/h2-console diff --git a/src/main/resources/static/js/userAjax.js b/src/main/resources/static/js/userAjax.js new file mode 100644 index 0000000..1a420f4 --- /dev/null +++ b/src/main/resources/static/js/userAjax.js @@ -0,0 +1,25 @@ +function register() { + const username = $("#username").val(); + const password = $("#password").val(); + const email = $("#email").val(); + const userDTO = JSON.stringify({ + user: { + username: username, + password: password, + email: email, + } + }) + + $.ajax({ + url: "/api/users", + data: userDTO, + contentType: "application/json", + async: false, + type: "POST", + }).success(function (data) { + // Todo redirect home login status + console.log(JSON.stringify(data)); + window.location.href= "/index.html"; + console.log(window.location.href) + }); +} \ No newline at end of file diff --git a/src/main/resources/templates/framents/footer.html b/src/main/resources/templates/framents/footer.html new file mode 100644 index 0000000..0240d0b --- /dev/null +++ b/src/main/resources/templates/framents/footer.html @@ -0,0 +1,14 @@ + + + +
+
+ conduit + + An interactive learning project from Thinkster. Code & design licensed under MIT. + +
+
+ +
+ \ No newline at end of file diff --git a/src/main/resources/templates/framents/header.html b/src/main/resources/templates/framents/header.html new file mode 100644 index 0000000..6def7ff --- /dev/null +++ b/src/main/resources/templates/framents/header.html @@ -0,0 +1,47 @@ + + + + + + + Conduit + + + + + + + + + + + + + diff --git a/src/main/resources/templates/framents/home.html b/src/main/resources/templates/framents/home.html new file mode 100644 index 0000000..b5086fe --- /dev/null +++ b/src/main/resources/templates/framents/home.html @@ -0,0 +1,88 @@ + + + +
+ + + + + +
+
+ \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 0000000..c4671cf --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/users/signup.html b/src/main/resources/templates/users/signup.html new file mode 100644 index 0000000..6f0fb8e --- /dev/null +++ b/src/main/resources/templates/users/signup.html @@ -0,0 +1,39 @@ + + + + +
+
+
+ +
+

Sign up

+

+ Have an account? +

+ +
    +
  • That email is already taken
  • +
+ +
+
+ +
+
+ +
+
+ +
+ +
+
+ +
+
+
+ + \ No newline at end of file diff --git a/web/index.jsp b/web/index.jsp deleted file mode 100644 index adf5a1a..0000000 --- a/web/index.jsp +++ /dev/null @@ -1,16 +0,0 @@ -<%-- - Created by IntelliJ IDEA. - User: kms - Date: 2022/09/07 - Time: 10:56 PM - To change this template use File | Settings | File Templates. ---%> -<%@ page contentType="text/html;charset=UTF-8" language="java" %> - - - $Title$ - - - $END$ - -