-
Notifications
You must be signed in to change notification settings - Fork 21
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
Nico1eKim
wants to merge
3
commits into
Konkuk-KUIT:main
Choose a base branch
from
Nico1eKim:week7-Nico1eKim
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/main/java/kuit/springbasic/controller/HomeController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/main/java/kuit/springbasic/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
@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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
33
src/main/java/kuit/springbasic/controller/qna/QnaController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
userId가 일치하는 회원이 존재하는지 확인하는 절차가 추가하면 좋을 것 같아요. 실제 서비스에서는 동일한 id를 갖는 회원이 존재하면 안되니까요.