package net.sevecek.dailyplanet;

import java.sql.*;
import java.util.*;
import org.mariadb.jdbc.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.jdbc.core.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.*;

@Controller
public class HlavniController {

    @RequestMapping(value = "/hlavni.html", method = RequestMethod.GET)
    public ModelAndView zobrazHlavniStranku() throws SQLException {
        ModelAndView drzacekNaSablonuAData;
        drzacekNaSablonuAData = new ModelAndView(
                "/WEB-INF/view/hlavni.jsp");

        MariaDbDataSource dataSource;
        dataSource = new MariaDbDataSource();
        dataSource.setUrl("jdbc:mysql://localhost/DailyPlanet");
        dataSource.setUser("student");
        dataSource.setPassword("password");

        JdbcTemplate pokladacDotazu;
        pokladacDotazu = new JdbcTemplate(dataSource);

/*
        Integer pocetAutoru = pokladacDotazu.queryForObject(
                "SELECT COUNT(Autor_ID) FROM autor",
                Integer.class);

        System.out.println("Pocet autoru: " + pocetAutoru);
*/

        RowMapper<Autor> prevodnikTridyAutor;
        prevodnikTridyAutor = BeanPropertyRowMapper.newInstance(Autor.class);

        List<Autor> seznamAutoru = pokladacDotazu.query(
                "SELECT Autor_ID as ID, KrestniJmeno, Prijmeni, Email, Telefon FROM autor",
                prevodnikTridyAutor);

        drzacekNaSablonuAData.addObject("autori", seznamAutoru);
        return drzacekNaSablonuAData;
    }

}
