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

5주차 미션 / 서버 1조 박종익 #5

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
18 changes: 2 additions & 16 deletions src/main/java/kuit/springbasic/controller/HomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;
Expand All @@ -22,7 +23,7 @@ public class HomeController {

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

ModelAndView modelAndView = new ModelAndView("home");
Expand All @@ -32,20 +33,6 @@ public ModelAndView showHomeV1(HttpServletRequest request, HttpServletResponse r

return modelAndView;
}

@RequestMapping("/homeV2")
// @RequestMapping("/")
public ModelAndView showHomeV2() {
log.info("HomeController.homeV2");

ModelAndView modelAndView = new ModelAndView("home");

List<Question> questions = memoryQuestionRepository.findAll();
modelAndView.addObject("questions", questions);

return modelAndView;
}

@RequestMapping("/")
public String showHomeV3(Model model) {
log.info("HomeController.homeV3");
Expand All @@ -55,5 +42,4 @@ public String showHomeV3(Model model) {

return "home";
}

}
62 changes: 62 additions & 0 deletions src/main/java/kuit/springbasic/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,69 @@
package kuit.springbasic.controller;

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

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

private final MemoryUserRepository memoryUserRepository;

@RequestMapping("/loginForm")
public String showLoginForm() {
log.info("LoginController.loginForm");
return "/user/login";
}

@RequestMapping("/login_failed")
public String showLoginFailed() {
log.info("LoginController.loginFailed");
return "/user/loginFailed";
}

@RequestMapping("/v1/login")
public String doLoginV1(@RequestParam("userId") String userId,
@RequestParam("password") String password, HttpServletRequest req) {
log.info("LoginController.doLoginV1");
User user = memoryUserRepository.findByUserId(userId);
if (user != null && user.isSameUser(userId, password)) {
req.getSession().setAttribute("user", user);
return "redirect:/";
}
return "redirect:/user/login_failed";
}

@RequestMapping("/login")
public String doLoginV2(@ModelAttribute User accessUser, HttpServletRequest req) {
log.info("LoginController.doLoginV2");
User user = memoryUserRepository.findByUserId(accessUser.getUserId());
if (user != null && user.isSameUser(accessUser.getUserId(), accessUser.getPassword())) {
req.getSession().setAttribute("user", user);
return "redirect:/";
}
return "redirect:/user/login_failed";
}

@RequestMapping("/logout")
public String logout(HttpServletRequest req) {
HttpSession session =req.getSession();
session.removeAttribute("user");
return "redirect:/";
}





/**
* TODO: showLoginForm
Expand Down
99 changes: 97 additions & 2 deletions src/main/java/kuit/springbasic/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,119 @@
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.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.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

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

private final MemoryUserRepository memoryUserRepository;
@RequestMapping("/form")
public String showUserForm() {
log.info("UserController.showUserForm");
return "/user/form";
}
@RequestMapping("/signup")
public String createUserV1(@RequestParam("userId") String userId, @RequestParam("password") String password,
@RequestParam("name") String name, @RequestParam("email") String email) {
log.info("UserController.createUserV1");
User user = new User(userId, password, name, email);
memoryUserRepository.insert(user);
return "redirect:/user/userList";
}

@RequestMapping("/v2/signup")
public String createUserV2(@ModelAttribute User newbie, HttpServletRequest req) {
log.info("UserController.createUserV2");
User user = new User(newbie.getUserId(), newbie.getPassword(), newbie.getName(), newbie.getEmail());
memoryUserRepository.insert(user);
return "redirect:/user/userList";
}

@RequestMapping("/userList")
public String showUserListV1(HttpServletRequest req) {
log.info("UserController.showUserListV1");
HttpSession session = req.getSession();
if(UserSessionUtils.isLoggedIn(session)) {
req.setAttribute("users", memoryUserRepository.findAll());
return "/user/list";
}
return "redirect:/user/loginForm";
}
@RequestMapping("/v2/userList")
public ModelAndView showUserListV2(HttpServletRequest req) {
log.info("UserController.showUserListV2");
HttpSession session = req.getSession();
if(UserSessionUtils.isLoggedIn(session)) {
ModelAndView mav = new ModelAndView("/user/list");
mav.addObject("users", memoryUserRepository.findAll());
return mav;
}
return new ModelAndView("redirect:/user/loginForm");
}

@RequestMapping("/updateForm")
public String showUpdateUserForm(@RequestParam("userId") String userId, HttpServletRequest req) {
User user = memoryUserRepository.findByUserId(userId);
if(user != null) {
req.setAttribute("user", user);
return "/user/updateForm";
}
return "redirect:/";
}

//입력되는 값들을 넘겨줘야 하기 때문에 setAttribute 사용해주기.
@RequestMapping("/v1/update")
public String updateUserV1(HttpServletRequest req) {
log.info("UserController.updateUserV1");
User user = new User(req.getParameter("userId"), req.getParameter("password"),
req.getParameter("name"), req.getParameter("email"));
memoryUserRepository.changeUserInfo(user);
return "redirect:/";
}

@RequestMapping("/update")
public String updateUserV2(@ModelAttribute User originUser, HttpServletRequest req) {
log.info("UserController.updateUserV2");
User user = new User(originUser.getUserId(), originUser.getPassword(), originUser.getName(), originUser.getEmail());
memoryUserRepository.update(user);
return "redirect:/";
}



/**
* TODO: showUserForm
* 완료
*/

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

/**
* TODO: showUserList
* 완료
*/

/**
* TODO: showUserUpdateForm
* 완료
*/

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package kuit.springbasic.controller.qna;

import kuit.springbasic.db.MemoryAnswerRepository;
import kuit.springbasic.db.MemoryQuestionRepository;
import kuit.springbasic.domain.Answer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

MemoryAnswerRepository memoryAnswerRepository;

/**
* TODO: addAnswer - @PostMapping
Expand Down
45 changes: 44 additions & 1 deletion src/main/java/kuit/springbasic/controller/qna/QnAController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
package kuit.springbasic.controller.qna;

import kuit.springbasic.db.MemoryAnswerRepository;
import kuit.springbasic.db.MemoryQuestionRepository;
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.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

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

private final MemoryQuestionRepository memoryQuestionRepository;
private final MemoryAnswerRepository memoryAnswerRepository;

@RequestMapping("/form")
public String showQnAForm() {
log.info("QnAController.showQnAForm");
return "/qna/form";
}

@RequestMapping(value = "/show", params = "questionId")
public ModelAndView showQnA(@RequestParam("questionId") int questionId) {
log.info("QnAController.showQnA");
ModelAndView mav = new ModelAndView("qna/show");
mav.addObject("question", memoryQuestionRepository.findByQuestionId(questionId)).
addObject("answers", memoryAnswerRepository.findAllByQuestionId(questionId));
return mav;
}

@PostMapping("/create")
public String createQnA(@ModelAttribute Question newQuestion) {
log.info("QnAController.createQnA");
memoryQuestionRepository.insert(newQuestion);
return "redirect:/";
}

/**
* TODO: showQnA
*/


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;

@Repository
public class MemoryAnswerRepository {

private Map<String, Answer> answers= new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@


import kuit.springbasic.domain.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class MemoryQuestionRepository {
private Map<String, Question> questions= new HashMap<>();
private static int PK = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository
public class MemoryUserRepository {
private Map<String, User> users = new HashMap<>();
private static MemoryUserRepository memoryUserRepository;
Expand Down
35 changes: 22 additions & 13 deletions src/main/java/kuit/springbasic/domain/Question.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package kuit.springbasic.domain;


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.time.LocalDate;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Question {
private int questionId;
private String writer;
Expand All @@ -12,18 +21,18 @@ 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() {
//
// }
//
// 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;
Expand Down
Loading