addItem gibt 201 mit location und json zurück

This commit is contained in:
Technoir committed 2026-10-11 13:45:27 +02:00
1 parent 71fd6ee420
commit 75721a7465
1 file changed
+8 -2
@@ -33,7 +33,7 @@ public class ShoppingItemController {
private ShoppingItemRepository shoppingItemRepository;
@PostMapping("/api/items")
public ShoppingItemResponseDTO addItem(@RequestBody ShoppingItemPostDTO newItem){
public ResponseEntity<ShoppingItemResponseDTO> addItem(@RequestBody ShoppingItemPostDTO newItem){
String name = newItem.itemName();
String amount = newItem.itemAmount();
ShoppingCategory c;
@@ -47,7 +47,13 @@ public class ShoppingItemController {
ShoppingItem item = new ShoppingItem(name, amount, false, c, list);
shoppingItemRepository.save(item);
return new ShoppingItemResponseDTO(name, amount, item.getId(), c != null? c.getId() : null);
URI location = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(item.getId())
.toUri();
return ResponseEntity.created(location).body(new ShoppingItemResponseDTO(name,amount,item.getId(),c != null ? c.getId() : null));
}
@PatchMapping("/api/items/{itemID}")