-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MVC - Step 2] 효오 미션 제출합니다. #59
Changes from 9 commits
056f1d4
cd14aa4
1c03b83
c8d7ccf
f7b1f0a
1e7a8a5
ad3f4ef
27a3f62
aa7c258
e664f4e
1cbc878
fc5eaa3
608af68
e64d1fc
8d7ba14
d6a956a
54dc9f4
b53d113
0f22c61
85582cb
3cb42e8
5005591
73d9421
d2b6c3b
2584a09
8da33e7
7f6243d
166180b
aaf07d1
2b4f779
d13bdc3
623c867
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,43 @@ | ||
package nextstep.mvc.tobe; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import nextstep.web.support.MediaType; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.util.Map; | ||
|
||
public class JsonView implements View { | ||
private static final Logger logger = LoggerFactory.getLogger(JsonView.class); | ||
|
||
@Override | ||
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception { | ||
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); | ||
|
||
if (model.isEmpty()) { | ||
return; | ||
} | ||
|
||
String json = toJson(model); | ||
logger.debug("Json : {}", json); | ||
response.getWriter().write(json); | ||
} | ||
|
||
private String toJson(Map<String, ?> model) throws JsonProcessingException { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
|
||
if (model.size() == 1) { | ||
Object value = model.values() | ||
.stream() | ||
.findFirst() | ||
.orElseThrow(IllegalArgumentException::new); | ||
|
||
return mapper.writeValueAsString(value); | ||
} | ||
|
||
return mapper.writeValueAsString(model); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,38 @@ | ||
package slipp.controller; | ||
|
||
import slipp.domain.User; | ||
import slipp.support.db.DataBase; | ||
import nextstep.mvc.asis.Controller; | ||
import nextstep.mvc.tobe.JspView; | ||
import nextstep.mvc.tobe.ModelAndView; | ||
import nextstep.mvc.tobe.RedirectView; | ||
import nextstep.web.annotation.Controller; | ||
import nextstep.web.annotation.RequestMapping; | ||
import nextstep.web.annotation.RequestMethod; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import slipp.domain.User; | ||
import slipp.support.db.DataBase; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
public class CreateUserController implements Controller { | ||
private static final Logger log = LoggerFactory.getLogger(CreateUserController.class); | ||
@Controller | ||
public class CreateUserController { | ||
private static final Logger logger = LoggerFactory.getLogger(CreateUserController.class); | ||
|
||
@RequestMapping(value = "/users/form", method = RequestMethod.GET) | ||
public ModelAndView signUpPage(HttpServletRequest request, HttpServletResponse response) { | ||
return new ModelAndView(new JspView("/user/form.jsp")); | ||
} | ||
|
||
@RequestMapping(value = "/users/create", method = RequestMethod.POST) | ||
public ModelAndView signUp(HttpServletRequest request, HttpServletResponse response) { | ||
User user = new User(request.getParameter("userId"), | ||
request.getParameter("password"), | ||
request.getParameter("name"), | ||
request.getParameter("email")); | ||
|
||
@Override | ||
public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { | ||
User user = new User(req.getParameter("userId"), req.getParameter("password"), req.getParameter("name"), | ||
req.getParameter("email")); | ||
log.debug("User : {}", user); | ||
logger.debug("User : {}", user); | ||
|
||
DataBase.addUser(user); | ||
return "redirect:/"; | ||
return new ModelAndView(new RedirectView("/")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,24 @@ | ||
package slipp.controller; | ||
|
||
import nextstep.mvc.tobe.JspView; | ||
import nextstep.mvc.tobe.ModelAndView; | ||
import nextstep.web.annotation.Controller; | ||
import nextstep.web.annotation.RequestMapping; | ||
import nextstep.web.annotation.RequestMethod; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import slipp.support.db.DataBase; | ||
import nextstep.mvc.asis.Controller; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
public class HomeController implements Controller { | ||
@Override | ||
public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { | ||
req.setAttribute("users", DataBase.findAll()); | ||
return "home.jsp"; | ||
@Controller | ||
public class HomeController { | ||
private static final Logger logger = LoggerFactory.getLogger(HomeController.class); | ||
|
||
@RequestMapping(value = "/", method = RequestMethod.GET) | ||
public ModelAndView home(HttpServletRequest request, HttpServletResponse response) { | ||
request.setAttribute("users", DataBase.findAll()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. layer 를 분리해보면 어떨까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Service Layer 만드려고 생각은 했으나, MVC 프레임워크에 집중하려고 했습니다! |
||
return new ModelAndView(new JspView("home.jsp")); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,50 @@ | ||
package slipp.controller; | ||
|
||
import nextstep.mvc.asis.Controller; | ||
import nextstep.mvc.tobe.JspView; | ||
import nextstep.mvc.tobe.ModelAndView; | ||
import nextstep.mvc.tobe.RedirectView; | ||
import nextstep.web.annotation.Controller; | ||
import nextstep.web.annotation.RequestMapping; | ||
import nextstep.web.annotation.RequestMethod; | ||
import slipp.controller.UserSessionUtils; | ||
import slipp.domain.User; | ||
import slipp.support.db.DataBase; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpSession; | ||
|
||
public class LoginController implements Controller { | ||
@Override | ||
public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { | ||
String userId = req.getParameter("userId"); | ||
String password = req.getParameter("password"); | ||
@Controller | ||
public class LoginController { | ||
private static final String USER_ID = "userId"; | ||
private static final String PASSWORD = "password"; | ||
private static final String LOGIN_FAILED = "loginFailed"; | ||
|
||
@RequestMapping(value = "/users/loginForm", method = RequestMethod.GET) | ||
public ModelAndView loginPage(HttpServletRequest request, HttpServletResponse response) { | ||
return new ModelAndView(new JspView("/user/login.jsp")); | ||
} | ||
|
||
@RequestMapping(value = "/users/login", method = RequestMethod.POST) | ||
public ModelAndView login(HttpServletRequest request, HttpServletResponse response) { | ||
String userId = request.getParameter(USER_ID); | ||
String password = request.getParameter(PASSWORD); | ||
User user = DataBase.findUserById(userId); | ||
if (user == null) { | ||
req.setAttribute("loginFailed", true); | ||
return "/user/login.jsp"; | ||
} | ||
if (user.matchPassword(password)) { | ||
HttpSession session = req.getSession(); | ||
|
||
if (user != null && user.matchPassword(password)) { | ||
HttpSession session = request.getSession(); | ||
session.setAttribute(UserSessionUtils.USER_SESSION_KEY, user); | ||
return "redirect:/"; | ||
} else { | ||
req.setAttribute("loginFailed", true); | ||
return "/user/login.jsp"; | ||
return new ModelAndView(new RedirectView("/")); | ||
} | ||
|
||
request.setAttribute(LOGIN_FAILED, true); | ||
return new ModelAndView(new JspView("/user/login.jsp")); | ||
} | ||
|
||
@RequestMapping(value = "/users/logout", method = RequestMethod.GET) | ||
public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) { | ||
HttpSession session = request.getSession(); | ||
session.removeAttribute(UserSessionUtils.USER_SESSION_KEY); | ||
return new ModelAndView(new RedirectView("/")); | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JsonUtils라는 클래스가 있는데 해당 클래스로 옮겨보면 어떨까요 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
놓친 부분이 있었네요! 피드백 감사합니다.