/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util.dao;

import entities.Knjiga;
import entities.Pisac;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jelic
 */
public class PisacDAO {
    public List<Pisac> dohvatiSvePisce(){
        List<Pisac> pisci = new ArrayList<>();
        Connection conn = util.DB.getInstance().getConnection();
        try(Statement st = conn.createStatement(); 
            ResultSet rs = st.executeQuery("select * from pisci")){
            while(rs.next()){
                Pisac p = new Pisac();
                p.setIme(rs.getString("ime"));
                p.setPrezime(rs.getString("prezime"));
                p.setKor_ime(rs.getString("korime"));
                p.setMejl(rs.getString("mejl"));
                p.setDatum_rodj(rs.getDate("datumrodj").toLocalDate());
                
                pisci.add(p);
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(PisacDAO.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            util.DB.getInstance().putConnection(conn);
        }
        return pisci;
    }
    
    public List<Knjiga> knjigeOdPisca(String korime){
        List<Knjiga> knjige = new ArrayList<>();
        Connection conn = util.DB.getInstance().getConnection();
        try(PreparedStatement st = conn.prepareStatement("select * from knjige where pisac=?")){
            st.setString(1, korime);
            ResultSet rs = st.executeQuery();
            while(rs.next()){
                Knjiga k = new Knjiga();
                k.setIdk(rs.getInt("idk"));
                k.setPisac(korime);
                k.setNaziv(rs.getString("naziv"));
                k.setBrstr(rs.getInt("brstr"));
                
                knjige.add(k);
            }
        } catch (SQLException ex) {
            Logger.getLogger(PisacDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally{
            util.DB.getInstance().putConnection(conn);
        } 
        return knjige;
    }
}
