package idc.board.command;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import idc.model.IdcDAO;

public class NoteBookDeleteCmd implements BoardCmd{

	BoardDAO dao = new BoardDAO();
	private IdcDAO daodao = new IdcDAO();
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		//checkToken 값이 다르면 true반환.
		if(daodao.checkToken(request))
		{
			request.setAttribute("reError", daodao.reError);
			return;
		}
		else
		{
			//토큰이 같아서 등록성공이 될 경우 토큰을 삭제해 준다.
			request.getSession().removeAttribute("token");
		}

		String num = request.getParameter("num");
		deleteNote(num);
		
	}
	
	//게시글 삭제 기능 수행
	public void deleteNote(String inputNum){
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		try {
			conn = dao.getConn();									
			String sql = "DELETE FROM noteDB WHERE num=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, Integer.parseInt(inputNum));
			
			pstmt.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
