diff --git a/src/main/java/de/timaldinger/einkaufsliste/Controller/ShoppingListController.java b/src/main/java/de/timaldinger/einkaufsliste/Controller/ShoppingListController.java index 98b9774..dbf3913 100644 --- a/src/main/java/de/timaldinger/einkaufsliste/Controller/ShoppingListController.java +++ b/src/main/java/de/timaldinger/einkaufsliste/Controller/ShoppingListController.java @@ -1,10 +1,5 @@ package de.timaldinger.einkaufsliste.Controller; -import de.timaldinger.einkaufsliste.Dto.ShoppingCategoryDTO; -import de.timaldinger.einkaufsliste.Dto.ShoppingItemResponseDTO; -import de.timaldinger.einkaufsliste.Dto.ShoppingItemDTO; -import de.timaldinger.einkaufsliste.Models.ShoppingList; -import de.timaldinger.einkaufsliste.Models.ShoppingCategory; import de.timaldinger.einkaufsliste.Models.ShoppingItem; import de.timaldinger.einkaufsliste.Repository.ShoppingCategoryRepository; import de.timaldinger.einkaufsliste.Repository.ShoppingItemRepository; diff --git a/src/main/resources/static/js/list/listAPI.js b/src/main/resources/static/js/list/listAPI.js deleted file mode 100644 index 843c1aa..0000000 --- a/src/main/resources/static/js/list/listAPI.js +++ /dev/null @@ -1,83 +0,0 @@ - -export async function addItem(listID, itemName, itemAmount, categoryID) { - const newItem = await fetch(`/api/item`, - { - method: "POST", - body: JSON.stringify({ - "itemName": itemName, - "itemAmount": itemAmount, - "categoryID": categoryID, - "listID": listID - }), - headers: { "Content-Type": "application/json" } - }); - - - return await newItem.json(); -} - - -export async function deleteItem(itemID) { - - const response = await fetch(`/api/item/${itemID}`, { method: "DELETE" }); - - if (response.status == 204) { - console.log(`deleted item ${itemID}`); - } - - return response; - -} - -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`, - { - method: "POST" - , body: JSON.stringify({ "categoryName": categoryName, "listID": listID }) - , headers: { "Content-Type": "application/json" } - }); - return await newCategory.json(); -} - - -export async function editCategory(categoryID, toChange) { - const response = await fetch(`/api/category/${categoryID}`, - { - method: "PATCH", - body: JSON.stringify(toChange), - headers: { "Content-Type": "application/json" } - }); - - return response; -} - -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; -} - -export async function deleteAllDoneItems(listID) { - - const response = await fetch(`/api/list/${listID}?deleteAllDoneItems=true`, { method: "DELETE" }); - - if (response.status == 204) { - console.log(`items have been deleted`); - } - return response; - -} diff --git a/src/main/resources/static/js/list/ShoppingCategory.js b/src/main/resources/static/js/models/ShoppingCategory.js similarity index 100% rename from src/main/resources/static/js/list/ShoppingCategory.js rename to src/main/resources/static/js/models/ShoppingCategory.js diff --git a/src/main/resources/static/js/list/ShoppingItem.js b/src/main/resources/static/js/models/ShoppingItem.js similarity index 100% rename from src/main/resources/static/js/list/ShoppingItem.js rename to src/main/resources/static/js/models/ShoppingItem.js diff --git a/src/main/resources/static/js/shared/ShoppingList.js b/src/main/resources/static/js/models/ShoppingList.js similarity index 100% rename from src/main/resources/static/js/shared/ShoppingList.js rename to src/main/resources/static/js/models/ShoppingList.js diff --git a/src/main/resources/static/js/index.js b/src/main/resources/static/js/pages/index/index.js similarity index 100% rename from src/main/resources/static/js/index.js rename to src/main/resources/static/js/pages/index/index.js diff --git a/src/main/resources/static/js/pages/list/api/categoryAPI.js b/src/main/resources/static/js/pages/list/api/categoryAPI.js new file mode 100644 index 0000000..bd328e8 --- /dev/null +++ b/src/main/resources/static/js/pages/list/api/categoryAPI.js @@ -0,0 +1,30 @@ +export async function addCategory(listID, categoryName) { + const newCategory = await fetch(`/api/category`, + { + method: "POST" + , body: JSON.stringify({ "categoryName": categoryName, "listID": listID }) + , headers: { "Content-Type": "application/json" } + }); + return await newCategory.json(); +} + + +export async function editCategory(categoryID, toChange) { + const response = await fetch(`/api/category/${categoryID}`, + { + method: "PATCH", + body: JSON.stringify(toChange), + headers: { "Content-Type": "application/json" } + }); + + return response; +} + +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; +} diff --git a/src/main/resources/static/js/pages/list/api/itemAPI.js b/src/main/resources/static/js/pages/list/api/itemAPI.js new file mode 100644 index 0000000..d18328a --- /dev/null +++ b/src/main/resources/static/js/pages/list/api/itemAPI.js @@ -0,0 +1,44 @@ + + +export async function addItem(listID, itemName, itemAmount, categoryID) { + const newItem = await fetch(`/api/item`, + { + method: "POST", + body: JSON.stringify({ + "itemName": itemName, + "itemAmount": itemAmount, + "categoryID": categoryID, + "listID": listID + }), + headers: { "Content-Type": "application/json" } + }); + + + return await newItem.json(); +} + + +export async function deleteItem(itemID) { + + const response = await fetch(`/api/item/${itemID}`, { method: "DELETE" }); + + if (response.status == 204) { + console.log(`deleted item ${itemID}`); + } + + return response; + +} + +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; +} + + diff --git a/src/main/resources/static/js/pages/list/api/listAPI.js b/src/main/resources/static/js/pages/list/api/listAPI.js new file mode 100644 index 0000000..f072cf4 --- /dev/null +++ b/src/main/resources/static/js/pages/list/api/listAPI.js @@ -0,0 +1,14 @@ +export * from "./itemAPI.js"; +export * from "./categoryAPI.js" + +export async function deleteAllDoneItems(listID) { + + const response = await fetch(`/api/list/${listID}?deleteAllDoneItems=true`, { method: "DELETE" }); + + if (response.status == 204) { + console.log(`items have been deleted`); + } + return response; + +} + diff --git a/src/main/resources/static/js/list/listFrontend.js b/src/main/resources/static/js/pages/list/frontend/categoryUI.js similarity index 53% rename from src/main/resources/static/js/list/listFrontend.js rename to src/main/resources/static/js/pages/list/frontend/categoryUI.js index c1ea796..93dd99d 100644 --- a/src/main/resources/static/js/list/listFrontend.js +++ b/src/main/resources/static/js/pages/list/frontend/categoryUI.js @@ -1,50 +1,5 @@ -import { ShoppingCategory } from "./ShoppingCategory.js"; -import { ShoppingItem } from "./ShoppingItem.js"; -export function addItem(itemAsJSON) { - let list; - if (!itemAsJSON.categoryID) { - list = document.querySelector(".displayedList"); - } else { - list = document.getElementById(`${itemAsJSON.categoryID}`).querySelector("ul"); - } - - const item = ShoppingItem.fromJSON(itemAsJSON); - - list.appendChild(item.render()); -} - -export function createEditItemForm(textElement) { - const editForm = document.createElement("form"); - const inputName = document.createElement("input"); - const inputAmount = document.createElement("input"); - const abortButton = document.createElement("button"); - const saveButton = document.createElement("button"); - - inputName.setAttribute("name", "newItemName"); - inputName.setAttribute("value", textElement.querySelector(".item__textName").textContent); - inputName.classList.add("editForm__name"); - - inputAmount.setAttribute("name", "newItemAmount"); - inputAmount.setAttribute("value", textElement.querySelector(".item__textAmount").textContent); - inputAmount.classList.add("editForm__amount"); - - - abortButton.textContent = "Abbrechen"; - saveButton.textContent = "Speichern"; - - - editForm.appendChild(inputName); - editForm.appendChild(inputAmount); - editForm.appendChild(saveButton); - editForm.appendChild(abortButton); - - return editForm; -} - -export function deleteItem(li) { - li.remove(); -} +import { ShoppingCategory } from "../../../models/ShoppingCategory.js"; export function addCategory(categoryAsJSON) { const list = document.querySelector(".displayedList"); @@ -101,10 +56,3 @@ export function editCategory(id, formElement, formData, categoryDiv) { document.querySelector(`option[value='${id}']`).textContent = newName; } - -export function deleteAllDoneItems() { - const doneItems = document.querySelectorAll("li.item:has(input[type=checkbox]:checked)"); - doneItems.forEach((li) => { - li.remove(); - }) -} diff --git a/src/main/resources/static/js/pages/list/frontend/itemUI.js b/src/main/resources/static/js/pages/list/frontend/itemUI.js new file mode 100644 index 0000000..4b2935d --- /dev/null +++ b/src/main/resources/static/js/pages/list/frontend/itemUI.js @@ -0,0 +1,48 @@ +import { ShoppingItem } from "../../../models/ShoppingItem.js"; + +export function addItem(itemAsJSON) { + let list; + if (!itemAsJSON.categoryID) { + list = document.querySelector(".displayedList"); + } else { + list = document.getElementById(`${itemAsJSON.categoryID}`).querySelector("ul"); + } + + const item = ShoppingItem.fromJSON(itemAsJSON); + + list.appendChild(item.render()); +} + +export function createEditItemForm(textElement) { + const editForm = document.createElement("form"); + const inputName = document.createElement("input"); + const inputAmount = document.createElement("input"); + const abortButton = document.createElement("button"); + const saveButton = document.createElement("button"); + + inputName.setAttribute("name", "newItemName"); + inputName.setAttribute("value", textElement.querySelector(".item__textName").textContent); + inputName.classList.add("editForm__name"); + + inputAmount.setAttribute("name", "newItemAmount"); + inputAmount.setAttribute("value", textElement.querySelector(".item__textAmount").textContent); + inputAmount.classList.add("editForm__amount"); + + + abortButton.textContent = "Abbrechen"; + saveButton.textContent = "Speichern"; + + + editForm.appendChild(inputName); + editForm.appendChild(inputAmount); + editForm.appendChild(saveButton); + editForm.appendChild(abortButton); + + return editForm; +} + +export function deleteItem(li) { + li.remove(); +} + + diff --git a/src/main/resources/static/js/pages/list/frontend/listUI.js b/src/main/resources/static/js/pages/list/frontend/listUI.js new file mode 100644 index 0000000..d9d4b1d --- /dev/null +++ b/src/main/resources/static/js/pages/list/frontend/listUI.js @@ -0,0 +1,9 @@ +export * from "./itemUI.js"; +export * from "./categoryUI.js"; + +export function deleteAllDoneItems() { + const doneItems = document.querySelectorAll("li.item:has(input[type=checkbox]:checked)"); + doneItems.forEach((li) => { + li.remove(); + }) +} diff --git a/src/main/resources/static/js/list/list.js b/src/main/resources/static/js/pages/list/list.js similarity index 98% rename from src/main/resources/static/js/list/list.js rename to src/main/resources/static/js/pages/list/list.js index 87f83d2..d93a079 100644 --- a/src/main/resources/static/js/list/list.js +++ b/src/main/resources/static/js/pages/list/list.js @@ -1,5 +1,5 @@ -import * as api from "./listAPI.js" -import * as ui from "./listFrontend.js" +import * as api from "./api/listAPI.js" +import * as ui from "./frontend/listUI.js" const addCategoryForm = document.querySelector(".addCategoryForm"); diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index a94e9f0..20582af 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -8,7 +8,7 @@ xmlns:th="http://www.thymeleaf.org"> - + diff --git a/src/main/resources/templates/list.html b/src/main/resources/templates/list.html index 8e887d9..b2b987c 100644 --- a/src/main/resources/templates/list.html +++ b/src/main/resources/templates/list.html @@ -6,7 +6,7 @@ Einkaufsliste - +