|
| 1 | +package net.guides.springboot2.crud.controller; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +import javax.validation.Valid; |
| 8 | + |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.http.ResponseEntity; |
| 11 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 12 | +import org.springframework.web.bind.annotation.GetMapping; |
| 13 | +import org.springframework.web.bind.annotation.PathVariable; |
| 14 | +import org.springframework.web.bind.annotation.PostMapping; |
| 15 | +import org.springframework.web.bind.annotation.PutMapping; |
| 16 | +import org.springframework.web.bind.annotation.RequestBody; |
| 17 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 18 | +import org.springframework.web.bind.annotation.RestController; |
| 19 | + |
| 20 | +import net.guides.springboot2.crud.exception.ResourceNotFoundException; |
| 21 | +import net.guides.springboot2.crud.model.Employee; |
| 22 | +import net.guides.springboot2.crud.repository.EmployeeRepository; |
| 23 | + |
| 24 | +@RestController |
| 25 | +@RequestMapping("/api/v1") |
| 26 | +public class EmployeeController { |
| 27 | + @Autowired |
| 28 | + private EmployeeRepository employeeRepository; |
| 29 | + |
| 30 | + @GetMapping("/employees") |
| 31 | + public List<Employee> getAllEmployees() { |
| 32 | + return employeeRepository.findAll(); |
| 33 | + } |
| 34 | + |
| 35 | + @GetMapping("/employees/{id}") |
| 36 | + public ResponseEntity<Employee> getEmployeeById(@PathVariable(value = "id") Long employeeId) |
| 37 | + throws ResourceNotFoundException { |
| 38 | + Employee employee = employeeRepository.findById(employeeId) |
| 39 | + .orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId)); |
| 40 | + return ResponseEntity.ok().body(employee); |
| 41 | + } |
| 42 | + |
| 43 | + @PostMapping("/employees") |
| 44 | + public Employee createEmployee(@Valid @RequestBody Employee employee) { |
| 45 | + return employeeRepository.save(employee); |
| 46 | + } |
| 47 | + |
| 48 | + @PutMapping("/employees/{id}") |
| 49 | + public ResponseEntity<Employee> updateEmployee(@PathVariable(value = "id") Long employeeId, |
| 50 | + @Valid @RequestBody Employee employeeDetails) throws ResourceNotFoundException { |
| 51 | + Employee employee = employeeRepository.findById(employeeId) |
| 52 | + .orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId)); |
| 53 | + |
| 54 | + employee.setEmailId(employeeDetails.getEmailId()); |
| 55 | + employee.setLastName(employeeDetails.getLastName()); |
| 56 | + employee.setFirstName(employeeDetails.getFirstName()); |
| 57 | + final Employee updatedEmployee = employeeRepository.save(employee); |
| 58 | + return ResponseEntity.ok(updatedEmployee); |
| 59 | + } |
| 60 | + |
| 61 | + @DeleteMapping("/employees/{id}") |
| 62 | + public Map<String, Boolean> deleteEmployee(@PathVariable(value = "id") Long employeeId) |
| 63 | + throws ResourceNotFoundException { |
| 64 | + Employee employee = employeeRepository.findById(employeeId) |
| 65 | + .orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId)); |
| 66 | + |
| 67 | + employeeRepository.delete(employee); |
| 68 | + Map<String, Boolean> response = new HashMap<>(); |
| 69 | + response.put("deleted", Boolean.TRUE); |
| 70 | + return response; |
| 71 | + } |
| 72 | +} |
0 commit comments