@Valid Annotation für DTOs

This commit is contained in:
Technoir committed 2026-10-12 13:16:31 +02:00
1 parent 4ba8b4d823
commit b4def65ec0
3 files changed
+9 -6

No files matched your search

@@ -11,6 +11,7 @@ import de.timaldinger.einkaufsliste.Models.ShoppingList;
import de.timaldinger.einkaufsliste.Repository.ShoppingCategoryRepository;
import de.timaldinger.einkaufsliste.Repository.ShoppingItemRepository;
import de.timaldinger.einkaufsliste.Repository.ShoppingListRepository;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -32,7 +33,7 @@ public class ShoppingCategoryController {
private ShoppingItemRepository shoppingItemRepository;
@PostMapping("/api/categories")
public ResponseEntity<ShoppingCategoryResponseDTO> addCategory(@RequestBody ShoppingCategoryPostDTO newCategory){
public ResponseEntity<ShoppingCategoryResponseDTO> addCategory(@Valid @RequestBody ShoppingCategoryPostDTO newCategory){
String name = newCategory.categoryName();
ShoppingList list = shoppingListRepository.findById(newCategory.listID()).orElseThrow(() -> new ResourceNotFoundException("listID",newCategory.listID()));
@@ -51,7 +52,7 @@ public class ShoppingCategoryController {
}
@PatchMapping("/api/categories/{categoryID}")
public ResponseEntity<Void> editCategory(@PathVariable UUID categoryID,@RequestBody ShoppingCategoryPatchDTO toChange){
public ResponseEntity<Void> editCategory(@PathVariable UUID categoryID,@Valid @RequestBody ShoppingCategoryPatchDTO toChange){
ShoppingCategory category = shoppingCategoryRepository.findById(categoryID).orElseThrow(() -> new ResourceNotFoundException("categoryID",categoryID));
if(toChange.categoryName() != null) category.setCategoryName(toChange.categoryName());
@@ -12,6 +12,7 @@ import de.timaldinger.einkaufsliste.Repository.ShoppingCategoryRepository;
import de.timaldinger.einkaufsliste.Repository.ShoppingItemRepository;
import de.timaldinger.einkaufsliste.Repository.ShoppingListRepository;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -33,7 +34,7 @@ public class ShoppingItemController {
private ShoppingItemRepository shoppingItemRepository;
@PostMapping("/api/items")
public ResponseEntity<ShoppingItemResponseDTO> addItem(@RequestBody ShoppingItemPostDTO newItem){
public ResponseEntity<ShoppingItemResponseDTO> addItem(@Valid @RequestBody ShoppingItemPostDTO newItem){
String name = newItem.itemName();
String amount = newItem.itemAmount();
ShoppingCategory c;
@@ -57,7 +58,7 @@ public class ShoppingItemController {
}
@PatchMapping("/api/items/{itemID}")
public ResponseEntity<Void> editItem(@PathVariable UUID itemID ,@RequestBody ShoppingItemPatchDTO toChange){
public ResponseEntity<Void> editItem(@PathVariable UUID itemID , @Valid @RequestBody ShoppingItemPatchDTO toChange){
ShoppingItem item = shoppingItemRepository.findById(itemID).orElseThrow(() -> new ResourceNotFoundException("itemID",itemID));
if(toChange.itemName() != null) item.setItemName(toChange.itemName());
@@ -10,6 +10,7 @@ import de.timaldinger.einkaufsliste.Repository.ShoppingCategoryRepository;
import de.timaldinger.einkaufsliste.Repository.ShoppingItemRepository;
import de.timaldinger.einkaufsliste.Repository.ShoppingListRepository;
import de.timaldinger.einkaufsliste.Service.ShoppingListService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -34,7 +35,7 @@ public class ShoppingListController {
private ShoppingListService shoppingListService;
@PostMapping("/api/lists")
public ResponseEntity<ShoppingListResponseDTO> addList(@RequestBody ShoppingListPostDTO newList){
public ResponseEntity<ShoppingListResponseDTO> addList(@Valid @RequestBody ShoppingListPostDTO newList){
ShoppingList list = new ShoppingList(newList.listName(), null, null, 0, 0);
shoppingListRepository.save(list);
@@ -57,7 +58,7 @@ public class ShoppingListController {
}
@PatchMapping("/api/lists/{listID}")
public ResponseEntity<Void> editList(@PathVariable UUID listID,@RequestBody ShoppingListPatchDTO toChange){
public ResponseEntity<Void> editList(@PathVariable UUID listID,@Valid @RequestBody ShoppingListPatchDTO toChange){
ShoppingList list = shoppingListRepository.findById(listID).orElseThrow(() -> new ResourceNotFoundException("listID", listID));
if(toChange.newListName() != null) list.setListName(toChange.newListName());