|
1 | 1 | package com.luv2code.springboot.cruddemo.rest;
|
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
3 | 5 | import com.luv2code.springboot.cruddemo.entity.Employee;
|
4 | 6 | import com.luv2code.springboot.cruddemo.service.EmployeeService;
|
5 | 7 | import org.springframework.beans.factory.annotation.Autowired;
|
6 | 8 | import org.springframework.web.bind.annotation.*;
|
7 | 9 |
|
8 | 10 | import java.util.List;
|
| 11 | +import java.util.Map; |
9 | 12 |
|
10 | 13 | @RestController
|
11 | 14 | @RequestMapping("/api")
|
12 | 15 | public class EmployeeRestController {
|
13 | 16 |
|
14 | 17 | private EmployeeService employeeService;
|
15 | 18 |
|
| 19 | + private ObjectMapper objectMapper; |
| 20 | + |
16 | 21 | @Autowired
|
17 |
| - public EmployeeRestController(EmployeeService theEmployeeService) { |
| 22 | + public EmployeeRestController(EmployeeService theEmployeeService, ObjectMapper theObjectMapper) { |
18 | 23 | employeeService = theEmployeeService;
|
| 24 | + objectMapper = theObjectMapper; |
19 | 25 | }
|
20 | 26 |
|
21 | 27 | // expose "/employees" and return a list of employees
|
@@ -63,22 +69,43 @@ public Employee updateEmployee(@RequestBody Employee theEmployee) {
|
63 | 69 | return dbEmployee;
|
64 | 70 | }
|
65 | 71 |
|
66 |
| - // add mapping for DELETE /employees/{employeeId} - delete employee |
| 72 | + // add mapping for PATCH /employees/{employeeId} - patch employee ... partial update |
67 | 73 |
|
68 |
| - @DeleteMapping("/employees/{employeeId}") |
69 |
| - public String deleteEmployee(@PathVariable int employeeId) { |
| 74 | + @PatchMapping("/employees/{employeeId}") |
| 75 | + public Employee patchEmployee(@PathVariable int employeeId, |
| 76 | + @RequestBody Map<String, Object> patchPayload) { |
70 | 77 |
|
71 | 78 | Employee tempEmployee = employeeService.findById(employeeId);
|
72 | 79 |
|
73 | 80 | // throw exception if null
|
74 |
| - |
75 | 81 | if (tempEmployee == null) {
|
76 | 82 | throw new RuntimeException("Employee id not found - " + employeeId);
|
77 | 83 | }
|
78 | 84 |
|
79 |
| - employeeService.deleteById(employeeId); |
| 85 | + // throw exception if request body contains "id" key |
| 86 | + if (patchPayload.containsKey("id")) { |
| 87 | + throw new RuntimeException("Employee id not allowed in request body - " + employeeId); |
| 88 | + } |
| 89 | + |
| 90 | + Employee patchedEmployee = apply(patchPayload, tempEmployee); |
| 91 | + |
| 92 | + Employee dbEmployee = employeeService.save(patchedEmployee); |
| 93 | + |
| 94 | + return dbEmployee; |
| 95 | + } |
| 96 | + |
| 97 | + private Employee apply(Map<String, Object> patchPayload, Employee tempEmployee) { |
| 98 | + |
| 99 | + // Convert employee object to a JSON object node |
| 100 | + ObjectNode employeeNode = objectMapper.convertValue(tempEmployee, ObjectNode.class); |
| 101 | + |
| 102 | + // Convert the patchPayload map to a JSON object node |
| 103 | + ObjectNode patchNode = objectMapper.convertValue(patchPayload, ObjectNode.class); |
| 104 | + |
| 105 | + // Merge the patch updates into the employee node |
| 106 | + employeeNode.setAll(patchNode); |
80 | 107 |
|
81 |
| - return "Deleted employee id - " + employeeId; |
| 108 | + return objectMapper.convertValue(employeeNode, Employee.class); |
82 | 109 | }
|
83 | 110 |
|
84 | 111 | }
|
|
0 commit comments