package com.example.demo.data;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.demo.model.Employee;

@Controller
@RequestMapping("/emps1")
public class Controller1 {

	@Autowired
	private EntityManager em;
	
	@GetMapping
	public String readAll(Model model) {
		TypedQuery<Employee> query = em.createNamedQuery("Employee.findAll", Employee.class);
		List<Employee> emps = query.getResultList();
		model.addAttribute("emps", emps);
		return "employees";
	}
	
	// localhost:8080/emps1/101
	@GetMapping("/{id}")
	public String readOne(Model model,
			@PathVariable("id") Integer idPracownika) {
		Employee emp = em.find(Employee.class, idPracownika);
		List<Employee> emps = List.of(emp);
		model.addAttribute("emps", emps);
		return "employees";
	}
}
