Compare commits

8 changed files with 152 additions and 159 deletions

No files matched your search

View File
File renamed without changes.
@@ -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,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,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(), null, null, 0, 0);
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();
}
}
@@ -16,8 +16,4 @@ import java.sql.Date;
@NoArgsConstructor
public class ShoppingList extends ShoppingElement {
private String listName;
private Date dateCreated;
private Date lastChanged;
private int numberItems;
private int numberCategory;
}
@@ -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);
}
}
@@ -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);
}
}
@@ -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);
}
}