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

7주차 미션 / 서버 3조 김나은 #9

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
48 changes: 48 additions & 0 deletions src/main/java/kuit/springbasic/controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,60 @@
package kuit.springbasic.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import kuit.springbasic.db.QuestionRepository;
import kuit.springbasic.domain.Question;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Collection;

@Slf4j
@Controller
@RequiredArgsConstructor
public class HomeController {

private final QuestionRepository questionRepository;

/**
* TODO: showHome
* showHomeV1 : parameter - HttpServletRequest, HttpServletResponse / return - ModelAndView
* showHomeV2 : parameter - none / return - ModelAndView
* showHomeV3 : parameter - Model / return - String
*/

@RequestMapping("/homeV1")
public ModelAndView showHomeV1(HttpServletRequest request, HttpServletResponse response) {
log.info("showHomeV1");
ModelAndView mav = new ModelAndView("home");

Collection<Question> questions = questionRepository.findAll();
// mav.getModel().put("questions", questions);
mav.addObject("questions", questions);
return mav;
}

@RequestMapping("/homeV2")
public ModelAndView showHomeV2() {
log.info("showHomeV2");
ModelAndView mav = new ModelAndView("home");

Collection<Question> questions = questionRepository.findAll();
// mav.getModel().put("questions", questions);
mav.addObject("questions", questions);
return mav;
}

@RequestMapping("/")
public String showHomeV3(Model model) {
log.info("showHomeV3");
Collection<Question> questions = questionRepository.findAll();
model.addAttribute("questions", questions);
return "home";
}
}
84 changes: 84 additions & 0 deletions src/main/java/kuit/springbasic/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
package kuit.springbasic.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import kuit.springbasic.db.UserRepository;
import kuit.springbasic.domain.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import static kuit.springbasic.util.UserSessionUtils.USER_SESSION_KEY;

@Slf4j
@Controller
@RequiredArgsConstructor
@RequestMapping("/user")
public class LoginController {

private final UserRepository userRepository;

/**
* TODO: showLoginForm
*/
// @RequestMapping(value = "/loginForm", method = RequestMethod.GET)
@GetMapping("/loginForm")
public String showLoginForm() {
log.info("showLoginForm");
return "/user/login";
}

/**
* TODO: showLoginFailed
*/
@RequestMapping("/loginFailed")
public String showLoginFailed() {
log.info("showLoginFailed");
return "/user/loginFailed";
}

/**
* TODO: login
Expand All @@ -17,9 +45,65 @@ public class LoginController {
* loginV3 : @RequestParam 생략(비추천)
* loginV4 : @ModelAttribute
*/
// @RequestMapping("/login")
public String loginV1(@RequestParam("userId") String userId,
@RequestParam("password") String password,
HttpServletRequest request) {
log.info("loginV1");
User loggedInUser = new User(userId, password);
User user = userRepository.findByUserId(userId);

if (user != null && user.isSameUser(loggedInUser)) {
HttpSession session = request.getSession();
session.setAttribute(USER_SESSION_KEY, user);
return "redirect:/";
}

return "redirect:/user/loginFailed";
}

// @RequestMapping("/login")
public String loginV2(@RequestParam String userId,
@RequestParam String password,
HttpServletRequest request) {
log.info("loginV2");
User loggedInUser = new User(userId, password);
User user = userRepository.findByUserId(userId);

if (user != null && user.isSameUser(loggedInUser)) {
HttpSession session = request.getSession();
session.setAttribute(USER_SESSION_KEY, user);
return "redirect:/";
}

return "redirect:/user/loginFailed";
}

// @RequestMapping(value = "/login", method = RequestMethod.POST)
@PostMapping("/login")
public String loginV4(@ModelAttribute User loggedInUser,
HttpServletRequest request) {
log.info("loginV4");
User user = userRepository.findByUserId(loggedInUser.getUserId());

if (user != null && user.isSameUser(loggedInUser)) {
HttpSession session = request.getSession();
session.setAttribute(USER_SESSION_KEY, user);
return "redirect:/";
}

return "redirect:/user/loginFailed";
}

/**
* TODO: logout
*/
@GetMapping("/logout")
public String logout(HttpServletRequest request) {
log.info("logout");
HttpSession session = request.getSession();
session.removeAttribute(USER_SESSION_KEY);
return "redirect:/";
}

}
84 changes: 84 additions & 0 deletions src/main/java/kuit/springbasic/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,113 @@
package kuit.springbasic.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import kuit.springbasic.db.UserRepository;
import kuit.springbasic.domain.User;
import kuit.springbasic.util.UserSessionUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import static kuit.springbasic.util.UserSessionUtils.USER_SESSION_KEY;

