package idc.server.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.IdcCmd;
import idc.model.IdcDAO;

public class IdcServerDeleteCmd implements IdcCmd{
	
	IdcDAO dao;
	
	public IdcServerDeleteCmd() {
		dao = new IdcDAO();
	}
	
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		//checkToken 값이 다르면 true반환.
		if(dao.checkToken(request)){
			request.setAttribute("reError", dao.reError);
			return;
		}else{
			//토큰이 같아서 등록성공이 될 경우 토큰을 삭제해 준다.
			request.getSession().removeAttribute("token");
		}
		
		String server_num = request.getParameter("server_num");		
		idcServerDelete(server_num);
		
	}
	
	//서버 삭제기능
	public void idcServerDelete(String server_num){
		Connection conn = null;
		PreparedStatement pstmt = null;		
		
		try {
			conn = dao.getConn();
			
			String sql = "DELETE FROM serverDB WHERE server_num=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, Integer.parseInt(server_num));
			
			pstmt.execute();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}