refactor: service logic von controller zu service klasse

This commit is contained in:
Technoir committed 2026-10-19 14:27:45 +02:00
1 parent 6508f589b3
commit 6ae4a1d1f6
2 files changed
+38 -49

No files matched your search

@@ -1,16 +1,10 @@
package de.timaldinger.einkaufsliste.Controller;
import de.timaldinger.einkaufsliste.Dto.Get.*;
import de.timaldinger.einkaufsliste.Dto.Get.ShoppingListGetDTO;
import de.timaldinger.einkaufsliste.Dto.Get.ShoppingListsGetDTO;
import de.timaldinger.einkaufsliste.Dto.Patch.ShoppingListPatchDTO;
import de.timaldinger.einkaufsliste.Dto.Post.ShoppingListPostDTO;
import de.timaldinger.einkaufsliste.Dto.Response.ShoppingListResponseDTO;
import de.timaldinger.einkaufsliste.Exceptions.ResourceNotFoundException;
import de.timaldinger.einkaufsliste.Mapper.ShoppingCategoryMapper;
import de.timaldinger.einkaufsliste.Mapper.ShoppingItemMapper;
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.ShoppingListService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
@@ -25,61 +19,41 @@ import java.util.UUID;
@RestController
public class ShoppingListController {
@Autowired
private ShoppingCategoryRepository shoppingCategoryRepository;
@Autowired
private ShoppingListRepository shoppingListRepository;
@Autowired
private ShoppingItemRepository shoppingItemRepository;
@Autowired
private ShoppingListService shoppingListService;
@Autowired
private ShoppingItemMapper shoppingItemMapper;
@Autowired
private ShoppingCategoryMapper shoppingCategoryMapper;
@GetMapping("/api/lists")
public ResponseEntity<List<ShoppingListsGetDTO>> getLists(){
public ResponseEntity<List<ShoppingListsGetDTO>> getLists(){
return ResponseEntity.ok().body(shoppingListService.getLists());
}
}
@GetMapping("/api/lists/{listID}")
public ResponseEntity<ShoppingListGetDTO> getList(@PathVariable UUID listID){
@GetMapping("/api/lists/{listID}")
public ResponseEntity<ShoppingListGetDTO> getList(@PathVariable UUID listID){
return ResponseEntity.ok().body(shoppingListService.getList(listID));
}
}
@PostMapping("/api/lists")
public ResponseEntity<ShoppingListResponseDTO> addList(@Valid @RequestBody ShoppingListPostDTO newList){
ShoppingList list = new ShoppingList(newList.listName());
shoppingListRepository.save(list);
ShoppingListResponseDTO created = shoppingListService.addList(newList);
URI location = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(list.getId())
.buildAndExpand(created.listID())
.toUri();
return ResponseEntity.created(location).body(new ShoppingListResponseDTO(list.getListName(), list.getId()));
return ResponseEntity.created(location).body(created);
}
@DeleteMapping("/api/lists/{listID}")
public ResponseEntity<Void> deleteList(@PathVariable UUID listID){
shoppingListRepository.findById(listID).orElseThrow(() -> new ResourceNotFoundException("listID",listID));
shoppingListService.deleteList(listID);
return ResponseEntity.noContent().build();
}
@PatchMapping("/api/lists/{listID}")
public ResponseEntity<Void> editList(@PathVariable UUID listID,@Valid @RequestBody ShoppingListPatchDTO toChange){
ShoppingList list = shoppingListRepository.findById(listID).orElseThrow(() -> new ResourceNotFoundException("listID", listID));
if(toChange.listName() != null) list.setListName(toChange.listName());
shoppingListRepository.save(list);
public ResponseEntity<Void> editList(@PathVariable UUID listID, @Valid @RequestBody ShoppingListPatchDTO toChange){
shoppingListService.editList(listID, toChange);
return ResponseEntity.noContent().build();
}
}
@@ -1,6 +1,9 @@
package de.timaldinger.einkaufsliste.Service;
import de.timaldinger.einkaufsliste.Dto.Get.*;
import de.timaldinger.einkaufsliste.Dto.Patch.ShoppingListPatchDTO;
import de.timaldinger.einkaufsliste.Dto.Post.ShoppingListPostDTO;
import de.timaldinger.einkaufsliste.Dto.Response.ShoppingListResponseDTO;
import de.timaldinger.einkaufsliste.Exceptions.ResourceNotFoundException;
import de.timaldinger.einkaufsliste.Mapper.ShoppingCategoryMapper;
import de.timaldinger.einkaufsliste.Mapper.ShoppingItemMapper;
@@ -31,27 +34,39 @@ public class ShoppingListService {
@Autowired
private ShoppingListMapper shoppingListMapper;
@Transactional
public void deleteList(UUID listID){
shoppingItemRepository.deleteByListId(listID);
shoppingCategoryRepository.deleteByListId(listID);
shoppingListRepository.deleteById(listID);
}
public List<ShoppingListsGetDTO> getLists(){
return shoppingListMapper.toListsGetDTOs(shoppingListRepository.findAll());
}
public ShoppingListGetDTO getList(UUID listID){
ShoppingList l = shoppingListRepository.findById(listID).orElseThrow( () -> new ResourceNotFoundException("listID", listID));
ShoppingList l = shoppingListRepository.findById(listID).orElseThrow(() -> new ResourceNotFoundException("listID", listID));
List<ShoppingCategoryGetDTO> categories = shoppingCategoryMapper.toGetDTOs(shoppingCategoryRepository.findByList(l));
List<ShoppingItemGetDTO> items = shoppingItemMapper.toGetDTOs(shoppingItemRepository.findByList_IdAndCategoryIsNull(listID));
return new ShoppingListGetDTO(categories,items);
return new ShoppingListGetDTO(categories, items);
}
public ShoppingListResponseDTO addList(ShoppingListPostDTO newList){
ShoppingList list = new ShoppingList(newList.listName());
shoppingListRepository.save(list);
return new ShoppingListResponseDTO(list.getListName(), list.getId());
}
public void editList(UUID listID, ShoppingListPatchDTO toChange){
ShoppingList list = shoppingListRepository.findById(listID).orElseThrow(() -> new ResourceNotFoundException("listID", listID));
if(toChange.listName() != null) list.setListName(toChange.listName());
shoppingListRepository.save(list);
}
@Transactional
public void deleteList(UUID listID){
shoppingListRepository.findById(listID).orElseThrow(() -> new ResourceNotFoundException("listID", listID));
shoppingItemRepository.deleteByListId(listID);
shoppingCategoryRepository.deleteByListId(listID);
shoppingListRepository.deleteById(listID);
}
}