Skip to content

Commit 8c16678

Browse files
Amends (and finally simplifies) the solution to PATCH HTPP requests.
Not using Generics anymore. Hahahaha.
1 parent 991c2eb commit 8c16678

File tree

5 files changed

+12
-54
lines changed

5 files changed

+12
-54
lines changed

src/main/java/dev/drugowick/algaworks/algafoodapi/api/controller/CityController.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ public ResponseEntity<?> partialUpdate(@PathVariable Long id, @RequestBody Map<S
9696
return ResponseEntity.notFound().build();
9797
}
9898

99-
ObjectMerger
100-
.of(City.class)
101-
.mergeRequestBodyToGenericObject(cityMap, cityToUpdate);
99+
ObjectMerger.mergeRequestBodyToGenericObject(cityMap, cityToUpdate, City.class);
102100

103101
return update(id, cityToUpdate);
104102
}

src/main/java/dev/drugowick/algaworks/algafoodapi/api/controller/CuisineController.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ public ResponseEntity<?> partialUpdate(@PathVariable Long id, @RequestBody Map<S
8080
return ResponseEntity.notFound().build();
8181
}
8282

83-
ObjectMerger
84-
.of(Cuisine.class)
85-
.mergeRequestBodyToGenericObject(cuisineMap, cuisineToUpdate);
83+
ObjectMerger.mergeRequestBodyToGenericObject(cuisineMap, cuisineToUpdate, Cuisine.class);
8684

8785
return update(id, cuisineToUpdate);
8886
}

src/main/java/dev/drugowick/algaworks/algafoodapi/api/controller/ProvinceController.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ public ResponseEntity<?> partialUpdate(@PathVariable Long id, @RequestBody Map<S
8686
return ResponseEntity.notFound().build();
8787
}
8888

89-
ObjectMerger
90-
.of(Province.class)
91-
.mergeRequestBodyToGenericObject(provinceMap, provinceToUpdate);
89+
ObjectMerger.mergeRequestBodyToGenericObject(provinceMap, provinceToUpdate, Province.class);
9290

9391
return update(id, provinceToUpdate);
9492
}

src/main/java/dev/drugowick/algaworks/algafoodapi/api/controller/RestaurantController.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ public ResponseEntity<?> partialUpdate(@PathVariable Long id, @RequestBody Map<S
8383
return ResponseEntity.notFound().build();
8484
}
8585

86-
ObjectMerger
87-
.of(Restaurant.class)
88-
.mergeRequestBodyToGenericObject(restaurantMap, restaurantToUpdate);
86+
ObjectMerger.mergeRequestBodyToGenericObject(restaurantMap, restaurantToUpdate, Restaurant.class);
8987

9088
return update(id, restaurantToUpdate);
9189
}

src/main/java/dev/drugowick/algaworks/algafoodapi/api/controller/utils/ObjectMerger.java

+8-42
Original file line numberDiff line numberDiff line change
@@ -8,63 +8,30 @@
88
import java.util.Map;
99

1010
/**
11-
* Helper method to merge the request body (a Map<String, Object>) to an Object to be updated via a Patch HTTP request.
11+
* Helper class merge the request body (a Map<String, Object>) to an Object to be updated via a Patch HTTP request.
1212
* <p>
1313
* Uses ReflectionUtils from Spring Framework to set values to the Object.
14-
*
15-
* @param <T>
1614
*/
17-
public class ObjectMerger<T> {
15+
public class ObjectMerger {
1816

1917
/**
2018
* Determines if a Map cache will be used for ObjectMergers.
2119
*/
2220
private static boolean cacheEnabled = true;
2321
private static Map<Object, ObjectMerger> objectMergerCache = new HashMap<>();
2422

25-
private static ObjectMapper objectMapper;
26-
private Class<T> type;
27-
28-
29-
private ObjectMerger(Class<T> type) {
30-
objectMapper = new ObjectMapper();
31-
this.type = type;
32-
}
33-
34-
/**
35-
* Returns a new instance of ObjectMerger<T> of the given type as parameter OR
36-
* an existing instance created before.
37-
*
38-
* @param type a class to be merged with a Map of <String, Object>.
39-
* @return
40-
*/
41-
public static ObjectMerger of(Class type) {
42-
43-
if (!cacheEnabled) {
44-
// Cache is not enabled. A new instance is always created.
45-
return new ObjectMerger(type);
46-
}
47-
48-
if (!objectMergerCache.containsKey(type)) {
49-
ObjectMerger objectMerger = new ObjectMerger(type);
50-
objectMergerCache.put(type, objectMerger);
51-
// Cache enabled. Instance created (first request).
52-
return objectMerger;
53-
}
54-
55-
// Cache enabled. Returning existing instance.
56-
return objectMergerCache.get(type);
57-
}
58-
5923
/**
60-
* Updates an objectToUpdate according to data from a Map<String, Object>.
24+
* Updates an objectToUpdate of type type according to data from a Map<String, Object>.
6125
*
6226
* @param objectMap a Map of <String, Object> with data to update destination object. The string values
6327
* must match fields on the destination object.
6428
* @param objectToUpdate the object to update.
29+
*
30+
* @param type the type of the objectToUpdate.
6531
*/
66-
public void mergeRequestBodyToGenericObject(Map<String, Object> objectMap, T objectToUpdate) {
67-
T newObject = objectMapper.convertValue(objectMap, type);
32+
public static void mergeRequestBodyToGenericObject(Map<String, Object> objectMap, Object objectToUpdate, Class type) {
33+
ObjectMapper objectMapper = new ObjectMapper();
34+
Object newObject = objectMapper.convertValue(objectMap, type);
6835

6936
objectMap.forEach((fieldProp, valueProp) -> {
7037
Field field = ReflectionUtils.findField(type, fieldProp);
@@ -75,5 +42,4 @@ public void mergeRequestBodyToGenericObject(Map<String, Object> objectMap, T obj
7542
ReflectionUtils.setField(field, objectToUpdate, newValue);
7643
});
7744
}
78-
7945
}

0 commit comments

Comments
 (0)