Compare commits

...
8 Commits
13 changed files with 129 additions and 115 deletions

No files matched your search

@@ -1,10 +1,12 @@
package de.timaldinger.einkaufsliste.Controller;
import de.timaldinger.einkaufsliste.Dto.ShoppingCategoryDTO;
import de.timaldinger.einkaufsliste.Dto.ShoppingListItemDTO;
import de.timaldinger.einkaufsliste.Models.ShoppingList;
import de.timaldinger.einkaufsliste.Models.ShoppingListCategory;
import de.timaldinger.einkaufsliste.Models.ShoppingListItem;
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.ui.Model;
@@ -24,6 +26,8 @@ public class ShoppingListController {
private ShoppingListCategoryRepository shoppingListCategoryRepository;
@Autowired
private ShoppingListRepository shoppingListRepository;
@Autowired
private ShoppingListItemRepository shoppingListItemRepository;
@PostMapping("/api/list/{listID}/category/")
public ShoppingListCategory addCategory(@PathVariable UUID listID, @RequestBody ShoppingCategoryDTO newCategory){
@@ -37,4 +41,15 @@ public class ShoppingListController {
return category;
}
@PostMapping("/api/list/{listID}/item/")
public ShoppingListItem addItem(@PathVariable UUID listID, @RequestBody ShoppingListItemDTO newItem){
String name = newItem.itemName();
String amount = newItem.itemAmount();
ShoppingListCategory c = shoppingListCategoryRepository.findById(newItem.category()).orElseThrow();
ShoppingList list = shoppingListRepository.findById(listID).orElseThrow();
ShoppingListItem item = new ShoppingListItem(name, amount, false, c, list);
shoppingListItemRepository.save(item);
return item;
}
}
@@ -0,0 +1,6 @@
package de.timaldinger.einkaufsliste.Dto;
import java.util.UUID;
public record ShoppingListItemDTO (String itemName, String itemAmount, UUID category){
}
@@ -4,12 +4,16 @@ import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class ShoppingListItem extends ShoppingElement {
@NotBlank
Whitespace-only changes.
Whitespace-only changes.
+15
View File
@@ -0,0 +1,15 @@
import { ShoppingListItem } from "../shared/";
import { ShoppingCategory } from "./ShoppingCategory.js";
import * as api from "./listAPI.js"
import * as ui from "./listFrontend.js"
const currentList = document.querySelector(".list");
function init() {
}
document.addEventListener('DOMContentLoaded', init);
@@ -0,0 +1,40 @@
export function addItem(currentList, itemName, itemAmount, categoryID) {
let id = currentList.id;
fetch(`/api/list/${id}/item`,
{
method: "POST",
body: JSON.stringify({
"itemName": itemName,
"itemAmount": itemAmount,
"categoryID": categoryID
}),
headers: { "Content-Type": "application/json" }
}).then(r => console.log("added new item"));
return 0;
}
export function deleteItem() {
}
export function addCategory() {
let id = currentList.id;
fetch(`/api/list/${id}/category/`,
{
method: "POST"
, body: JSON.stringify({ "categoryName": categoryName })
, headers: { "Content-Type": "application/json" }
}).then(r => console.log("Add new category"));
listElements.push(new ShoppingCategory(categoryName, renderAddItemPrompt, renderItemList));
renderItemList();
return 0;
}
export function deleteCategory() {
}
@@ -0,0 +1,16 @@
export function addItem() {
}
export function deleteItem() {
}
export function addCategory() {
}
export function deleteCategory() {
}
-107
View File
@@ -1,107 +0,0 @@
import { ShoppingListItem } from "./ShoppingListItem.js";
import { ShoppingCategory } from "./ShoppingCategory.js";
const addItemForm = document.querySelector(".addItemForm");
const addCategoryForm = document.querySelector(".addCategoryForm")
const itemListElement = document.querySelector(".displayedList");
const deleteAllDoneItemsButton = document.querySelector(".deleteAllDoneItemsButton");
const currentList = document.querySelector(".list");
let listElements = []; // Liste der erstellten Items und Kategorien
function addNewCategory(categoryName) {
let id = currentList.id;
fetch(`/api/list/${id}/category/`,
{
method: "POST"
, body: JSON.stringify({ "categoryName": categoryName })
, headers: { "Content-Type": "application/json" }
}).then(r => console.log("Add new category"));
listElements.push(new ShoppingCategory(categoryName, renderAddItemPrompt, renderItemList));
renderItemList();
return 0;
}
export function addNewItem(itemName, itemAmount, category) {
if (category) {
category = listElements.find((element) => element instanceof ShoppingCategory && element.categoryName == category)
category.addItem(new ShoppingListItem(itemName, itemAmount, renderItemList));
} else {
listElements.push(new ShoppingListItem(itemName, itemAmount, renderItemList));
}
renderItemList();
return 0;
}
function renderItemList() {
listElements = listElements.filter(item => !item.remove)
itemListElement.innerHTML = ""; //reset innerItemList
listElements.forEach((element, index) => {
itemListElement.appendChild(element.render(index));
});
}
function deleteAllDoneItems() {
listElements.forEach((category, index) => {
if (category instanceof ShoppingCategory) {
category.itemsInCategory = category.itemsInCategory.filter((item) => !item.isDone);
}
});
listElements = listElements.filter((element) => !element.isDone);
renderItemList();
}
function renderAddItemPrompt() {
const selectMenu = document.querySelector(".addItemForm__categorySelect");
let categories = listElements.filter(element => element instanceof ShoppingCategory);
const emptySelection = document.createElement("option");
emptySelection.setAttribute("value", "");
emptySelection.selected = true;
emptySelection.textContent = "Keine"
selectMenu.innerHTML = "";
selectMenu.appendChild(emptySelection);
categories.forEach(category => {
const option = document.createElement("option");
option.setAttribute("value", category.categoryName);
option.textContent = category.categoryName;
selectMenu.appendChild(option);
});
}
function init() {
renderAddItemPrompt();
addItemForm.addEventListener("submit", (event) => {
if (!addItemForm.checkValidity()) return;
event.preventDefault();
const addItemFormData = Object.fromEntries(new FormData(addItemForm));
addNewItem(addItemFormData.itemName, addItemFormData.itemAmount, addItemFormData.itemCategory);
});
addCategoryForm.addEventListener("submit", (event) => {
if (!addCategoryForm.checkValidity()) return;
event.preventDefault();
const addCategoryFormData = Object.fromEntries(new FormData(addCategoryForm));
addNewCategory(addCategoryFormData.categoryName);
renderAddItemPrompt();
});
deleteAllDoneItemsButton.addEventListener("click", (event) => {
deleteAllDoneItems();
})
}
init();
@@ -0,0 +1,11 @@
export class ShoppingList{
constructor(id){
this.shoppingListName;
this.lastModified = "";
this.created = "";
this.id = id;
this.remove = false;
}
}
+3 -2
View File
@@ -6,20 +6,21 @@ xmlns:th="http://www.thymeleaf.org">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<link href="/css/base.css" rel="stylesheet">
<link href="/css/index.css" rel="stylesheet" >
<script src="/js/index.js" type="module"></script>
<body>
<div class="createShoppingList">
<form th:action="@{/list}" method="post">
<form th:action="@{api/list}" method="post">
<input type="text" name="listName" required />
<button type="submit">Neue Liste erstellen</button>
</form>
</div>
<div class="shoppingListOverwiev">
<ul th:each="shoppingList : ${lists}" >
<li class="shoppingListOverview__shoppingList" th:id="${shoppingList.id}" th:attr="name=${shoppingList.name}">
<li class="shoppingListOverview__shoppingList" th:id="${shoppingList.id}" th:attr="name=${shoppingList.listName}">
<a th:text="${shoppingList.listName}" th:href="@{/list/{id}(id=${shoppingList.id})}"></a>
<button type="button" class="shoppingListOverview__deleteListButton" th:attr="data-id=${shoppingList.id}">Liste löschen</button>
</li>
+19 -6
View File
@@ -4,15 +4,16 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Einkaufsliste</title>
<link href="/css/style.css" rel="stylesheet">
<script src="/js/list/main.js" type="module"></script>
<link href="/css/base.css" rel="stylesheet">
<link href="/css/list.css" rel="stylesheet">
<script src="/js/list/list.js" type="module"></script>
</head>
<body>
<div class="title">
<h1>Einkaufsliste</h1>
<h1 th:text="${list.listName}"></h1>
</div>
<div class="list">
<div class="list" th:id="${list.id}">
<form class="addCategoryForm">
<span>Neue Kategorie hinzufügen:</span>
<input type="text" class="addCategoryForm__nameInput" name="categoryName" placeholder="Neue Kategorie hinzufügen...">
@@ -31,8 +32,20 @@
</form>
<button class="deleteAllDoneItemsButton">Alle fertigen Items löschen</button>
<ul class="displayedList">
</ul>
<ul class="displayedList">
<li> <!-- Es werden erste alle Kategorien erstellt -->
<ul class="displayedList__categoryName--bold displayedList__categoryName--big"
th:each="category: ${categories}"
th:id="${category.id}"
th:text="${category.categoryName}"> <!-- für jede Kategorie wird ein ul erstellt in welcher dann die Items aufgeführt werden -->
<li th:each="item: ${category.itemsInCategory}">
<span th:text="${item.itemName}"></span>
</li>
</ul>
</li>
</ul>
</div>
</body>