/**
 * 
 */
package org.mynonglam.dao;

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

import org.mynonglam.connectdb.ConnectionDTB;
import org.mynonglam.entitys.CommentPhoto;

/**
 * @author Dao
 *
 */
public class CoomentPhotoDAO {

	private static Connection connection;
	private static PreparedStatement statement = null;
	private ResultSet resultSet;
	/**
	 * 
	 */
	public CoomentPhotoDAO() {
		connection = ConnectionDTB.getConnection();
	}
	
	public boolean insertCommentPhoto(CommentPhoto comment) {
		boolean result = false;
		String sql = "INSERT INTO commentphoto(idcomphoto, idphoto, email, content, commentdate) VALUES (?,?,?,?,?)";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, comment.getIdcomphoto());
			statement.setString(2, comment.getLinkphoto().getIdphoto());
			statement.setString(3, comment.getAccount().getEmail());
			statement.setString(4, comment.getContent());
			statement.setString(5, comment.getCommentdate());
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}
	
	public String getIDCommentPhotoLast() {
		String result = "";
		
		String sql = "SELECT idcomphoto FROM commentphoto ORDER BY idcomphoto DESC LIMIT 0,1";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			ResultSet resultSet = statement.executeQuery();
			
			if(resultSet.next()) {
				result = resultSet.getString("idcomphoto");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	
	public boolean deleteCommentPhoto(String idcomment, String email) {
		boolean result = false;
		String sql = "DELETE FROM commentphoto WHERE idcomphoto=? AND email=?";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, idcomment);
			statement.setString(2, email);
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}

}
