refactor: service logik vomo controller in den service gelegt
This commit is contained in:
1 parent
3a1c954023
commit
110e5d415e
2 files changed
+57
-53
No files matched your search
+7
-52
@@ -1,17 +1,9 @@
|
||||
package de.timaldinger.einkaufsliste.Controller;
|
||||
|
||||
import de.timaldinger.einkaufsliste.Dto.Get.ShoppingCategoryGetDTO;
|
||||
import de.timaldinger.einkaufsliste.Dto.Post.ShoppingCategoryPostDTO;
|
||||
import de.timaldinger.einkaufsliste.Dto.Patch.ShoppingCategoryPatchDTO;
|
||||
import de.timaldinger.einkaufsliste.Dto.Post.ShoppingCategoryPostDTO;
|
||||
import de.timaldinger.einkaufsliste.Dto.Response.ShoppingCategoryResponseDTO;
|
||||
import de.timaldinger.einkaufsliste.Exceptions.BadRequestException;
|
||||
import de.timaldinger.einkaufsliste.Exceptions.ResourceNotFoundException;
|
||||
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.ShoppingCategoryService;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -20,17 +12,10 @@ 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 ShoppingCategoryController {
|
||||
@Autowired
|
||||
private ShoppingCategoryRepository shoppingCategoryRepository;
|
||||
@Autowired
|
||||
private ShoppingListRepository shoppingListRepository;
|
||||
@Autowired
|
||||
private ShoppingItemRepository shoppingItemRepository;
|
||||
|
||||
@Autowired
|
||||
private ShoppingCategoryService shoppingCategoryService;
|
||||
@@ -42,56 +27,26 @@ public class ShoppingCategoryController {
|
||||
|
||||
@PostMapping("/api/categories")
|
||||
public ResponseEntity<ShoppingCategoryResponseDTO> addCategory(@Valid @RequestBody ShoppingCategoryPostDTO newCategory){
|
||||
|
||||
String name = newCategory.categoryName();
|
||||
ShoppingList list = shoppingListRepository.findById(newCategory.listID()).orElseThrow(() -> new ResourceNotFoundException("listID",newCategory.listID()));
|
||||
|
||||
ShoppingCategory category = new ShoppingCategory(name, List.of(),list);
|
||||
|
||||
shoppingCategoryRepository.save(category);
|
||||
ShoppingCategoryResponseDTO created = shoppingCategoryService.addCategory(newCategory);
|
||||
|
||||
URI location = ServletUriComponentsBuilder
|
||||
.fromCurrentRequest()
|
||||
.path("/{id}")
|
||||
.buildAndExpand(category.getId())
|
||||
.buildAndExpand(created.categoryID())
|
||||
.toUri();
|
||||
|
||||
return ResponseEntity.created(location).body(new ShoppingCategoryResponseDTO(name, category.getId()));
|
||||
return ResponseEntity.created(location).body(created);
|
||||
}
|
||||
|
||||
@PatchMapping("/api/categories/{categoryID}")
|
||||
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());
|
||||
|
||||
shoppingCategoryRepository.save(category);
|
||||
|
||||
public ResponseEntity<Void> editCategory(@PathVariable UUID categoryID, @Valid @RequestBody ShoppingCategoryPatchDTO toChange){
|
||||
shoppingCategoryService.editCategory(categoryID, toChange);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
|
||||
@DeleteMapping("/api/categories/{categoryID}")
|
||||
public ResponseEntity<Void> deleteCategory(@PathVariable UUID categoryID, @RequestParam(required = false) Boolean deleteAllItems){
|
||||
|
||||
if(deleteAllItems == null){
|
||||
throw new BadRequestException("Missing required parameter deleteAllItems (can be either true or false)");
|
||||
}
|
||||
|
||||
shoppingCategoryRepository.findById(categoryID).orElseThrow(() -> new ResourceNotFoundException("categoryID", categoryID));
|
||||
|
||||
List<ShoppingItem> itemList = shoppingItemRepository.findAllByCategory_Id(categoryID);
|
||||
if(deleteAllItems){
|
||||
itemList.forEach((item) -> shoppingItemRepository.deleteById(item.getId()));
|
||||
}else{
|
||||
itemList.forEach((item) -> item.setCategory(null));
|
||||
shoppingItemRepository.saveAll(itemList);
|
||||
}
|
||||
|
||||
shoppingCategoryRepository.deleteById(categoryID);
|
||||
|
||||
shoppingCategoryService.deleteCategory(categoryID, deleteAllItems);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,13 +1,23 @@
|
||||
package de.timaldinger.einkaufsliste.Service;
|
||||
|
||||
import de.timaldinger.einkaufsliste.Dto.Get.ShoppingCategoryGetDTO;
|
||||
import de.timaldinger.einkaufsliste.Dto.Patch.ShoppingCategoryPatchDTO;
|
||||
import de.timaldinger.einkaufsliste.Dto.Post.ShoppingCategoryPostDTO;
|
||||
import de.timaldinger.einkaufsliste.Dto.Response.ShoppingCategoryResponseDTO;
|
||||
import de.timaldinger.einkaufsliste.Exceptions.BadRequestException;
|
||||
import de.timaldinger.einkaufsliste.Exceptions.ResourceNotFoundException;
|
||||
import de.timaldinger.einkaufsliste.Mapper.ShoppingCategoryMapper;
|
||||
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 jakarta.transaction.Transactional;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
@@ -15,12 +25,51 @@ public class ShoppingCategoryService {
|
||||
|
||||
@Autowired
|
||||
private ShoppingCategoryMapper shoppingCategoryMapper;
|
||||
|
||||
@Autowired
|
||||
private ShoppingCategoryRepository shoppingCategoryRepository;
|
||||
@Autowired
|
||||
private ShoppingListRepository shoppingListRepository;
|
||||
@Autowired
|
||||
private ShoppingItemRepository shoppingItemRepository;
|
||||
|
||||
public ShoppingCategoryGetDTO getCategory(UUID categoryID){
|
||||
ShoppingCategory category = shoppingCategoryRepository.findById(categoryID).orElseThrow(() -> new ResourceNotFoundException("categoryID", categoryID));
|
||||
return shoppingCategoryMapper.toGetDTO(category);
|
||||
}
|
||||
|
||||
public ShoppingCategoryResponseDTO addCategory(ShoppingCategoryPostDTO newCategory){
|
||||
ShoppingList list = shoppingListRepository.findById(newCategory.listID()).orElseThrow(() -> new ResourceNotFoundException("listID", newCategory.listID()));
|
||||
|
||||
ShoppingCategory category = new ShoppingCategory(newCategory.categoryName(), List.of(), list);
|
||||
shoppingCategoryRepository.save(category);
|
||||
|
||||
return new ShoppingCategoryResponseDTO(category.getCategoryName(), category.getId());
|
||||
}
|
||||
|
||||
public void editCategory(UUID categoryID, ShoppingCategoryPatchDTO toChange){
|
||||
ShoppingCategory category = shoppingCategoryRepository.findById(categoryID).orElseThrow(() -> new ResourceNotFoundException("categoryID", categoryID));
|
||||
|
||||
if(toChange.categoryName() != null) category.setCategoryName(toChange.categoryName());
|
||||
|
||||
shoppingCategoryRepository.save(category);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteCategory(UUID categoryID, Boolean deleteAllItems){
|
||||
if(deleteAllItems == null){
|
||||
throw new BadRequestException("Missing required parameter deleteAllItems (can be either true or false)");
|
||||
}
|
||||
|
||||
shoppingCategoryRepository.findById(categoryID).orElseThrow(() -> new ResourceNotFoundException("categoryID", categoryID));
|
||||
|
||||
List<ShoppingItem> itemList = shoppingItemRepository.findAllByCategory_Id(categoryID);
|
||||
if(deleteAllItems){
|
||||
itemList.forEach((item) -> shoppingItemRepository.deleteById(item.getId()));
|
||||
}else{
|
||||
itemList.forEach((item) -> item.setCategory(null));
|
||||
shoppingItemRepository.saveAll(itemList);
|
||||
}
|
||||
|
||||
shoppingCategoryRepository.deleteById(categoryID);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user