@Slf4j
@Controller
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {

private final UserRepository userRepository;

/**
* TODO: showUserForm
*/
@GetMapping("/form")
public String showUserForm() {
log.info("showUserForm");
return "user/form";
}

/**
* TODO: createUser
* createUserV1 : @RequestParam
* createUserV2 : @ModelAttribute
*/
// @PostMapping("/signup")
public String createUserV1(@RequestParam("userId") String userId,
@RequestParam("password") String password,
@RequestParam("name") String name,
@RequestParam("email") String email) {
log.info("createUserV1");
User createdUser = new User(userId, password, name, email);
userRepository.insert(createdUser);
return "redirect:/user/list";
Comment on lines +43 to +46

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

userId가 일치하는 회원이 존재하는지 확인하는 절차가 추가하면 좋을 것 같아요. 실제 서비스에서는 동일한 id를 갖는 회원이 존재하면 안되니까요.

}

@PostMapping("/signup")
public String createUserV2(@ModelAttribute User createdUser) {
log.info("createUserV2");
userRepository.insert(createdUser);
return "redirect:/user/list";
}

/**
* TODO: showUserList
*/
@GetMapping("/list")
public String showUserList(HttpServletRequest request) {
log.info("showUserList");

HttpSession session = request.getSession();
if (UserSessionUtils.isLoggedIn(session)) {
request.setAttribute("users", userRepository.findAll());
return "/user/list";
}
return "/user/login";
}

/**
* TODO: showUserUpdateForm
*/
@GetMapping("/updateForm")
public String showUpdateForm(@RequestParam("userId") String userId, HttpServletRequest request) {
log.info("showUpdateForm");

User user = userRepository.findByUserId(userId);
HttpSession session = request.getSession();
Object loggedInUser = session.getAttribute(USER_SESSION_KEY);

if (user != null && loggedInUser != null) {
if (user.isSameUser((User) loggedInUser)) {
return "/user/updateForm";
}
}
return "redirect:/";
}

/**
* TODO: updateUser
* updateUserV1 : @RequestParam
* updateUserV2 : @ModelAttribute
*/
// @PostMapping("/update")
public String updateUserV1(@RequestParam("userId") String userId,
@RequestParam("password") String password,
@RequestParam("name") String name,
@RequestParam("email") String email) {
log.info("updateUserV1");
User updatedUser = new User(userId, password, name, email);
userRepository.update(updatedUser);
return "redirect:/user/list";
}
Comment on lines +96 to +104

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다른 수정 api에도 인가 처리를 구현하면 좋을 것 같아요


@PostMapping("/update")
public String updateUserV2(@ModelAttribute User updatedUser) {
log.info("updateUserV2");
userRepository.update(updatedUser);
return "redirect:/user/list";
}

}
33 changes: 33 additions & 0 deletions src/main/java/kuit/springbasic/controller/qna/QnaController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
package kuit.springbasic.controller.qna;

import jakarta.servlet.http.HttpServletRequest;
import kuit.springbasic.db.AnswerRepository;
import kuit.springbasic.db.QuestionRepository;
import kuit.springbasic.domain.Answer;
import kuit.springbasic.domain.Question;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collection;

@Slf4j
@Controller
@RequiredArgsConstructor
@RequestMapping("/qna")
public class QnaController {

private final QuestionRepository questionRepository;
private final AnswerRepository answerRepository;

/**
* TODO: showQnA
*/
@GetMapping("/show")
public String showQnA(@RequestParam("questionId") int questionId,
HttpServletRequest request) {
log.info("showQnA");

Question question = questionRepository.findByQuestionId(questionId);
Collection<Answer> answers = answerRepository.findAllByQuestionId(questionId);
request.setAttribute("question", question);
request.setAttribute("answers", answers);

return "/qna/show";
}

}
Loading