Skip to content
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 구현하기 - 2, 3단계] 에어(김준서) 미션 제출합니다. #73

Merged
merged 7 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

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

import com.techcourse.air.core.annotation.Controller;
import com.techcourse.air.mvc.web.annotation.RequestMapping;
import com.techcourse.air.mvc.web.support.RequestMethod;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@Controller
public class HomeController {

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,39 +1,50 @@
package com.techcourse.air.controller;

import com.techcourse.air.core.annotation.Controller;
import com.techcourse.air.domain.User;
import com.techcourse.air.mvc.core.view.ModelAndView;
import com.techcourse.air.mvc.web.annotation.RequestMapping;
import com.techcourse.air.mvc.web.support.RequestMethod;
import com.techcourse.air.repository.InMemoryUserRepository;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import com.techcourse.air.mvc.core.controller.asis.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoginController implements Controller {
@Controller
public class LoginController {

private static final Logger log = LoggerFactory.getLogger(LoginController.class);
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String loginView(HttpServletRequest request, HttpServletResponse response) {
return UserSession.getUserFrom(request.getSession())
.map(user -> "redirect:/index.jsp")
.orElse("/login.jsp");
}

@Override
public String execute(HttpServletRequest req, HttpServletResponse res) throws Exception {
if (UserSession.isLoggedIn(req.getSession())) {
return "redirect:/index.jsp";
@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
if (UserSession.isLoggedIn(request.getSession())) {
return new ModelAndView("redirect:/index.jsp");
}
return InMemoryUserRepository.findByAccount(request.getParameter("account"))
.map(user -> login(request, user))
.orElse(new ModelAndView("redirect:/401.jsp"));
}

return InMemoryUserRepository.findByAccount(req.getParameter("account"))
.map(user -> {
log.info("User : {}", user);
return login(req, user);
})
.orElse("redirect:/401.jsp");
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logout(HttpServletRequest req, HttpServletResponse res) {
final HttpSession session = req.getSession();
session.removeAttribute(UserSession.SESSION_KEY);
return "redirect:/";
}

private String login(HttpServletRequest request, User user) {
private ModelAndView login(HttpServletRequest request, User user) {
if (user.checkPassword(request.getParameter("password"))) {
final HttpSession session = request.getSession();
session.setAttribute(UserSession.SESSION_KEY, user);
return "redirect:/index.jsp";
return new ModelAndView("redirect:/index.jsp");
} else {
return "redirect:/401.jsp";
return new ModelAndView("redirect:/401.jsp");
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package com.techcourse.air.controller;

import com.techcourse.air.core.annotation.Controller;
import com.techcourse.air.domain.User;
import com.techcourse.air.mvc.web.annotation.RequestMapping;
import com.techcourse.air.mvc.web.support.RequestMethod;
import com.techcourse.air.repository.InMemoryUserRepository;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import com.techcourse.air.mvc.core.controller.asis.Controller;

public class RegisterController implements Controller {
@Controller
public class RegisterController {

@RequestMapping(value = "/register", method = RequestMethod.GET)
public String registerView(HttpServletRequest req, HttpServletResponse res) {
return "/register.jsp";
}

@Override
public String execute(HttpServletRequest req, HttpServletResponse res) throws Exception {
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String register(HttpServletRequest req, HttpServletResponse res) {
final User user = new User(2,
req.getParameter("account"),
req.getParameter("password"),
Expand Down

This file was deleted.

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

import com.techcourse.air.core.annotation.Controller;
import com.techcourse.air.domain.User;
import com.techcourse.air.mvc.core.view.JsonView;
import com.techcourse.air.mvc.core.view.ModelAndView;
import com.techcourse.air.mvc.web.annotation.RequestMapping;
import com.techcourse.air.mvc.web.annotation.ResponseBody;
import com.techcourse.air.mvc.web.support.RequestMethod;
import com.techcourse.air.repository.InMemoryUserRepository;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
public class UserController {

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

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

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

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

// test용 GET /api/user/v2?account=gugu
@RequestMapping(value = "/api/user/v2", method = RequestMethod.GET)
@ResponseBody
public User show2(HttpServletRequest request, HttpServletResponse response) {
final String account = request.getParameter("account");
log.debug("user id : {}", account);

return InMemoryUserRepository.findByAccount(account)
.orElseThrow();
}

// test용 GET /api/ok
@RequestMapping(value = "/api/ok", method = RequestMethod.GET)
@ResponseBody
public String ok(HttpServletRequest request, HttpServletResponse response) {
return "ok";
}
}
13 changes: 12 additions & 1 deletion app/src/main/java/com/techcourse/air/domain/User.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.techcourse.air.domain;

public class User {

private final long id;
private final String account;
private final String password;
Expand All @@ -18,10 +17,22 @@ public boolean checkPassword(String password) {
return this.password.equals(password);
}

public long getId() {
return id;
}

public String getAccount() {
return account;
}

public String getPassword() {
return password;
}

public String getEmail() {
return email;
}

@Override
public String toString() {
return "User{" +
Expand Down
2 changes: 1 addition & 1 deletion app/webapp/login.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</form>
</div>
<div class="card-footer text-center py-3">
<div class="small"><a href="/register/view">아이디가 없나요? 회원가입 하러가기</a></div>
<div class="small"><a href="/register">아이디가 없나요? 회원가입 하러가기</a></div>
</div>
</div>
</div>
Expand Down
Loading