/**
 * 
 */
package org.springframework.samples.kyuriboard.web.spring;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import org.springframework.samples.kyuriboard.domain.Board;
import org.springframework.samples.kyuriboard.domain.logic.KyuriBoardFacade;

/**
 * @author DAMI(archy712@naver.com)
 *
 */
public class ViewBoardController implements Controller {
	
	private KyuriBoardFacade kyuriBoard;

	/**
	 * @param kyuriBoard the kyuriBoard to set
	 */
	public void setKyuriBoard(KyuriBoardFacade kyuriBoard) {
		this.kyuriBoard = kyuriBoard;
	}


	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.Controller#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub

		int boardId = new Integer(request.getParameter("boardId")).intValue();
		Board board = this.kyuriBoard.getBoardByBoardId(boardId);
		
		// 조회수 증가
		int curVisited = board.getVisited();
		board.setVisited(curVisited + 1);
		this.kyuriBoard.updateBoard(board);
		
		// 메모 객체
		List memoList = this.kyuriBoard.getMemoListByBoardId(boardId);		
		
		// 모델 객체 생성		
		Map model = new HashMap();
		model.put("board", board);
		model.put("memoList", memoList);
		
		return new ModelAndView("viewBoard", model);
	}

}
