Compare commits
14
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2298ac3895 | ||
|
|
d94dec9062 | ||
|
|
fe1f9fdfd9 | ||
|
|
fd68ce2f34 | ||
|
|
88be63384f | ||
|
|
ad942e38ff | ||
|
|
1aeeecb32b | ||
|
|
68daed2bcc | ||
|
|
35ef1a2bc8 | ||
|
|
da996b5922 | ||
|
|
f1fa9ffcb4 | ||
|
|
b4def65ec0 | ||
|
|
4ba8b4d823 | ||
|
|
f108692809 |
No files matched your search
+3
-2
@@ -11,6 +11,7 @@ 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 jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -32,7 +33,7 @@ public class ShoppingCategoryController {
|
||||
private ShoppingItemRepository shoppingItemRepository;
|
||||
|
||||
@PostMapping("/api/categories")
|
||||
public ResponseEntity<ShoppingCategoryResponseDTO> addCategory(@RequestBody ShoppingCategoryPostDTO newCategory){
|
||||
public ResponseEntity<ShoppingCategoryResponseDTO> addCategory(@Valid @RequestBody ShoppingCategoryPostDTO newCategory){
|
||||
|
||||
String name = newCategory.categoryName();
|
||||
ShoppingList list = shoppingListRepository.findById(newCategory.listID()).orElseThrow(() -> new ResourceNotFoundException("listID",newCategory.listID()));
|
||||
@@ -51,7 +52,7 @@ public class ShoppingCategoryController {
|
||||
}
|
||||
|
||||
@PatchMapping("/api/categories/{categoryID}")
|
||||
public ResponseEntity<Void> editCategory(@PathVariable UUID categoryID,@RequestBody ShoppingCategoryPatchDTO toChange){
|
||||
public ResponseEntity<Void> editCategory(@PathVariable UUID categoryID,@Valid @RequestBody ShoppingCategoryPatchDTO toChange){
|
||||
ShoppingCategory category = shoppingCategoryRepository.findById(categoryID).orElseThrow(() -> new ResourceNotFoundException("categoryID",categoryID));
|
||||
|
||||
if(toChange.categoryName() != null) category.setCategoryName(toChange.categoryName());
|
||||
|
||||
@@ -12,6 +12,7 @@ import de.timaldinger.einkaufsliste.Repository.ShoppingCategoryRepository;
|
||||
import de.timaldinger.einkaufsliste.Repository.ShoppingItemRepository;
|
||||
import de.timaldinger.einkaufsliste.Repository.ShoppingListRepository;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -33,7 +34,7 @@ public class ShoppingItemController {
|
||||
private ShoppingItemRepository shoppingItemRepository;
|
||||
|
||||
@PostMapping("/api/items")
|
||||
public ResponseEntity<ShoppingItemResponseDTO> addItem(@RequestBody ShoppingItemPostDTO newItem){
|
||||
public ResponseEntity<ShoppingItemResponseDTO> addItem(@Valid @RequestBody ShoppingItemPostDTO newItem){
|
||||
String name = newItem.itemName();
|
||||
String amount = newItem.itemAmount();
|
||||
ShoppingCategory c;
|
||||
@@ -57,7 +58,7 @@ public class ShoppingItemController {
|
||||
}
|
||||
|
||||
@PatchMapping("/api/items/{itemID}")
|
||||
public ResponseEntity<Void> editItem(@PathVariable UUID itemID ,@RequestBody ShoppingItemPatchDTO toChange){
|
||||
public ResponseEntity<Void> editItem(@PathVariable UUID itemID , @Valid @RequestBody ShoppingItemPatchDTO toChange){
|
||||
ShoppingItem item = shoppingItemRepository.findById(itemID).orElseThrow(() -> new ResourceNotFoundException("itemID",itemID));
|
||||
|
||||
if(toChange.itemName() != null) item.setItemName(toChange.itemName());
|
||||
|
||||
@@ -10,6 +10,7 @@ import de.timaldinger.einkaufsliste.Repository.ShoppingCategoryRepository;
|
||||
import de.timaldinger.einkaufsliste.Repository.ShoppingItemRepository;
|
||||
import de.timaldinger.einkaufsliste.Repository.ShoppingListRepository;
|
||||
import de.timaldinger.einkaufsliste.Service.ShoppingListService;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -34,7 +35,7 @@ public class ShoppingListController {
|
||||
private ShoppingListService shoppingListService;
|
||||
|
||||
@PostMapping("/api/lists")
|
||||
public ResponseEntity<ShoppingListResponseDTO> addList(@RequestBody ShoppingListPostDTO newList){
|
||||
public ResponseEntity<ShoppingListResponseDTO> addList(@Valid @RequestBody ShoppingListPostDTO newList){
|
||||
ShoppingList list = new ShoppingList(newList.listName(), null, null, 0, 0);
|
||||
|
||||
shoppingListRepository.save(list);
|
||||
@@ -57,7 +58,7 @@ public class ShoppingListController {
|
||||
}
|
||||
|
||||
@PatchMapping("/api/lists/{listID}")
|
||||
public ResponseEntity<Void> editList(@PathVariable UUID listID,@RequestBody ShoppingListPatchDTO toChange){
|
||||
public ResponseEntity<Void> editList(@PathVariable UUID listID,@Valid @RequestBody ShoppingListPatchDTO toChange){
|
||||
ShoppingList list = shoppingListRepository.findById(listID).orElseThrow(() -> new ResourceNotFoundException("listID", listID));
|
||||
|
||||
if(toChange.newListName() != null) list.setListName(toChange.newListName());
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
package de.timaldinger.einkaufsliste.Dto.Patch;
|
||||
|
||||
public record ShoppingCategoryPatchDTO(String categoryName) {
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public record ShoppingCategoryPatchDTO(@NotBlank(message = "categoryName can't be blank") String categoryName) {
|
||||
}
|
||||
@@ -1,6 +1,15 @@
|
||||
package de.timaldinger.einkaufsliste.Dto.Patch;
|
||||
|
||||
import jakarta.validation.constraints.AssertTrue;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public record ShoppingItemPatchDTO(String itemName, String itemAmount, Boolean isDone, UUID categoryID) {
|
||||
|
||||
@AssertTrue(message = "itemName and itemAmount both cant be blank!")
|
||||
private boolean isAtleastOneNotBlank(){
|
||||
boolean n = itemName != null && !itemName.isBlank();
|
||||
boolean a = itemAmount != null && !itemAmount.isBlank();
|
||||
return n || a;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
package de.timaldinger.einkaufsliste.Dto.Patch;
|
||||
|
||||
public record ShoppingListPatchDTO(String newListName) {
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public record ShoppingListPatchDTO(@NotBlank(message = "listName must not be blank!") String newListName) {
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package de.timaldinger.einkaufsliste.Dto.Post;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public record ShoppingCategoryPostDTO(String categoryName, UUID listID) {
|
||||
public record ShoppingCategoryPostDTO(@NotBlank String categoryName, UUID listID) {
|
||||
}
|
||||
@@ -1,6 +1,20 @@
|
||||
package de.timaldinger.einkaufsliste.Dto.Post;
|
||||
|
||||
import jakarta.validation.constraints.AssertTrue;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public record ShoppingItemPostDTO(String itemName, String itemAmount, UUID categoryID, UUID listID){
|
||||
public record ShoppingItemPostDTO(String itemName,
|
||||
String itemAmount,
|
||||
UUID categoryID,
|
||||
@NotNull(message = "listID must not be empty!")
|
||||
UUID listID){
|
||||
|
||||
@AssertTrue(message = "itemName and itemAmount cant both be blank!")
|
||||
private boolean isAtleastOneNotBlank(){
|
||||
boolean n = itemName != null && !itemName.isBlank();
|
||||
boolean a = itemAmount != null && !itemAmount.isBlank();
|
||||
return n || a;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
package de.timaldinger.einkaufsliste.Dto.Post;
|
||||
|
||||
public record ShoppingListPostDTO(String listName) {
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public record ShoppingListPostDTO(@NotBlank(message = "listName must not be blank!") String listName) {
|
||||
}
|
||||
@@ -2,19 +2,28 @@ package de.timaldinger.einkaufsliste.Exceptions;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||
|
||||
@ExceptionHandler(BadRequestException.class)
|
||||
public ResponseEntity<ErrorResponse> handleBadRequest(BadRequestException e, HttpServletRequest req) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
||||
@@ -28,6 +37,7 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ErrorResponse> handleAll(Exception e, HttpServletRequest req){
|
||||
log.error("{}",req.getRequestURI(), e);
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ErrorResponse(500,"Internal error", req.getRequestURI()));
|
||||
}
|
||||
|
||||
@@ -36,4 +46,13 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
var error = new ErrorResponse(statusCode.value(), ex.getMessage(),((ServletWebRequest) request).getRequest().getRequestURI());
|
||||
return new ResponseEntity<>(error,headers,statusCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @Nullable ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
String msg = ex.getBindingResult().getFieldErrors().stream()
|
||||
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||
.collect(Collectors.joining(", "));
|
||||
var error = new ErrorResponse(status.value(), msg ,((ServletWebRequest) request).getRequest().getRequestURI());
|
||||
return new ResponseEntity<>(error,headers,status);
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,6 @@ import java.util.List;
|
||||
@NoArgsConstructor
|
||||
public class ShoppingCategory extends ShoppingElement{
|
||||
|
||||
@NotBlank
|
||||
private String categoryName;
|
||||
@OneToMany(mappedBy = "category") //eine Liste hat mehrere Kategorien, diese sind mappedBy category (Feld in ShoppingItem) damit keine extra Tabelle erzeugt wird
|
||||
private List<ShoppingItem> itemsInCategory;
|
||||
|
||||
@@ -14,7 +14,6 @@ import lombok.*;
|
||||
@ToString
|
||||
public class ShoppingItem extends ShoppingElement {
|
||||
|
||||
@NotBlank
|
||||
private String itemName;
|
||||
private String itemAmount;
|
||||
private boolean isDone;
|
||||
|
||||
@@ -15,7 +15,6 @@ import java.sql.Date;
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ShoppingList extends ShoppingElement {
|
||||
@NotBlank
|
||||
private String listName;
|
||||
private Date dateCreated;
|
||||
private Date lastChanged;
|
||||
|
||||
@@ -29,6 +29,9 @@ export function createEditListForm(textElement) {
|
||||
abortButton.textContent = "Abbrechen";
|
||||
saveButton.textContent = "Speichern";
|
||||
|
||||
saveButton.classList.add("editForm__saveButton");
|
||||
abortButton.classList.add("editForm__abortButton");
|
||||
|
||||
editForm.appendChild(inputName);
|
||||
editForm.appendChild(saveButton);
|
||||
editForm.appendChild(abortButton);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ShoppingList } from "../../models/ShoppingList.js";
|
||||
import { showError, initErrors } from "../../shared/error.js";
|
||||
import * as api from "./api/indexAPI.js";
|
||||
import * as ui from "./frontend/indexUI.js"
|
||||
|
||||
@@ -9,7 +9,8 @@ async function addList(listName) {
|
||||
const json = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.error(`error creating list ${json}`);
|
||||
console.error(`error creating list ${json.msg}`);
|
||||
showError(json.msg);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -34,6 +35,8 @@ function editList(listLi) {
|
||||
if (!response.ok) {
|
||||
const json = await response.json();
|
||||
console.error(`error editing list ${json}`);
|
||||
showError(json.msg);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -45,6 +48,9 @@ function editList(listLi) {
|
||||
save();
|
||||
});
|
||||
|
||||
formElement.querySelector(".editForm__abortButton").addEventListener("click", () => {
|
||||
formElement.replaceWith(listLi);
|
||||
})
|
||||
formElement.addEventListener("keydown", (e) => {
|
||||
if (e.key == "Escape") formElement.replaceWith(listLi);
|
||||
})
|
||||
@@ -59,6 +65,8 @@ async function deleteList(listElement) {
|
||||
if (!response.ok) {
|
||||
const json = await response.json();
|
||||
console.error(`error deleting list ${json}`);
|
||||
showError(json.msg);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -67,6 +75,8 @@ async function deleteList(listElement) {
|
||||
|
||||
|
||||
function init() {
|
||||
|
||||
initErrors(document.querySelector(".nav"));
|
||||
addListForm.addEventListener("submit", (e) => {
|
||||
e.preventDefault();
|
||||
const formData = Object.fromEntries(new FormData(addListForm));
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { showError, initErrors } from "../../shared/error.js";
|
||||
import * as api from "./api/listAPI.js"
|
||||
import * as ui from "./frontend/listUI.js"
|
||||
|
||||
@@ -15,6 +16,7 @@ async function addItem(itemName, itemAmount, categoryID) {
|
||||
|
||||
if (!response.ok) {
|
||||
console.error(`Error, item couldnt be created ${json}`);
|
||||
showError(json.msg);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -29,7 +31,9 @@ async function addCategory(categoryName) {
|
||||
const json = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
console.error(`Error, category couldnt be created ${json}`)
|
||||
console.error(`Error, category couldnt be created ${json}`);
|
||||
showError(json.msg);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -54,11 +58,12 @@ async function editItem(textElement) {
|
||||
let formData = Object.fromEntries(new FormData(formElement));
|
||||
console.log(`editing item ${id} with ${formData}`)
|
||||
|
||||
const response = await api.editItem(id, { itemName: formData.newName, itemAmount: formData.newAmount });
|
||||
const response = await api.editItem(id, { itemName: formData.newItemName, itemAmount: formData.newItemAmount });
|
||||
|
||||
if (!response.ok) {
|
||||
const json = await response.json();
|
||||
console.error(`Error editingItem ${json}`);
|
||||
showError(json.msg)
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -99,6 +104,7 @@ function editCategory(categoryElement) {
|
||||
if (!response.ok) {
|
||||
const json = await response.json();
|
||||
console.error(`Error editing category ${json}`);
|
||||
showError(json.msg);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -121,6 +127,7 @@ async function deleteItem(li) {
|
||||
if (!response.ok) {
|
||||
const json = await response.json();
|
||||
console.error(`Error deleting item ${json}`);
|
||||
showError(json.msg);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -134,6 +141,7 @@ async function deleteCategory(deleteAllItems, categoryID) {
|
||||
if (!response.ok) {
|
||||
const json = await response.json();
|
||||
console.error(`Error deleting category ${json}`);
|
||||
showError(json.msg);
|
||||
return;
|
||||
}
|
||||
ui.deleteCategory(deleteAllItems, categoryID);
|
||||
@@ -146,12 +154,17 @@ async function deleteAllDoneItems() {
|
||||
if (!response.ok) {
|
||||
const json = await response.json();
|
||||
console.error(`Error deleting all done items ${json}`);
|
||||
showError(json.msg);
|
||||
|
||||
return;
|
||||
}
|
||||
ui.deleteAllDoneItems();
|
||||
}
|
||||
|
||||
function init() {
|
||||
|
||||
initErrors(document.querySelector(".nav"));
|
||||
|
||||
addCategoryForm.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
let formData = Object.fromEntries(new FormData(addCategoryForm));
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
const errorDiv = document.createElement("div");
|
||||
const errorSpan = document.createElement("span");
|
||||
let errorTimer;
|
||||
|
||||
export function showError(msg) {
|
||||
errorSpan.textContent = msg;
|
||||
errorDiv.hidden = false
|
||||
|
||||
clearTimeout(errorTimer);
|
||||
errorTimer = (setTimeout(() => {
|
||||
errorDiv.hidden = true;
|
||||
errorSpan.textContent = "";
|
||||
}, 3000));
|
||||
}
|
||||
|
||||
|
||||
export function initErrors(element) {
|
||||
errorDiv.hidden = true;
|
||||
errorDiv.append(errorSpan);
|
||||
|
||||
element.append(errorDiv);
|
||||
}
|
||||
Reference in new issue
Block a user