Skip to content

Commit e6ef21e

Browse files
committed
Update for HTTP PATCH method
1 parent 5c0c8da commit e6ef21e

File tree

73 files changed

+2026
-25
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2026
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.luv2code.springboot.cruddemo.dao;
2+
3+
import com.luv2code.springboot.cruddemo.entity.Employee;
4+
5+
import java.util.List;
6+
7+
public interface EmployeeDAO {
8+
9+
List<Employee> findAll();
10+
11+
Employee findById(int theId);
12+
13+
Employee save(Employee theEmployee);
14+
15+
}
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.luv2code.springboot.cruddemo.dao;
2+
3+
import com.luv2code.springboot.cruddemo.entity.Employee;
4+
import jakarta.persistence.EntityManager;
5+
import jakarta.persistence.TypedQuery;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Repository;
8+
9+
import java.util.List;
10+
11+
@Repository
12+
public class EmployeeDAOJpaImpl implements EmployeeDAO {
13+
14+
// define field for entitymanager
15+
private EntityManager entityManager;
16+
17+
18+
// set up constructor injection
19+
@Autowired
20+
public EmployeeDAOJpaImpl(EntityManager theEntityManager) {
21+
entityManager = theEntityManager;
22+
}
23+
24+
25+
@Override
26+
public List<Employee> findAll() {
27+
28+
// create a query
29+
TypedQuery<Employee> theQuery = entityManager.createQuery("from Employee", Employee.class);
30+
31+
// execute query and get result list
32+
List<Employee> employees = theQuery.getResultList();
33+
34+
// return the results
35+
return employees;
36+
}
37+
38+
@Override
39+
public Employee findById(int theId) {
40+
41+
// get employee
42+
Employee theEmployee = entityManager.find(Employee.class, theId);
43+
44+
// return employee
45+
return theEmployee;
46+
}
47+
48+
@Override
49+
public Employee save(Employee theEmployee) {
50+
51+
// save employee
52+
Employee dbEmployee = entityManager.merge(theEmployee);
53+
54+
// return the dbEmployee
55+
return dbEmployee;
56+
}
57+
58+
}
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,6 @@ public Employee updateEmployee(@RequestBody Employee theEmployee) {
6363
return dbEmployee;
6464
}
6565

66-
// add mapping for DELETE /employees/{employeeId} - delete employee
67-
68-
@DeleteMapping("/employees/{employeeId}")
69-
public String deleteEmployee(@PathVariable int employeeId) {
70-
71-
Employee tempEmployee = employeeService.findById(employeeId);
72-
73-
// throw exception if null
74-
75-
if (tempEmployee == null) {
76-
throw new RuntimeException("Employee id not found - " + employeeId);
77-
}
78-
79-
employeeService.deleteById(employeeId);
80-
81-
return "Deleted employee id - " + employeeId;
82-
}
83-
8466
}
8567

