Skip to content

Commit

Permalink
[MVC 구현하기 - 3단계] 가비 미션 제출합니다. (#609)
Browse files Browse the repository at this point in the history
* feat: JSONView 작성

* feat: Legacy MVC 제거

* refactor: 매직넘버 추출

* chore: 개행 제거

* refactor: 변수명 변경

* refactor: API 명세 기존과 맞도록 통일

* refactor: model에 데이터가 하나일 경우 값을 그대로 출력하도록 변경

* refactor: @controller 내 경로 명세 제거

* refactor: logout 내 view 제거
  • Loading branch information
iamjooon2 authored Sep 25, 2023
1 parent 90211d9 commit 73baf44
Show file tree
Hide file tree
Showing 26 changed files with 208 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import web.org.springframework.web.WebApplicationInitializer;
import webmvc.org.springframework.web.servlet.mvc.DispatcherServlet;

/**
* Base class for {@link WebApplicationInitializer}
Expand Down
44 changes: 0 additions & 44 deletions app/src/main/java/com/techcourse/ManualHandlerMapping.java

This file was deleted.

25 changes: 0 additions & 25 deletions app/src/main/java/com/techcourse/ManulHandlerAdapter.java

This file was deleted.

24 changes: 24 additions & 0 deletions app/src/main/java/com/techcourse/controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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 HomeController {

@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView showIndex(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView(new JspView("index.jsp"));
}

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView show(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView(new JspView("index.jsp"));
}

}
55 changes: 55 additions & 0 deletions app/src/main/java/com/techcourse/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.techcourse.controller;

import com.techcourse.domain.User;
import com.techcourse.repository.InMemoryUserRepository;
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 LoginController {
private static final String REDIRECT_HOME = "redirect:/index.jsp";
private static final String REDIRECT_UNAUTHORIZED = "redirect:/401.jsp";

private static final Logger log = LoggerFactory.getLogger(LoginController.class);

@RequestMapping(value = "/login/view", method = RequestMethod.GET)
public ModelAndView show(HttpServletRequest request, HttpServletResponse response) {
return UserSession.getUserFrom(request.getSession())
.map(user -> {
log.info("logged in {}", user.getAccount());
return new ModelAndView(new JspView(REDIRECT_HOME));
})
.orElse(new ModelAndView(new JspView("/login.jsp")));
}

@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
if (UserSession.isLoggedIn(request.getSession())) {
return new ModelAndView(new JspView(REDIRECT_HOME));
}

return InMemoryUserRepository.findByAccount(request.getParameter("account"))
.map(user -> {
log.info("User : {}", user);
return doLogin(request, user);
})
.orElse(new ModelAndView(new JspView(REDIRECT_UNAUTHORIZED)));
}

private ModelAndView doLogin(HttpServletRequest request, 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_HOME));
} else {
return new ModelAndView(new JspView(REDIRECT_UNAUTHORIZED));
}
}
}
21 changes: 21 additions & 0 deletions app/src/main/java/com/techcourse/controller/LogoutController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 LogoutController {

@RequestMapping(value = "/logout", method = RequestMethod.GET)
public ModelAndView show(HttpServletRequest request, HttpServletResponse response) {
final var session = request.getSession();
session.removeAttribute(UserSession.SESSION_KEY);
return new ModelAndView(new JspView("redirect:/"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.techcourse.controller;

import com.techcourse.domain.User;
import com.techcourse.repository.InMemoryUserRepository;
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 RegisterController {

@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView register(HttpServletRequest req, HttpServletResponse res) {
final var user = new User(2,
req.getParameter("account"),
req.getParameter("password"),
req.getParameter("email"));
InMemoryUserRepository.save(user);

return new ModelAndView(new JspView("redirect:/index.jsp"));
}

@RequestMapping(value = "/register/view", method = RequestMethod.GET)
public ModelAndView show(HttpServletRequest req, HttpServletResponse res) {
return new ModelAndView(new JspView("/register.jsp"));
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.techcourse.controller.annotation;
package com.techcourse.controller;

import context.org.springframework.stereotype.Controller;
import jakarta.servlet.http.HttpServletRequest;
Expand Down
34 changes: 34 additions & 0 deletions app/src/main/java/com/techcourse/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.techcourse.controller;

import com.techcourse.domain.User;
import com.techcourse.repository.InMemoryUserRepository;
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.JsonView;


@Controller
public class UserController {

private static final Logger log = LoggerFactory.getLogger(UserController.class);

@RequestMapping(value = "/api/user", method = RequestMethod.GET)
public ModelAndView show(HttpServletRequest request, HttpServletResponse response) {
String account = request.getParameter("account");
log.debug("user id : {}", account);

ModelAndView modelAndView = new ModelAndView(new JsonView());
User user = InMemoryUserRepository.findByAccount(account)
.orElseThrow();

modelAndView.addObject("user", user);
return modelAndView;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.techcourse.controller.legacy;
package com.techcourse.controller;

import com.techcourse.domain.User;
import jakarta.servlet.http.HttpSession;
Expand All @@ -9,6 +9,9 @@ public class UserSession {

public static final String SESSION_KEY = "user";

private UserSession() {
}

public static Optional<User> getUserFrom(final HttpSession session) {
final var user = (User) session.getAttribute(SESSION_KEY);
return Optional.ofNullable(user);
Expand All @@ -18,5 +21,4 @@ public static boolean isLoggedIn(final HttpSession session) {
return getUserFrom(session).isPresent();
}

private UserSession() {}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package webmvc.org.springframework.web.servlet.mvc.tobe;
package webmvc.org.springframework.web.servlet.mvc;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand Down
Loading

0 comments on commit 73baf44

Please sign in to comment.