스프링

##반복주기 5학습 목표

* 객체 간의 관계 설정 (@OneToMany, @ManyToOne 등)

## 강의 순서

* 5-1. 회원과 질문 간의 관계 매핑 및 리팩토링

* 5-1. 질문 상세보기 기능 구현

* 5-3. 질문 수정/삭제 기능 구현

* 5-4. 수정/삭제 기능에 대한 보안 처리 및 LocalDateTime 설정

* 5-5. 답변 추가 및 답변 목록 기능 구현

* 5-6. QuestionController 중복 제거 리팩토링

* 5-7. 원격 서버에 소스 코드 배포

 

class QuestionController

package net.slipp.web;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import net.slipp.domain.Question;
import net.slipp.domain.QuestionRepository;
import net.slipp.domain.Result;
import net.slipp.domain.User;

@Controller
@RequestMapping("/questions")
public class QuestionController {

	private static final Logger log = LoggerFactory.getLogger(QuestionController.class);

	@Autowired
	private QuestionRepository questionRepository;
	
	@GetMapping("/form")
	public String form(HttpSession session){
		if(!HttpSessionUtils.isLoginUser(session)){
			return "/users/loginForm";
		}
		return "/qna/form";
	}
	
	@PostMapping("")
	public String create(HttpSession session, String title, String contents){
		if(!HttpSessionUtils.isLoginUser(session)){
			return "/users/loginForm";
		}
		log.info("create(String title, String contents) {} , {} ", title, contents);
		User sessionUser =HttpSessionUtils.getUserFromSession(session);
		Question newQuestion =new Question(sessionUser, title, contents);
		
		questionRepository.save(newQuestion);	
		return "redirect:/";
	}
	
	@GetMapping("/{id}")
	public String show(@PathVariable Long id, Model model){
		Question question=questionRepository.findOne(id);
		model.addAttribute("question", question);
		model.addAttribute("answers", question.getAnswers());
		return "/qna/show";
	}
	
	@GetMapping("/{id}/form")
	public String updateForm(@PathVariable Long id, Model model, HttpSession session){
		Question question =questionRepository.findOne(id);
		Result result=valid(session, question);
		if(!result.isValid()){
			model.addAttribute("errorMessage", result.getErrorMessage());
			return "/user/login";
		}
		model.addAttribute("question", questionRepository.findOne(id));
		return "/qna/updateForm";	
	}
		
	
	private Result valid(HttpSession session, Question question){
		if(!HttpSessionUtils.isLoginUser(session)){
			 return Result.fail("로그인이 필요합니다.");
		}
		User loginUser =HttpSessionUtils.getUserFromSession(session);
		if(!question.isSameWriter(loginUser)){
			return Result.fail("자신이 쓴 글만 수정, 삭제가 가능합니다.");
		}
		return Result.ok();
	}
	
	
	@PutMapping("/{id}")
	public String update(@PathVariable Long id, String title, String contents, Model model, HttpSession session){
		Question question =questionRepository.findOne(id);
		Result result=valid(session, question);
		if(!result.isValid()){
			model.addAttribute("errorMessage", result.getErrorMessage());
			return "/user/login";
		}
		question.update(title, contents);
		questionRepository.save(question);
		return String.format("redirect:/questions/%d", id);
	}
	
	
	@DeleteMapping("/{id}")
	public String delete(@PathVariable Long id, Model model, HttpSession session){
		Question question =questionRepository.findOne(id);
		Result result=valid(session, question);
		if(!result.isValid()){
			model.addAttribute("errorMessage" , result.getErrorMessage());
			return "/user/login";
		}	
		questionRepository.delete(id);
		return "redirect:/";
	}
	

	
}

 

login.html

{{> include/header}}
{{> include/navigation}}

<div class="container" id="main">
   <div class="col-md-6 col-md-offset-3">
      <div class="panel panel-default content-main">
          
          {{#errorMessage}}
          <div class="alert alert-danger" role="alert">{{this}}</div>
          {{/errorMessage}}
          
          <form name="question" method="post" action="/users/login">
              <div class="form-group">
                  <label for="userId">사용자 아이디</label>
                  <input class="form-control" id="userId" name="userId" placeholder="User ID">
              </div>
              <div class="form-group">
                  <label for="password">비밀번호</label>
                  <input type="password" class="form-control" id="password" name="password" placeholder="Password">
              </div>
              <button type="submit" class="btn btn-success clearfix pull-right">로그인</button>
              <div class="clearfix" />
          </form>
        </div>
    </div>
</div>

{{> include/footer}}

 

class Result

package net.slipp.domain;

public class Result {

	private boolean valid;
	
	private String errorMessage;

	private Result(boolean valid){
		this.valid=valid;
	}
	private Result(boolean valid, String erroMessage){
		this.valid=valid;
		this.errorMessage=erroMessage;
	}
	
	public boolean isValid(){
		return valid;
	}
	
	public String getErrorMessage(){
		return errorMessage;
	}

	public static Result ok(){
		return new Result(true);
	}

	public static Result fail(String errorMessage){
		return new Result(false, errorMessage);
	}
	
	
	
}


 

 

 

 

 

 

 

spring

 

about author

PHRASE

Level 60  머나먼나라

어버이는 어버이로서 존중한다. 그래서 당연히 조상을 존중하는 것이다. 조상을 존중하는 일은 멀리 있는 것이 아니다. 마치 어버이를 섬기는 마음 같이 자연히 우러나오는 마음인 것이다. -예기

댓글 ( 4)

댓글 남기기

작성