Class ItemService
java.lang.Object
edu.ntnu.idatt2106_2023_06.backend.service.items.ItemService
- All Implemented Interfaces:
IItemService
ItemService is responsible for handling requests related to items, including adding items to the store,
fridge and shopping list. It also has methods to retrieve items from the fridge and shopping list and delete
them from the fridge.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
acceptSuggestion
(ItemRemoveDTO itemDTO) Accepts the suggested item by finding the store, item, fridge, and shopping item associated with the given itemDTO, and then sets the suggestion status of the shopping item to false.void
addIngredientsToShoppingList
(RecipeShoppingDTO recipeShoppingDTO, String username) Adds an item to the item database.void
addToFridge
(ItemDTO itemDTO, Long fridgeId) Adds an item to the fridge.void
addToShoppingList
(ItemDTO itemDTO, Long fridgeId, boolean suggestion) Adds an item to the shopping list for the specified fridge.void
This method fills the database's existing items with their corresponding units.void
buyItemsFromShoppingList
(List<ItemMoveDTO> shoppingItemIds) Buys the specified list of items from the shopping list.void
deleteAllItemsFromShoppingList
(List<ItemRemoveDTO> itemRemoveDTOList) Deletes the specified quantity of many items from the shopping list for the specified fridge.getFridgeItems
(Long fridgeId) Retrieves a list of items from the specified fridge.getShoppingListItems
(Long fridgeId) Retrieves a list of items from the shopping list for the specified fridge.void
removeItemFromFridge
(ItemRemoveDTO itemRemoveDTO) Removes the specified quantity of an item from the specified fridge.void
removeItemFromShoppingList
(ItemRemoveDTO itemRemoveDTO, boolean suggestion) Removes the specified quantity of an item from the shopping list for the specified fridge.org.springframework.data.domain.Page<FridgeItemLoadDTO>
searchFridgeItems
(FridgeItemSearchDTO fridgeItemSearchDTO) Searches for items in the fridge.void
updateFridgeItem
(FridgeItemUpdateDTO fridgeItemUpdateDTO) This method updates a fridge itemvoid
updateShoppingItem
(ShoppingItemUpdateDTO shoppingItemUpdateDTO, String username) This method updates a shopping list item.
-
Constructor Details
-
ItemService
public ItemService()
-
-
Method Details
-
addItem
Adds an item to the item database.- Specified by:
addItem
in interfaceIItemService
- Parameters:
itemDTO
- The ItemDTO containing the item to add.- Returns:
- The item created or found using the itemDTO.
- Throws:
StoreNotFoundException
- if the store cannot be found.ItemNotFoundException
- if the item cannot be found.
-
addToFridge
Adds an item to the fridge.- Specified by:
addToFridge
in interfaceIItemService
- Parameters:
itemDTO
- The dto containing the item informationfridgeId
- The ID of the fridge to add the item to.- Throws:
ItemNotFoundException
- if the item cannot be found.FridgeNotFoundException
- if the fridge cannot be found.
-
addToShoppingList
Adds an item to the shopping list for the specified fridge.- Specified by:
addToShoppingList
in interfaceIItemService
- Parameters:
itemDTO
- The itemDTO containing the essential information for an item.fridgeId
- The ID of the fridge to add the item to the shopping list for.suggestion
- A boolean indicating whether the item is a suggested item or not.- Throws:
ItemNotFoundException
- if the specified item ID does not exist.FridgeNotFoundException
- if the specified fridge ID does not exist.
-
addIngredientsToShoppingList
-
updateFridgeItem
This method updates a fridge item- Parameters:
fridgeItemUpdateDTO
- The DTO containing the information of the update fridge item.
-
updateShoppingItem
This method updates a shopping list item. This may for example be changing a suggestion to an item that can be bought.- Parameters:
shoppingItemUpdateDTO
- The DTO containing the information of the update shopping list item.
-
removeItemFromFridge
Removes the specified quantity of an item from the specified fridge.- Specified by:
removeItemFromFridge
in interfaceIItemService
- Parameters:
itemRemoveDTO
- A DTO object containing the details of the item to remove.- Throws:
StoreNotFoundException
- if the specified store name does not exist.ItemNotFoundException
- if the specified item name does not exist in the specified store.FridgeNotFoundException
- if the specified fridge ID does not exist.FridgeItemsNotFoundException
- if the specified item does not exist in the specified fridge.
-
searchFridgeItems
public org.springframework.data.domain.Page<FridgeItemLoadDTO> searchFridgeItems(FridgeItemSearchDTO fridgeItemSearchDTO) Searches for items in the fridge. The items can be sorted and filtered by expirationDate or purchaseDate.- Specified by:
searchFridgeItems
in interfaceIItemService
- Parameters:
fridgeItemSearchDTO
- The DTO containing the information of the search.- Returns:
- A list of FridgeItemLoadDTOs containing the items that match the search.
-
removeItemFromShoppingList
Removes the specified quantity of an item from the shopping list for the specified fridge.- Specified by:
removeItemFromShoppingList
in interfaceIItemService
- Parameters:
itemRemoveDTO
- A DTO object containing the details of the item to remove.suggestion
- A boolean indicating whether the item is a suggested item or not.- Throws:
StoreNotFoundException
- if the specified store name does not exist.ItemNotFoundException
- if the specified item name does not exist in the specified store.FridgeNotFoundException
- if the specified fridge ID does not exist.ShoppingItemsNotFoundException
- if the specified item does not exist in the shopping list for the specified fridge.
-
deleteAllItemsFromShoppingList
Deletes the specified quantity of many items from the shopping list for the specified fridge.- Specified by:
deleteAllItemsFromShoppingList
in interfaceIItemService
- Parameters:
itemRemoveDTOList
- A DTO object containing the details of the items to remove as a list.
-
buyItemsFromShoppingList
Buys the specified list of items from the shopping list.- Specified by:
buyItemsFromShoppingList
in interfaceIItemService
- Parameters:
shoppingItemIds
- A list of DTO objects containing the details of the items to buy.- Throws:
StoreNotFoundException
- if the specified store name does not exist.ItemNotFoundException
- if the specified item name does not exist in the specified store.FridgeNotFoundException
- if the specified fridge ID does not exist.FridgeItemsNotFoundException
- if the specified item does not exist in the specified fridge.ShoppingItemsNotFoundException
- if the specified item does not exist in the shopping list for the specified fridge.
-
getFridgeItems
Retrieves a list of items from the specified fridge.- Specified by:
getFridgeItems
in interfaceIItemService
- Parameters:
fridgeId
- The ID of the fridge to retrieve items from.- Returns:
- A list of ItemDTO objects representing the items in the fridge.
- Throws:
FridgeNotFoundException
- if the specified fridge ID does not exist.FridgeItemsNotFoundException
- if there are no items in the specified fridge.
-
getShoppingListItems
Retrieves a list of items from the shopping list for the specified fridge.- Specified by:
getShoppingListItems
in interfaceIItemService
- Parameters:
fridgeId
- The ID of the fridge to retrieve shopping list items for.- Returns:
- A list of ItemDTO objects representing the items in the shopping list for the specified fridge.
- Throws:
FridgeNotFoundException
- if the specified fridge ID does not exist.ShoppingItemsNotFoundException
- if there are no items in the shopping list for the specified fridge.
-
acceptSuggestion
Accepts the suggested item by finding the store, item, fridge, and shopping item associated with the given itemDTO, and then sets the suggestion status of the shopping item to false.- Specified by:
acceptSuggestion
in interfaceIItemService
- Parameters:
itemDTO
- an object containing the name of the store, item, and fridge, as well as the ID of the fridge and a boolean indicating whether the item was suggested for removal- Throws:
StoreNotFoundException
- if the store with the given name cannot be found in the store repositoryItemNotFoundException
- if the item with the given name cannot be found in the item repository for the given storeFridgeNotFoundException
- if the fridge with the given ID cannot be found in the fridge repositoryShoppingItemsNotFoundException
- if the shopping item associated with the given item, fridge, and suggestion status cannot be found in the shopping items repository
-
addUnitToExistingItems
public void addUnitToExistingItems()This method fills the database's existing items with their corresponding units. The unit and amount are found through theUnitParser.parse(String)
.
-