package idc.main.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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

public class IdcShareBoardDeleteCmd implements IdcCmd
{
	IdcDAO dao;
	public IdcShareBoardDeleteCmd()
	{
		dao = new IdcDAO();
	}
	
	//공용자료실 삭제
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		//checkToken 값이 다르면 true반환.
		if(dao.checkToken(request))
		{
			request.setAttribute("reError", dao.reError);
			return;
		}
		else
		{
			//토큰이 같아서 등록성공이 될 경우 토큰을 삭제해 준다.
			request.getSession().removeAttribute("token");
		}
		
		
		int num = Integer.parseInt(request.getParameter("num"));
		idcShareBoardDeleteCmd(num);
	}
	
	private void idcShareBoardDeleteCmd(int num)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="DELETE FROM shareboarddb WHERE num=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, num);
			pstmt.executeUpdate();
		}
		catch (Exception e) 
		{
			System.out.println("idcShareBoardDeleteCmd 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(rs!=null)rs.close();
			}
			catch (SQLException e) 
			{
				System.out.println("idcShareBoardDeleteCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	
}
