From 116a9d7909a674884537780c9731aa670d000425 Mon Sep 17 00:00:00 2001 From: kevstevie Date: Mon, 25 Sep 2023 15:03:15 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=BB=A8=ED=8A=B8=EB=A1=A4?= =?UTF-8?q?=EB=9F=AC=20=ED=86=B5=ED=95=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/LoginController.java | 17 +++++++++--- .../controller/LoginViewController.java | 27 ------------------- .../controller/RegisterController.java | 7 ++++- .../controller/RegisterViewController.java | 18 ------------- 4 files changed, 20 insertions(+), 49 deletions(-) delete mode 100644 app/src/main/java/com/techcourse/controller/LoginViewController.java delete mode 100644 app/src/main/java/com/techcourse/controller/RegisterViewController.java diff --git a/app/src/main/java/com/techcourse/controller/LoginController.java b/app/src/main/java/com/techcourse/controller/LoginController.java index 478af6c398..63067e3340 100644 --- a/app/src/main/java/com/techcourse/controller/LoginController.java +++ b/app/src/main/java/com/techcourse/controller/LoginController.java @@ -16,11 +16,22 @@ public class LoginController { private static final Logger log = LoggerFactory.getLogger(LoginController.class); + private static final String REDIRECT_INDEX_JSP = "redirect:/index.jsp"; + + @RequestMapping(value = "/login", method = RequestMethod.GET) + public ModelAndView loginView(final HttpServletRequest req, final HttpServletResponse res) { + return UserSession.getUserFrom(req.getSession()) + .map(user -> { + log.info("logged in {}", user.getAccount()); + return new ModelAndView(new JspView(REDIRECT_INDEX_JSP)); + }) + .orElse(new ModelAndView(new JspView("/login.jsp"))); + } @RequestMapping(value = "/login", method = RequestMethod.POST) - public ModelAndView execute(final HttpServletRequest req, final HttpServletResponse res) { + public ModelAndView login(final HttpServletRequest req, final HttpServletResponse res) { if (UserSession.isLoggedIn(req.getSession())) { - return new ModelAndView(new JspView("redirect:/index.jsp")); + return new ModelAndView(new JspView(REDIRECT_INDEX_JSP)); } return InMemoryUserRepository.findByAccount(req.getParameter("account")) @@ -35,7 +46,7 @@ private ModelAndView login(final HttpServletRequest request, final User user) { if (user.checkPassword(request.getParameter("password"))) { final var session = request.getSession(); session.setAttribute(UserSession.SESSION_KEY, user); - return new ModelAndView(new JspView("redirect:/index.jsp")); + return new ModelAndView(new JspView(REDIRECT_INDEX_JSP)); } return new ModelAndView(new JspView("redirect:/401.jsp")); } diff --git a/app/src/main/java/com/techcourse/controller/LoginViewController.java b/app/src/main/java/com/techcourse/controller/LoginViewController.java deleted file mode 100644 index 52789e15d1..0000000000 --- a/app/src/main/java/com/techcourse/controller/LoginViewController.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.techcourse.controller; - -import context.org.springframework.stereotype.Controller; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import web.org.springframework.web.bind.annotation.RequestMapping; -import web.org.springframework.web.bind.annotation.RequestMethod; -import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.view.JspView; - -@Controller -public class LoginViewController { - - private static final Logger log = LoggerFactory.getLogger(LoginViewController.class); - - @RequestMapping(value = "/login", method = RequestMethod.GET) - public ModelAndView execute(final HttpServletRequest req, final HttpServletResponse res) { - return UserSession.getUserFrom(req.getSession()) - .map(user -> { - log.info("logged in {}", user.getAccount()); - return new ModelAndView(new JspView("redirect:/index.jsp")); - }) - .orElse(new ModelAndView(new JspView("/login.jsp"))); - } -} diff --git a/app/src/main/java/com/techcourse/controller/RegisterController.java b/app/src/main/java/com/techcourse/controller/RegisterController.java index 6f44e8a402..d70582a762 100644 --- a/app/src/main/java/com/techcourse/controller/RegisterController.java +++ b/app/src/main/java/com/techcourse/controller/RegisterController.java @@ -14,7 +14,7 @@ public class RegisterController { @RequestMapping(value = "/register", method = RequestMethod.POST) - public ModelAndView execute(final HttpServletRequest req, final HttpServletResponse res) { + public ModelAndView register(final HttpServletRequest req, final HttpServletResponse res) { final var user = new User(2, req.getParameter("account"), req.getParameter("password"), @@ -23,4 +23,9 @@ public ModelAndView execute(final HttpServletRequest req, final HttpServletRespo return new ModelAndView(new JspView("/index.jsp")); } + + @RequestMapping(value = "/register", method = RequestMethod.GET) + public ModelAndView registerView(final HttpServletRequest req, final HttpServletResponse res) { + return new ModelAndView(new JspView("/register.jsp")); + } } diff --git a/app/src/main/java/com/techcourse/controller/RegisterViewController.java b/app/src/main/java/com/techcourse/controller/RegisterViewController.java deleted file mode 100644 index fce20ba0e2..0000000000 --- a/app/src/main/java/com/techcourse/controller/RegisterViewController.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.techcourse.controller; - -import context.org.springframework.stereotype.Controller; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import web.org.springframework.web.bind.annotation.RequestMapping; -import web.org.springframework.web.bind.annotation.RequestMethod; -import webmvc.org.springframework.web.servlet.ModelAndView; -import webmvc.org.springframework.web.servlet.view.JspView; - -@Controller -public class RegisterViewController { - - @RequestMapping(value = "/register", method = RequestMethod.GET) - public ModelAndView execute(final HttpServletRequest req, final HttpServletResponse res) { - return new ModelAndView(new JspView("/register.jsp")); - } -}