package
com.demo.controller;
import
com.demo.model.Employee;
import
com.demo.repository.EmployeeRepository;
import
java.util.List;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.DeleteMapping;
import
org.springframework.web.bind.annotation.GetMapping;
import
org.springframework.web.bind.annotation.PathVariable;
import
org.springframework.web.bind.annotation.PostMapping;
import
org.springframework.web.bind.annotation.PutMapping;
import
org.springframework.web.bind.annotation.RequestBody;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping
(
"/emp"
)
public
class
EmployeeController {
@Autowired
private
EmployeeRepository employeeRepository;
@ResponseBody
@PostMapping
public
Employee
addPerson(
@RequestBody
Employee employee)
{
return
employeeRepository.save(employee);
}
@ResponseBody
@GetMapping
public
List<Employee> getAllEmployee()
{
return
employeeRepository.findAll();
}
@ResponseBody
@GetMapping
(
"/{id}"
)
public
Employee getEmployeeById(
@PathVariable
Long id)
{
return
employeeRepository.findById(id).orElse(
null
);
}
}