Compare commits
7
Commits
ca09f8d404
...
bce0b982ee
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bce0b982ee | ||
|
|
9b8887793e | ||
|
|
bfca2878b9 | ||
|
|
4c67f191a2 | ||
|
|
4f62c7f93e | ||
|
|
7698efd446 | ||
|
|
a99342cc98 |
No files matched your search
+28
-7
@@ -2,15 +2,16 @@ package de.timaldinger.einkaufsliste.Controller;
|
||||
|
||||
import de.timaldinger.einkaufsliste.Dto.ShoppingCategoryDTO;
|
||||
import de.timaldinger.einkaufsliste.Models.ShoppingCategory;
|
||||
import de.timaldinger.einkaufsliste.Models.ShoppingItem;
|
||||
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 org.springframework.beans.factory.annotation.Autowired;
|
||||
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.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
@@ -24,11 +25,11 @@ public class ShoppingCategoryController {
|
||||
@Autowired
|
||||
private ShoppingItemRepository shoppingItemRepository;
|
||||
|
||||
@PostMapping("/api/list/{listID}/category/")
|
||||
public ShoppingCategory addCategory(@PathVariable UUID listID, @RequestBody ShoppingCategoryDTO newCategory){
|
||||
@PostMapping("/api/category")
|
||||
public ShoppingCategory addCategory(@RequestBody ShoppingCategoryDTO newCategory){
|
||||
|
||||
String name = newCategory.categoryName();
|
||||
ShoppingList list = shoppingListRepository.findById(listID).orElseThrow();
|
||||
ShoppingList list = shoppingListRepository.findById(newCategory.listID()).orElseThrow();
|
||||
|
||||
ShoppingCategory category = new ShoppingCategory(name, List.of(),list);
|
||||
|
||||
@@ -37,5 +38,25 @@ public class ShoppingCategoryController {
|
||||
return category;
|
||||
}
|
||||
|
||||
@DeleteMapping("/api/category/{categoryID}")
|
||||
public ResponseEntity<Void> deleteCategory(@PathVariable UUID categoryID, @RequestParam(required = false) Boolean deleteAllItems){
|
||||
|
||||
if(deleteAllItems == null){
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Missing required parameter deleteAllItems (can be either true or false)");
|
||||
}
|
||||
|
||||
List<ShoppingItem> itemList = shoppingItemRepository.findAllByCategory_Id(categoryID);
|
||||
if(deleteAllItems){
|
||||
itemList.forEach((item) -> shoppingItemRepository.deleteById(item.getId()));
|
||||
}else{
|
||||
itemList.forEach((item) -> item.setCategory(null));
|
||||
shoppingItemRepository.saveAll(itemList);
|
||||
}
|
||||
|
||||
shoppingCategoryRepository.deleteById(categoryID);
|
||||
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
package de.timaldinger.einkaufsliste.Dto;
|
||||
|
||||
public record ShoppingCategoryDTO(String categoryName) {
|
||||
import java.util.UUID;
|
||||
|
||||
public record ShoppingCategoryDTO(String categoryName, UUID listID) {
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.timaldinger.einkaufsliste.Repository;
|
||||
|
||||
import de.timaldinger.einkaufsliste.Models.ShoppingItem;
|
||||
import de.timaldinger.einkaufsliste.Models.ShoppingList;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
@@ -8,5 +9,6 @@ import java.util.UUID;
|
||||
|
||||
public interface ShoppingItemRepository extends JpaRepository<ShoppingItem, UUID> {
|
||||
|
||||
List<ShoppingItem> findByListAndCategoryIsNull(Object list);
|
||||
List<ShoppingItem> findByListAndCategoryIsNull(ShoppingList list);
|
||||
List<ShoppingItem> findAllByCategory_Id(UUID categoryID);
|
||||
}
|
||||
@@ -15,6 +15,7 @@ export class ShoppingCategory {
|
||||
const span = document.createElement("span");
|
||||
const deleteCategoryButton = document.createElement("button");
|
||||
const deleteAllItemsCheckbox = document.createElement("input");
|
||||
const innerUl = document.createElement("ul");
|
||||
li.id = this.categoryID;
|
||||
li.classList.add("category");
|
||||
|
||||
@@ -29,6 +30,7 @@ export class ShoppingCategory {
|
||||
li.appendChild(span);
|
||||
li.appendChild(deleteCategoryButton);
|
||||
li.appendChild(deleteAllItemsCheckbox);
|
||||
li.appendChild(innerUl);
|
||||
|
||||
return li;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,12 @@ function deleteItem(li) {
|
||||
ui.deleteItem(li);
|
||||
}
|
||||
|
||||
function deleteCategory(deleteAllItems, categoryID) {
|
||||
console.log(`deleting category with allItems: ${deleteAllItems} id: ${categoryID}`);
|
||||
api.deleteCategory(deleteAllItems, categoryID);
|
||||
ui.deleteCategory(deleteAllItems, categoryID);
|
||||
}
|
||||
|
||||
function init() {
|
||||
addCategoryForm.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
|
||||
@@ -19,26 +19,32 @@ export async function addItem(listID, itemName, itemAmount, categoryID) {
|
||||
|
||||
export async function deleteItem(itemID) {
|
||||
|
||||
const response = await fetch(`/api/item/${itemID}`, { method: "DELETE" })
|
||||
.then(r => console.log("item deleted"));
|
||||
const response = await fetch(`/api/item/${itemID}`, { method: "DELETE" });
|
||||
|
||||
if (response.status == 204) {
|
||||
console.log(`deleted item ${itemID}`);
|
||||
}
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
|
||||
export async function addCategory(listID, categoryName) {
|
||||
const newCategory = await fetch(`/api/list/${listID}/category/`,
|
||||
const newCategory = await fetch(`/api/category`,
|
||||
{
|
||||
method: "POST"
|
||||
, body: JSON.stringify({ "categoryName": categoryName })
|
||||
, body: JSON.stringify({ "categoryName": categoryName, "listID": listID })
|
||||
, headers: { "Content-Type": "application/json" }
|
||||
});
|
||||
//listElements.push(new ShoppingCategory(categoryName, renderAddItemPrompt, renderItemList));
|
||||
//renderItemList();
|
||||
return await newCategory.json();
|
||||
}
|
||||
|
||||
|
||||
export function deleteCategory() {
|
||||
export async function deleteCategory(deleteAllItems, categoryID) {
|
||||
const response = await fetch(`/api/category/${categoryID}?deleteAllItems=${deleteAllItems}`, { method: "DELETE" });
|
||||
|
||||
if (response.status == 204) {
|
||||
console.log(`deleted item ${categoryID}`);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
@@ -25,6 +25,13 @@ export function addCategory(categoryAsJSON) {
|
||||
list.appendChild(category.render());
|
||||
}
|
||||
|
||||
export function deleteCategory() {
|
||||
export function deleteCategory(deleteAllItems, categoryID) {
|
||||
const category = document.getElementById(categoryID);
|
||||
if (!deleteAllItems) {
|
||||
const ul = category.querySelector("ul");
|
||||
if (ul) document.querySelector(".displayedList").append(...ul.children);
|
||||
}
|
||||
category.remove()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -44,7 +44,7 @@
|
||||
<input type="checkbox">
|
||||
<!-- Items der Kategorie -->
|
||||
<ul>
|
||||
<li th:each="item: ${category.itemsInCategory}" th:id="${item.id}"> <!-- erstellung von einem Item -->
|
||||
<li th:each="item: ${category.itemsInCategory}" th:id="${item.id}" class="item"> <!-- erstellung von einem Item -->
|
||||
<input type="checkbox" class="item__markAsDone">
|
||||
<span th:text="|${item.itemName} ${item.itemAmount}|"></span>
|
||||
<button class="item__deleteButton">Löschen</button>
|
||||
|
||||
Reference in new issue
Block a user