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주차 미션 / 서버 2조 황정안 #19

Open
wants to merge 1 commit 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
47 changes: 47 additions & 0 deletions src/main/java/kuit/springbasic/controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,59 @@
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;

@Controller
@Slf4j
@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.addObject("questions",questions);
return mav;
}

@RequestMapping("/homeV2")
public ModelAndView showHomeV2(){
log.info("showHomeV2");
//뷰 이름 담아서 객체 생성해줌
ModelAndView mav=new ModelAndView("home");
Collection<Question> questions=questionRepository.findAll();
mav.addObject("questions",questions);
return mav;
}
@RequestMapping("/")
public String showHomeV3(Model model){
log.info("showHomeV3");
//뷰 이름 담아서 객체 생성해줌
Collection<Question> questions=questionRepository.findAll();
model.addAttribute("questions",questions);
//그냥 viewName으로 반환해도 RequestMapping이 어댑터 역할해서 mav로 반환해준다!!
return "home";
}

}
111 changes: 110 additions & 1 deletion src/main/java/kuit/springbasic/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
package kuit.springbasic.controller;

public class LoginController {
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.stereotype.Repository;
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");
//login 파일에 닿아야 하니
return "/user/login";
}


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

/**
* TODO: login
Expand All @@ -18,8 +47,88 @@ public class LoginController {
* loginV4 : @ModelAttribute
*/


// @RequestMapping("/user/login")
//키값을 지정해서데이터를 가져오는거임
public String loginV1(@RequestParam("userId") String userId,
@RequestParam("password") String password,
HttpServletRequest request){
log.info("loginV1");
User loggedUser =new User(userId,password);
User user=userRepository.findByUserId(userId);

if(user!=null&&user.isSameUser(loggedUser)){
HttpSession session =request.getSession();
session.setAttribute(USER_SESSION_KEY,loggedUser);
return "redirect:/";
}
return "redirect:/user/loginFailed";
}
//RequestParam은 만약 key 변수 값이 같으면 생략 가능 이렇게
//@RequestMapping("/user/login")
public String loginV2(@RequestParam String userId,
@RequestParam String password,
HttpServletRequest request){
log.info("loginV2");
User loggedUser =new User(userId,password);
User user=userRepository.findByUserId(userId);

if(user!=null&&user.isSameUser(loggedUser)){
HttpSession session =request.getSession();
session.setAttribute(USER_SESSION_KEY,loggedUser);
return "redirect:/";
}
return "redirect:/user/loginFailed";
}
//@RequestMapping("/user/login")
public String loginV3( String userId,
String password,
HttpServletRequest request){
log.info("loginV2");
User loggedUser =new User(userId,password);
User user=userRepository.findByUserId(userId);

if(user!=null&&user.isSameUser(loggedUser)){
HttpSession session =request.getSession();
session.setAttribute(USER_SESSION_KEY,loggedUser);
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,loggedInUser);
return "redirect:/";
}
return "redirect:/user/loginFailed";

}
/**
*
* TODO: logout
*/
@GetMapping("/logout")
public String logout(@ModelAttribute User loggedUser,
HttpServletRequest request){
log.info("logout");

User user=userRepository.findByUserId(loggedUser.getUserId());

HttpSession session = request.getSession();
session.removeAttribute(USER_SESSION_KEY);


return "redirect:/";
}

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

import ch.qos.logback.core.model.Model;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import kuit.springbasic.db.MemoryUserRepository;
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 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("showLoginForm");
return "/user/form";
}

/**
* TODO: createUser
* createUserV1 : @RequestParam
* createUserV2 : @ModelAttribute
*/

//@PostMapping("/signup")
public String createUserV1(@RequestParam String userId,
@RequestParam String password,
@RequestParam String name,
@RequestParam String email){
log.info("createUserV1");
User user=new User(userId,password,name,email);
userRepository.insert(user);

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



/**
* TODO: showUserList
*/

@GetMapping("/list")
public String showUserList(HttpServletRequest req){
log.info("showUserList");
HttpSession session = req.getSession();

if (UserSessionUtils.isLoggedIn(session)) {
req.setAttribute(USER_SESSION_KEY, userRepository.findAll());
return "/user/list";
}
return "/user/login";
}


/**
* TODO: showUserUpdateForm
*/
//왜인지 모르겟는데 자꾸 @RequestParam String userId 이런식으로 하면 누락되네
@GetMapping("/updateForm")
public String showUpdateForm(@RequestParam("userId") String userId,
HttpServletRequest request) {

HttpSession session = request.getSession();
User loginUser = (User) session.getAttribute("user");
User user = userRepository.findByUserId(userId);

if(user != null && user.isSameUser(loginUser)) {
return "/user/updateForm";
}

return "redirect:/";
}

/**
* TODO: updateUser
* updateUserV1 : @RequestParam
* updateUserV2 : @ModelAttribute
*/
//@RequestMapping("/update")
public String updateUserV1(@RequestParam("userId") String userId,
@RequestParam("password") String password,
@RequestParam("name") String name,
@RequestParam("email") String email) {
log.info("updateUserV1");
User user = new User(userId, password, name, email);
userRepository.update(user);

return "redirect:/user/list";
}

@RequestMapping("/update")
public String updateUserV2(@ModelAttribute User updatedUser) {
log.info("updateUserV2");

User user = new User(updatedUser.getUserId(), updatedUser.getPassword(), updatedUser.getName(), updatedUser.getEmail());
userRepository.update(user);
return "redirect:/user/list";
}


}
32 changes: 32 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,41 @@
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.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
*/

// 왜 자꾸 이렇게 안 쓰면 에러?
@RequestMapping("/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