스프링

Git-Hub    https://github.com/braverokmc79/spring_boot_demo2

 

class ApiAnswerController

package net.slipp.web;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import net.slipp.domain.Answer;
import net.slipp.domain.AnswerRepository;
import net.slipp.domain.Question;
import net.slipp.domain.QuestionRepository;
import net.slipp.domain.User;

@RestController
@RequestMapping("/api/questions/{questionId}/answers")
public class ApiAnswerController {
	
	@Autowired
	private QuestionRepository questionRepository;
	
	@Autowired
	private AnswerRepository answerRepository;
	
	@PostMapping("")
	public Answer create(@PathVariable Long questionId, String contents, HttpSession session){
		if(!HttpSessionUtils.isLoginUser(session)){
			return null;
		}
		
		User loginUser =HttpSessionUtils.getUserFromSession(session);
		Question question =questionRepository.findOne(questionId);
		Answer answer =new Answer(loginUser, question, contents);
		return answerRepository.save(answer);
	}


}

 

 

show.html

                          <form class="answer-write" method="post" action="/api/questions/{{id}}/answers">
                              <div class="form-group" style="padding:14px;">
                                  <textarea class="form-control" placeholder="Update your status" name="contents"></textarea>
                              </div>
                            
                              <input type="submit" class="btn btn-success pull-right" value="답변하기" >
                              <div class="clearfix" ></div>
                          </form>

 

 

scripts.js

   
$(".answer-write input[type=submit]").click(addAnswer);
   
function addAnswer(e){	
    	e.preventDefault();
    	console.log("click!");

		var queryString=$(".answer-write").serialize();
		console.log("query : " +queryString);
		
		var url =$(".answer-write").attr("action");
		console.log("url : " + url);
		
		$.ajax({
			type:'post',
			url :url,
			data: queryString,
			dataType :'json',
			error :onError,
			success:onSuccess
			
		});	
}


function onError(){
	console.log("error");
}

function onSuccess(data, status){
	console.log(data);
	var answerTemplate =$("#answerTemplate").html();
	var template =answerTemplate.format(data.writer.userId, data.formattedCreateDate, data.contents, data.id, data.id);
	$(".qna-comment-slipp-articles").prepend(template);
}

String.prototype.format = function() {
	  var args = arguments;
	  return this.replace(/{(\d+)}/g, function(match, number) {
	    return typeof args[number] != 'undefined'
	        ? args[number]
	        : match
	        ;
	  });
};

 

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

상(喪)을 입고 있는 자의 옆에서 식사를 할 때에는 결코 배불리 먹는 일은 없었다. 이것이 공자의 생활 태도였다. -예기

댓글 ( 4)

댓글 남기기

작성