refactor: service logik von controller in die service klasse geelgt

This commit is contained in:
Technoir committed 2026-10-19 14:27:45 +02:00
1 parent 110e5d415e
commit 6508f589b3
2 files changed
+57 -53

No files matched your search

@@ -1,18 +1,9 @@
package de.timaldinger.einkaufsliste.Controller;
import de.timaldinger.einkaufsliste.Dto.Get.ShoppingItemGetDTO;
import de.timaldinger.einkaufsliste.Dto.Post.ShoppingItemPostDTO;
import de.timaldinger.einkaufsliste.Dto.Patch.ShoppingItemPatchDTO;
import de.timaldinger.einkaufsliste.Dto.Post.ShoppingItemPostDTO;
import de.timaldinger.einkaufsliste.Dto.Response.ShoppingItemResponseDTO;
import de.timaldinger.einkaufsliste.Exceptions.BadRequestException;
import de.timaldinger.einkaufsliste.Exceptions.ResourceNotFoundException;
import de.timaldinger.einkaufsliste.Mapper.ShoppingItemMapper;
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 de.timaldinger.einkaufsliste.Service.ShoppingItemService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
@@ -21,24 +12,14 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.net.URI;
import java.util.List;
import java.util.UUID;
@RestController
public class ShoppingItemController {
@Autowired
private ShoppingCategoryRepository shoppingCategoryRepository;
@Autowired
private ShoppingListRepository shoppingListRepository;
@Autowired
private ShoppingItemRepository shoppingItemRepository;
@Autowired
private ShoppingItemService shoppingItemService;
@Autowired
private ShoppingItemMapper shoppingItemMapper;
@GetMapping("/api/items/{itemID}")
public ResponseEntity<ShoppingItemGetDTO> getItem(@PathVariable UUID itemID){
return ResponseEntity.ok().body(shoppingItemService.getItem(itemID));
@@ -46,58 +27,32 @@ public class ShoppingItemController {
@PostMapping("/api/items")
public ResponseEntity<ShoppingItemResponseDTO> addItem(@Valid @RequestBody ShoppingItemPostDTO newItem){
String name = newItem.itemName();
String amount = newItem.itemAmount();
ShoppingCategory c;
if(newItem.categoryID() != null){
c = shoppingCategoryRepository.findById(newItem.categoryID()).orElseThrow(() -> new ResourceNotFoundException("categoryID", newItem.categoryID()));
}else{
c = null;
}
ShoppingList list = shoppingListRepository.findById(newItem.listID()).orElseThrow(() -> new ResourceNotFoundException("listID", newItem.listID()));
ShoppingItem item = new ShoppingItem(name, amount, false, c, list);
shoppingItemRepository.save(item);
ShoppingItemResponseDTO created = shoppingItemService.addItem(newItem);
URI location = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(item.getId())
.buildAndExpand(created.itemID())
.toUri();
return ResponseEntity.created(location).body(new ShoppingItemResponseDTO(name,amount,item.getId(),c != null ? c.getId() : null));
return ResponseEntity.created(location).body(created);
}
@PatchMapping("/api/items/{itemID}")
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());
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(() -> new ResourceNotFoundException("categoryID",toChange.categoryID())));
shoppingItemRepository.save(item);
public ResponseEntity<Void> editItem(@PathVariable UUID itemID, @Valid @RequestBody ShoppingItemPatchDTO toChange){
shoppingItemService.editItem(itemID, toChange);
return ResponseEntity.noContent().build();
}
@DeleteMapping("/api/items/{itemID}")
public ResponseEntity<Void> deleteItem(@PathVariable UUID itemID){
shoppingItemRepository.findById(itemID).orElseThrow(() ->new ResourceNotFoundException("itemID",itemID));
shoppingItemRepository.deleteById(itemID);
shoppingItemService.deleteItem(itemID);
return ResponseEntity.noContent().build();
}
@DeleteMapping("/api/lists/{listID}/items")
public ResponseEntity<Void> deleteAllDoneItems(@PathVariable UUID listID, @RequestParam boolean done){
if(!done) throw new BadRequestException("Parameter \"done\" must be true!");
shoppingListRepository.findById(listID).orElseThrow(() -> new ResourceNotFoundException("listID", listID));
List<ShoppingItem> items = shoppingItemRepository.findByList_IdAndIsDoneIsTrue(listID);
shoppingItemRepository.deleteAll(items);
shoppingItemService.deleteAllDoneItems(listID, done);
return ResponseEntity.noContent().build();
}
}
@@ -1,13 +1,22 @@
package de.timaldinger.einkaufsliste.Service;
import de.timaldinger.einkaufsliste.Dto.Get.ShoppingItemGetDTO;
import de.timaldinger.einkaufsliste.Dto.Patch.ShoppingItemPatchDTO;
import de.timaldinger.einkaufsliste.Dto.Post.ShoppingItemPostDTO;
import de.timaldinger.einkaufsliste.Dto.Response.ShoppingItemResponseDTO;
import de.timaldinger.einkaufsliste.Exceptions.BadRequestException;
import de.timaldinger.einkaufsliste.Exceptions.ResourceNotFoundException;
import de.timaldinger.einkaufsliste.Mapper.ShoppingItemMapper;
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.stereotype.Service;
import java.util.List;
import java.util.UUID;
@Service
@@ -17,10 +26,50 @@ public class ShoppingItemService {
private ShoppingItemMapper shoppingItemMapper;
@Autowired
private ShoppingItemRepository shoppingItemRepository;
@Autowired
private ShoppingCategoryRepository shoppingCategoryRepository;
@Autowired
private ShoppingListRepository shoppingListRepository;
public ShoppingItemGetDTO getItem(UUID itemID){
ShoppingItem item = shoppingItemRepository.findById(itemID).orElseThrow(() -> new ResourceNotFoundException("itemID", itemID));
return shoppingItemMapper.toGetDTO(item);
}
public ShoppingItemResponseDTO addItem(ShoppingItemPostDTO newItem){
ShoppingCategory category = null;
if(newItem.categoryID() != null){
category = shoppingCategoryRepository.findById(newItem.categoryID()).orElseThrow(() -> new ResourceNotFoundException("categoryID", newItem.categoryID()));
}
ShoppingList list = shoppingListRepository.findById(newItem.listID()).orElseThrow(() -> new ResourceNotFoundException("listID", newItem.listID()));
ShoppingItem item = new ShoppingItem(newItem.itemName(), newItem.itemAmount(), false, category, list);
shoppingItemRepository.save(item);
return new ShoppingItemResponseDTO(item.getItemName(), item.getItemAmount(), item.getId(), category != null ? category.getId() : null);
}
public void editItem(UUID itemID, ShoppingItemPatchDTO toChange){
ShoppingItem item = shoppingItemRepository.findById(itemID).orElseThrow(() -> new ResourceNotFoundException("itemID", itemID));
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(() -> new ResourceNotFoundException("categoryID", toChange.categoryID())));
shoppingItemRepository.save(item);
}
public void deleteItem(UUID itemID){
shoppingItemRepository.findById(itemID).orElseThrow(() -> new ResourceNotFoundException("itemID", itemID));
shoppingItemRepository.deleteById(itemID);
}
public void deleteAllDoneItems(UUID listID, boolean done){
if(!done) throw new BadRequestException("Parameter \"done\" must be true!");
shoppingListRepository.findById(listID).orElseThrow(() -> new ResourceNotFoundException("listID", listID));
List<ShoppingItem> items = shoppingItemRepository.findByList_IdAndIsDoneIsTrue(listID);
shoppingItemRepository.deleteAll(items);
}
}