/**
 * 
 */
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.Liking;

/**
 * @author Dao
 *
 */
public class LikingDAO {
	
	private static Connection connection;
	private static PreparedStatement statement = null;
	private ResultSet resultSet;

	/**
	 * 
	 */
	public LikingDAO() {
		connection = ConnectionDTB.getConnection();
	}

	public Liking getLinkingForAccount(String idacc) {
		Liking result = null;
		
		String sql = "SELECT hobbies, musics, movies, books FROM liking where email=?";
		
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, idacc);
			
			ResultSet resultSet = statement.executeQuery();
			if(resultSet.next()) {
				result = new Liking();
				result.setHobbies(resultSet.getString("hobbies"));
				result.setMusics(resultSet.getString("musics"));
				result.setMovies(resultSet.getString("movies"));
				result.setBooks(resultSet.getString("books"));
			}
		} catch (SQLException e) {
			result = null;
			e.printStackTrace();
		}
		
		return result;
	}
	
	public boolean updateLikingForAccount(Liking liking) {
		boolean result = false;
		String sql = "UPDATE liking SET hobbies=?, musics=?, movies=?, books=? WHERE email=?";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, liking.getHobbies());
			statement.setString(2, liking.getMusics());
			statement.setString(3, liking.getMovies());
			statement.setString(4, liking.getBooks());
			statement.setString(5, liking.getEmail());
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}
	
	public boolean insertLikingForAccount(Liking liking) {
		boolean result = false;
		String sql = "INSERT INTO liking(email, hobbies, musics, movies, books) VALUES (?,?,?,?,?)";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, liking.getEmail());
			statement.setString(2, liking.getHobbies());
			statement.setString(3, liking.getMusics());
			statement.setString(4, liking.getMovies());
			statement.setString(5, liking.getBooks());
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}
	
}
