Items wenn abgehakt werden auch im backend als done markiert, fixes #13

Wir haben eine editItem() Methode erstellt, welches ein JSON mitbekommen
kriegt mit den Änderungen eines bestimmten items mit itemID. Das
ShoppingItemPatchDTO hat nullable Felder und im Controller wird nur das
geändert von einem item, was nicht null ist.
This commit is contained in:
Technoir committed 2026-10-07 14:14:19 +02:00
1 parent 344ae5c584
commit 1b321420a5
4 files changed
+33 -2

No files matched your search

@@ -1,6 +1,7 @@
package de.timaldinger.einkaufsliste.Controller;
import de.timaldinger.einkaufsliste.Dto.ShoppingItemDTO;
import de.timaldinger.einkaufsliste.Dto.ShoppingItemPatchDTO;
import de.timaldinger.einkaufsliste.Dto.ShoppingItemResponseDTO;
import de.timaldinger.einkaufsliste.Models.ShoppingCategory;
import de.timaldinger.einkaufsliste.Models.ShoppingItem;
@@ -40,6 +41,20 @@ public class ShoppingItemController {
return new ShoppingItemResponseDTO(name, amount, item.getId(), c != null? c.getId() : null);
}
@PatchMapping("/api/item/{itemID}")
public ResponseEntity<Void> editItem(@PathVariable UUID itemID ,@RequestBody ShoppingItemPatchDTO toChange){
ShoppingItem item = shoppingItemRepository.findById(itemID).orElseThrow();
if(toChange.itemName() != null) item.setItemName(toChange.itemName());
if(toChange.itemAmount() != null) item.setItemAmount(toChange.itemAmount());
if(toChange.isDone() != null) item.setDone(toChange.isDone());
if(toChange.categoryID() != null) item.setCategory(shoppingCategoryRepository.findById(toChange.categoryID()).orElseThrow());
shoppingItemRepository.save(item);
return ResponseEntity.noContent().build();
}
@DeleteMapping("/api/item/{itemID}")
public ResponseEntity<Void> deleteItem(@PathVariable UUID itemID){
if(!shoppingItemRepository.existsById(itemID)){
@@ -55,6 +55,11 @@ function init() {
deleteCategory(checkbox.checked, li.id);
}
document.querySelector(".displayedList").addEventListener("change", (e) => {
if (e.target.matches(".item__markAsDone")) {
const item = e.target.closest("li");
api.editItem(item.id, { done: e.target.checked });
}
});
console.log("javascript initialized!");
@@ -29,6 +29,17 @@ export async function deleteItem(itemID) {
}
export async function editItem(itemID, toChange) {
const response = await fetch(`/api/item/${itemID}`,
{
method: "PATCH",
body: JSON.stringify(toChange),
headers: { "Content-Type": "application/json" }
});
return response;
}
export async function addCategory(listID, categoryName) {
const newCategory = await fetch(`/api/category`,
{
+2 -2
View File
@@ -45,7 +45,7 @@
<!-- Items der Kategorie -->
<ul>
<li th:each="item: ${category.itemsInCategory}" th:id="${item.id}" class="item"> <!-- erstellung von einem Item -->
<input type="checkbox" class="item__markAsDone">
<input type="checkbox" class="item__markAsDone" th:checked="${item.isDone}">
<span th:text="|${item.itemName} ${item.itemAmount}|"></span>
<button class="item__deleteButton">Löschen</button>
</li>
@@ -53,7 +53,7 @@
</li>
<li th:each="item: ${items}" th:id="${item.id}" class="item"> <!-- erstellung items ohne Kategorie -->
<input type="checkbox" class="item__markAsDone">
<input type="checkbox" class="item__markAsDone" th:checked="${item.isDone}">
<span th:text="|${item.itemName} ${item.itemAmount}|"></span>
<button class="item__deleteButton">Löschen</button>
</li>