category kann optional sein

This commit is contained in:
Technoir committed 2026-10-05 21:51:19 +02:00
1 parent 49377a98a8
commit 33964f2a30
1 file changed
+10 -5
@@ -9,11 +9,11 @@ import de.timaldinger.einkaufsliste.Repository.ShoppingListCategoryRepository;
import de.timaldinger.einkaufsliste.Repository.ShoppingListItemRepository;
import de.timaldinger.einkaufsliste.Repository.ShoppingListRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.expression.Lists;
import java.util.List;
@@ -46,7 +46,12 @@ public class ShoppingListController {
public ShoppingListItem addItem(@PathVariable UUID listID, @RequestBody ShoppingListItemDTO newItem){
String name = newItem.itemName();
String amount = newItem.itemAmount();
ShoppingListCategory c = shoppingListCategoryRepository.findById(newItem.category()).orElseThrow();
ShoppingListCategory c;
if(newItem.categoryID() != null){
c = shoppingListCategoryRepository.findById(newItem.categoryID()).orElseThrow();
}else{
c = null;
}
ShoppingList list = shoppingListRepository.findById(listID).orElseThrow();
ShoppingListItem item = new ShoppingListItem(name, amount, false, c, list);
shoppingListItemRepository.save(item);