8668

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.luv2code.springboot.cruddemo.service;
2+
3+
import com.luv2code.springboot.cruddemo.entity.Employee;
4+
5+
import java.util.List;
6+
7+
public interface EmployeeService {
8+
9+
List<Employee> findAll();
10+
11+
Employee findById(int theId);
12+
13+
Employee save(Employee theEmployee);
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.luv2code.springboot.cruddemo.service;
2+
3+
import com.luv2code.springboot.cruddemo.dao.EmployeeDAO;
4+
import com.luv2code.springboot.cruddemo.entity.Employee;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
import org.springframework.transaction.annotation.Transactional;
8+
9+
import java.util.List;
10+
11+
@Service
12+
public class EmployeeServiceImpl implements EmployeeService {
13+
14+
private EmployeeDAO employeeDAO;
15+
16+
@Autowired
17+
public EmployeeServiceImpl(EmployeeDAO theEmployeeDAO) {
18+
employeeDAO = theEmployeeDAO;
19+
}
20+
21+
@Override
22+
public List<Employee> findAll() {
23+
return employeeDAO.findAll();
24+
}
25+
26+
@Override
27+
public Employee findById(int theId) {
28+
return employeeDAO.findById(theId);
29+
}
30+
31+
@Transactional
32+
@Override
33+
public Employee save(Employee theEmployee) {
34+
return employeeDAO.save(theEmployee);
35+
}
36+
37+
}
38+
39+
40+
41+
42+
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.luv2code.springboot.cruddemo.dao;
2+
3+
import com.luv2code.springboot.cruddemo.entity.Employee;
4+
5+
import java.util.List;
6+
7+
public interface EmployeeDAO {
8+
9+
List<Employee> findAll();
10+
11+
Employee findById(int theId);
12+
13+
Employee save(Employee theEmployee);
14+
15+
}
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.luv2code.springboot.cruddemo.dao;
2+
3+
import com.luv2code.springboot.cruddemo.entity.Employee;
4+
import jakarta.persistence.EntityManager;
5+
import jakarta.persistence.TypedQuery;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Repository;
8+
9+
import java.util.List;
10+
11+
@Repository
12+
public class EmployeeDAOJpaImpl implements EmployeeDAO {
13+
14+
// define field for entitymanager
15+
private EntityManager entityManager;
16+
17+
18+
// set up constructor injection
19+
@Autowired
20+
public EmployeeDAOJpaImpl(EntityManager theEntityManager) {
21+
entityManager = theEntityManager;
22+
}
23+
24+
25+
@Override
26+
public List<Employee> findAll() {
27+
28+
// create a query
29+
TypedQuery<Employee> theQuery = entityManager.createQuery("from Employee", Employee.class);
30+
31+
// execute query and get result list
32+
List<Employee> employees = theQuery.getResultList();
33+
34+
// return the results
35+
return employees;
36+
}
37+
38+
@Override
39+
public Employee findById(int theId) {
40+
41+
// get employee
42+
Employee theEmployee = entityManager.find(Employee.class, theId);
43+
44+
// return employee
45+
return theEmployee;
46+
}
47+
48+
@Override
49+
public Employee save(Employee theEmployee) {
50+
51+
// save employee
52+
Employee dbEmployee = entityManager.merge(theEmployee);
53+
54+
// return the dbEmployee
55+
return dbEmployee;
56+
}
57+
58+
}
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
package com.luv2code.springboot.cruddemo.rest;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.node.ObjectNode;
35
import com.luv2code.springboot.cruddemo.entity.Employee;
46
import com.luv2code.springboot.cruddemo.service.EmployeeService;
57
import org.springframework.beans.factory.annotation.Autowired;
68
import org.springframework.web.bind.annotation.*;
79

810
import java.util.List;
11+
import java.util.Map;
912

1013
@RestController
1114
@RequestMapping("/api")
1215
public class EmployeeRestController {
1316

1417
private EmployeeService employeeService;
1518

19+
private ObjectMapper objectMapper;
20+
1621
@Autowired
17-
public EmployeeRestController(EmployeeService theEmployeeService) {
22+
public EmployeeRestController(EmployeeService theEmployeeService, ObjectMapper theObjectMapper) {
1823
employeeService = theEmployeeService;
24+
objectMapper = theObjectMapper;
1925
}
2026

2127
// expose "/employees" and return a list of employees
@@ -63,22 +69,43 @@ public Employee updateEmployee(@RequestBody Employee theEmployee) {
6369
return dbEmployee;
6470
}
6571

66-
// add mapping for DELETE /employees/{employeeId} - delete employee
72+
// add mapping for PATCH /employees/{employeeId} - patch employee ... partial update
6773

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) {
7077

7178
Employee tempEmployee = employeeService.findById(employeeId);
7279

7380
// throw exception if null
74-
7581
if (tempEmployee == null) {
7682
throw new RuntimeException("Employee id not found - " + employeeId);
7783
}
7884

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);
80107

81-
return "Deleted employee id - " + employeeId;
108+
return objectMapper.convertValue(employeeNode, Employee.class);
82109
}
83110

84111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.luv2code.springboot.cruddemo.service;
2+
3+
import com.luv2code.springboot.cruddemo.entity.Employee;
4+
5+
import java.util.List;
6+
7+
public interface EmployeeService {
8+
9+
List<Employee> findAll();
10+
11+
Employee findById(int theId);
12+
13+
Employee save(Employee theEmployee);
14+
15+
}

0 commit comments

Comments
 (0)