ShoppingItemResponseDTO hinzugefügt um rekursives JSON zu vermeiden
Würde man das normale ShoppingItem zurückgeben so ergäbe sich ein riesiges JSON weil die Felder category und item auf sich selbst verweisen. Genommen wurde ein DTO, da es zur vorherigen Rangehenweise passt, alternatvi wäre auch eine Jackson annotation möglich gewesen (@JsonIgnore) bei bei itemsIncategory in ShoppingCategory.java
This commit is contained in:
1 parent
f4822ceb4d
commit
002d09955a
2 files changed
+13
-6
No files matched your search
@@ -39,8 +39,8 @@ public class ShoppingListController {
|
||||
return category;
|
||||
}
|
||||
|
||||
@PostMapping("/api/list/{listID}/item")
|
||||
public ShoppingListItem addItem(@PathVariable UUID listID, @RequestBody ShoppingListItemDTO newItem){
|
||||
@PostMapping("/api/item")
|
||||
public ShoppingItemResponseDTO addItem(@RequestBody ShoppingItemDTO newItem){
|
||||
String name = newItem.itemName();
|
||||
String amount = newItem.itemAmount();
|
||||
ShoppingCategory c;
|
||||
@@ -49,10 +49,11 @@ public class ShoppingListController {
|
||||
}else{
|
||||
c = null;
|
||||
}
|
||||
ShoppingList list = shoppingListRepository.findById(listID).orElseThrow();
|
||||
ShoppingListItem item = new ShoppingListItem(name, amount, false, c, list);
|
||||
shoppingListItemRepository.save(item);
|
||||
return item;
|
||||
ShoppingList list = shoppingListRepository.findById(newItem.listID()).orElseThrow();
|
||||
ShoppingItem item = new ShoppingItem(name, amount, false, c, list);
|
||||
shoppingItemRepository.save(item);
|
||||
|
||||
return new ShoppingItemResponseDTO(name, amount, item.getId(), c != null? c.getId() : null);
|
||||
}
|
||||
|
||||
@DeleteMapping("/api/item/{itemID}")
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package de.timaldinger.einkaufsliste.Dto;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public record ShoppingItemResponseDTO(String itemName, String itemAmount, UUID itemID, UUID categoryID) {
|
||||
}
|
||||
Reference in new issue
Block a user