diff --git a/src/main/java/kuit/springbasic/controller/HomeController.java b/src/main/java/kuit/springbasic/controller/HomeController.java index 6a59270..90fd2c8 100644 --- a/src/main/java/kuit/springbasic/controller/HomeController.java +++ b/src/main/java/kuit/springbasic/controller/HomeController.java @@ -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 questions=questionRepository.findAll(); + mav.addObject("questions",questions); + return mav; + } + + @RequestMapping("/homeV2") + public ModelAndView showHomeV2(){ + log.info("showHomeV2"); + //뷰 이름 담아서 객체 생성해줌 + ModelAndView mav=new ModelAndView("home"); + Collection questions=questionRepository.findAll(); + mav.addObject("questions",questions); + return mav; + } + @RequestMapping("/") + public String showHomeV3(Model model){ + log.info("showHomeV3"); + //뷰 이름 담아서 객체 생성해줌 + Collection questions=questionRepository.findAll(); + model.addAttribute("questions",questions); + //그냥 viewName으로 반환해도 RequestMapping이 어댑터 역할해서 mav로 반환해준다!! + return "home"; + } } diff --git a/src/main/java/kuit/springbasic/controller/LoginController.java b/src/main/java/kuit/springbasic/controller/LoginController.java index 76bf4b2..a345fa4 100644 --- a/src/main/java/kuit/springbasic/controller/LoginController.java +++ b/src/main/java/kuit/springbasic/controller/LoginController.java @@ -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 @@ -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:/"; + } } diff --git a/src/main/java/kuit/springbasic/controller/UserController.java b/src/main/java/kuit/springbasic/controller/UserController.java index dde62b9..47d1403 100644 --- a/src/main/java/kuit/springbasic/controller/UserController.java +++ b/src/main/java/kuit/springbasic/controller/UserController.java @@ -1,10 +1,37 @@ 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 @@ -12,18 +39,87 @@ public class UserController { * 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"; + } + } diff --git a/src/main/java/kuit/springbasic/controller/qna/QnaController.java b/src/main/java/kuit/springbasic/controller/qna/QnaController.java index adb7a2b..d4768cc 100644 --- a/src/main/java/kuit/springbasic/controller/qna/QnaController.java +++ b/src/main/java/kuit/springbasic/controller/qna/QnaController.java @@ -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 answers = answerRepository.findAllByQuestionId(questionId); + //설정 + request.setAttribute("question", question); + request.setAttribute("answers", answers); + return "/qna/show"; + } + } diff --git a/src/main/java/kuit/springbasic/controller/qna/QuestionController.java b/src/main/java/kuit/springbasic/controller/qna/QuestionController.java index acf6673..0186302 100644 --- a/src/main/java/kuit/springbasic/controller/qna/QuestionController.java +++ b/src/main/java/kuit/springbasic/controller/qna/QuestionController.java @@ -1,17 +1,40 @@ package kuit.springbasic.controller.qna; +import jakarta.servlet.http.HttpServletRequest; +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 QuestionController { /** * TODO: showQuestionForm */ + @RequestMapping("/form") + public String showQuestionForm(){ + log.info("showQuestionForm"); + return "/qna/form"; + } + /** * TODO: createQuestion * createQuestionV1 : @RequestParam * createQuestionV2 : @ModelAttribute */ + /** * TODO: showUpdateQuestionForm * showUpdateQuestionFormV1 : @RequestParam, HttpServletRequest, Model diff --git a/src/main/java/kuit/springbasic/db/JdbcUserRepository.java b/src/main/java/kuit/springbasic/db/JdbcUserRepository.java new file mode 100644 index 0000000..df80a58 --- /dev/null +++ b/src/main/java/kuit/springbasic/db/JdbcUserRepository.java @@ -0,0 +1,32 @@ +package kuit.springbasic.db; + +import kuit.springbasic.domain.User; + +import java.util.Collection; + +public class JdbcUserRepository implements UserRepository{ + @Override + public void insert(User user) { + + } + + @Override + public User findByUserId(String userId) { + return null; + } + + @Override + public Collection findAll() { + return null; + } + + @Override + public void changeUserInfo(User user) { + + } + + @Override + public void update(User user) { + + } +} diff --git a/src/main/java/kuit/springbasic/domain/Answer.java b/src/main/java/kuit/springbasic/domain/Answer.java index 2f2c152..2e976e4 100644 --- a/src/main/java/kuit/springbasic/domain/Answer.java +++ b/src/main/java/kuit/springbasic/domain/Answer.java @@ -1,28 +1,21 @@ package kuit.springbasic.domain; +import lombok.*; + import java.sql.Date; import java.time.LocalDate; - +@NoArgsConstructor +@AllArgsConstructor +@Getter +@Setter +@ToString public class Answer { private int answerId; private int questionId; private String writer; private String contents; private Date createdDate; - - public Answer() { - - } - - public Answer(int answerId, int questionId, String writer, String contents, Date createdDate) { - this.answerId = answerId; - this.questionId = questionId; - this.writer = writer; - this.contents = contents; - this.createdDate = createdDate; - } - public Answer(int questionId, String writer, String contents) { this.questionId = questionId; this.writer = writer; @@ -30,54 +23,4 @@ public Answer(int questionId, String writer, String contents) { this.createdDate = Date.valueOf(LocalDate.now()); } - public int getAnswerId() { - return answerId; - } - - public void setAnswerId(int answerId) { - this.answerId = answerId; - } - - public int getQuestionId() { - return questionId; - } - - public void setQuestionId(int questionId) { - this.questionId = questionId; - } - - public String getWriter() { - return writer; - } - - public void setWriter(String writer) { - this.writer = writer; - } - - public String getContents() { - return contents; - } - - public void setContents(String contents) { - this.contents = contents; - } - - public Date getCreatedDate() { - return createdDate; - } - - public void setCreatedDate(Date createdDate) { - this.createdDate = createdDate; - } - - @Override - public String toString() { - return "Answer{" + - "answerId=" + answerId + - ", questionId=" + questionId + - ", writer='" + writer + '\'' + - ", contents='" + contents + '\'' + - ", createdDate=" + createdDate + - '}'; - } } diff --git a/src/main/java/kuit/springbasic/domain/Question.java b/src/main/java/kuit/springbasic/domain/Question.java index 11f7528..ed8f5c4 100644 --- a/src/main/java/kuit/springbasic/domain/Question.java +++ b/src/main/java/kuit/springbasic/domain/Question.java @@ -1,9 +1,16 @@ package kuit.springbasic.domain; +import lombok.*; + import java.sql.Date; import java.time.LocalDate; +@NoArgsConstructor +@AllArgsConstructor +@Getter +@Setter +@ToString public class Question { private int questionId; private String writer; @@ -12,18 +19,6 @@ public class Question { private Date createdDate; private int countOfAnswer; - public Question() { - - } - - public Question(int questionId, String writer, String title, String contents, Date createdDate, int countOfAnswer) { - this.questionId = questionId; - this.writer = writer; - this.title = title; - this.contents = contents; - this.createdDate = createdDate; - this.countOfAnswer = countOfAnswer; - } public Question(String writer, String title, String contents, int countOfAnswer) { this.questionId = 0; @@ -33,55 +28,6 @@ public Question(String writer, String title, String contents, int countOfAnswer) this.createdDate = Date.valueOf(LocalDate.now()); this.countOfAnswer = countOfAnswer; } - - public int getQuestionId() { - return questionId; - } - - public void setQuestionId(int questionId) { - this.questionId = questionId; - } - - public String getWriter() { - return writer; - } - - public void setWriter(String writer) { - this.writer = writer; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getContents() { - return contents; - } - - public void setContents(String contents) { - this.contents = contents; - } - - public Date getCreatedDate() { - return createdDate; - } - - public void setCreatedDate(Date createdDate) { - this.createdDate = createdDate; - } - - public int getCountOfAnswer() { - return countOfAnswer; - } - - public void setCountOfAnswer(int countOfAnswer) { - this.countOfAnswer = countOfAnswer; - } - public void updateTitleAndContents(String title, String contents) { this.title = title; this.contents = contents; @@ -110,15 +56,4 @@ public void update(Question question) { this.countOfAnswer = question.countOfAnswer; } - @Override - public String toString() { - return "Question{" + - "questionId=" + questionId + - ", writer='" + writer + '\'' + - ", title='" + title + '\'' + - ", contents='" + contents + '\'' + - ", createdDate=" + createdDate + - ", countOfAnswer=" + countOfAnswer + - '}'; - } } diff --git a/src/main/java/kuit/springbasic/domain/User.java b/src/main/java/kuit/springbasic/domain/User.java index 0304f40..ddf9a5f 100644 --- a/src/main/java/kuit/springbasic/domain/User.java +++ b/src/main/java/kuit/springbasic/domain/User.java @@ -1,59 +1,22 @@ package kuit.springbasic.domain; +import lombok.*; + +@NoArgsConstructor +@AllArgsConstructor +@Getter +@Setter +@ToString public class User { private String userId; private String password; private String name; private String email; - - public User() { - - } - - public User(String userId, String password, String name, String email) { - this.userId = userId; - this.password = password; - this.name = name; - this.email = email; - } - public User(String userId, String password) { this.userId = userId; this.password = password; } - public String getUserId() { - return userId; - } - - public void setUserId(String userId) { - this.userId = userId; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - public void update(User updateUser) { this.password = updateUser.password; this.name = updateUser.name; @@ -76,13 +39,4 @@ public boolean isSameUser(String userId, String password) { return userId.equals(this.userId) && matchPassword(password); } - @Override - public String toString() { - return "User{" + - "userId='" + userId + '\'' + - ", password='" + password + '\'' + - ", name='" + name + '\'' + - ", email='" + email + '\'' + - '}'; - } } diff --git a/src/main/webapp/include/navigation.jspf b/src/main/webapp/include/navigation.jspf index e88381a..766faec 100644 --- a/src/main/webapp/include/navigation.jspf +++ b/src/main/webapp/include/navigation.jspf @@ -43,10 +43,10 @@ 개인정보수정 - Log-In + Log-In Sign-up - \ No newline at end of file +