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주차 미션 / 1조 문승우 #13

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

return "home";
}

}
86 changes: 86 additions & 0 deletions src/main/java/kuit/springbasic/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
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", "/login"}, method = RequestMethod.GET)
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 +44,68 @@ 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, loggedInUser);
return "redirect:/";
} else {
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, loggedInUser);
return "redirect:/";
} else {
return "redirect:/user/loginFailed";
}
}

@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:/";
} else {
return "redirect:/user/loginFailed";
}
}

/**
* TODO: logout
*/

@GetMapping("/logout")
public String logout(HttpSession session) throws IllegalAccessException {
log.info("logout");
if(session.getAttribute(USER_SESSION_KEY) != null){
session.removeAttribute(USER_SESSION_KEY);
} else {
throw new IllegalAccessException("잘못된 접근입니다.");
}

return "redirect:/";
}

}
87 changes: 87 additions & 0 deletions src/main/java/kuit/springbasic/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,116 @@
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.ui.Model;
import org.springframework.web.bind.annotation.*;

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

private final UserRepository userRepository;

/**
* TODO: showUserForm
*/
@GetMapping("/form")
public String userForm(Model model) {
log.info("sign up form");
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("sign up");
User addingUser = new User(userId, password, name, email);
userRepository.insert(addingUser);
return "redirect:/user/loginForm";
}

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

/**
* TODO: showUserList
*/

@GetMapping("/list")
public String showUserList(HttpSession session, Model model) {
log.info("show user list");

if(UserSessionUtils.isLoggedIn(session)) {
model.addAttribute("users", userRepository.findAll());
return "/user/list";
}
return "redirect:/user/loginForm";
}

/**
* TODO: showUserUpdateForm
*/
@GetMapping("/updateForm")
public String showUserUpdateForm(@RequestParam("userId") String userId, HttpSession session, Model model) {
log.info("show user update form");

User user = userRepository.findByUserId(userId);
Object value = session.getAttribute("user");

if (user != null && value != null) {
if (user.isSameUser((User)value)){ // 수정되는 user와 수정하는 user가 동일한 경우
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,
HttpSession session) {
log.info("update user");

User user = new User(userId, password, name, email);

userRepository.update(user);
return "redirect:/user/list";
}

@PostMapping("/update")
public String updateUserV2(@ModelAttribute User user, HttpSession session) {
log.info("update user");

userRepository.update(user);
return "redirect:/user/list";
}


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package kuit.springbasic.controller.qna;

import kuit.springbasic.db.AnswerRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

@Slf4j
@Controller
@RequiredArgsConstructor
public class AnswerController {

private final AnswerRepository answerRepository;

/**
* TODO: addAnswer - @PostMapping
* addAnswerV0 : @RequestParam, HttpServletResponse
Expand All @@ -10,4 +21,6 @@ public class AnswerController {
* addAnswerV3 : @ModelAttribute, @ResponseBody
*/

// @PostMapping("")

}
34 changes: 34 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,43 @@
package kuit.springbasic.controller.qna;

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

import java.util.Collection;
import java.util.List;

@Slf4j
@Controller
@RequiredArgsConstructor
public class QnaController {

private final QuestionRepository questionRepository;
private final AnswerRepository answerRepository;

/**
* TODO: showQnA
*/

@RequestMapping("/qna/show")
public String showQna(@RequestParam(name = "questionId") String questionId, Model model, HttpSession session) {
Question question = questionRepository.findByQuestionId(Integer.parseInt(questionId));
Collection<Answer> answers = answerRepository.findAllByQuestionId(question.getQuestionId());
model.addAttribute("question", question);
model.addAttribute("answers", answers);

log.info(question.getWriter());
log.info(((User)session.getAttribute("user")).getUserId());
return "/qna/show";
}

}
Loading