java.lang.Object
edu.ntnu.idatt2106_2023_06.backend.service.items.ItemService
All Implemented Interfaces:
IItemService

@Service public class ItemService extends Object implements 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 Details

    • ItemService

      public ItemService()
  • Method Details

    • addItem

      public Item addItem(ItemDTO itemDTO)
      Adds an item to the item database.
      Specified by:
      addItem in interface IItemService
      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

      public void addToFridge(ItemDTO itemDTO, Long fridgeId)
      Adds an item to the fridge.
      Specified by:
      addToFridge in interface IItemService
      Parameters:
      itemDTO - The dto containing the item information
      fridgeId - 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

      public void addToShoppingList(ItemDTO itemDTO, Long fridgeId, boolean suggestion)
      Adds an item to the shopping list for the specified fridge.
      Specified by:
      addToShoppingList in interface IItemService
      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

      public void addIngredientsToShoppingList(RecipeShoppingDTO recipeShoppingDTO, String username)
    • updateFridgeItem

      public void updateFridgeItem(FridgeItemUpdateDTO fridgeItemUpdateDTO)
      This method updates a fridge item
      Parameters:
      fridgeItemUpdateDTO - The DTO containing the information of the update fridge item.
    • updateShoppingItem

      public void updateShoppingItem(ShoppingItemUpdateDTO shoppingItemUpdateDTO, String username)
      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

      public void removeItemFromFridge(ItemRemoveDTO itemRemoveDTO)
      Removes the specified quantity of an item from the specified fridge.
      Specified by:
      removeItemFromFridge in interface IItemService
      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 interface IItemService
      Parameters:
      fridgeItemSearchDTO - The DTO containing the information of the search.
      Returns:
      A list of FridgeItemLoadDTOs containing the items that match the search.
    • removeItemFromShoppingList

      public void removeItemFromShoppingList(ItemRemoveDTO itemRemoveDTO, boolean suggestion)
      Removes the specified quantity of an item from the shopping list for the specified fridge.
      Specified by:
      removeItemFromShoppingList in interface IItemService
      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

      public void deleteAllItemsFromShoppingList(List<ItemRemoveDTO> itemRemoveDTOList)
      Deletes the specified quantity of many items from the shopping list for the specified fridge.
      Specified by:
      deleteAllItemsFromShoppingList in interface IItemService
      Parameters:
      itemRemoveDTOList - A DTO object containing the details of the items to remove as a list.
    • buyItemsFromShoppingList

      public void buyItemsFromShoppingList(List<ItemMoveDTO> shoppingItemIds)
      Buys the specified list of items from the shopping list.
      Specified by:
      buyItemsFromShoppingList in interface IItemService
      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

      public List<FridgeItemLoadDTO> getFridgeItems(Long fridgeId)
      Retrieves a list of items from the specified fridge.
      Specified by:
      getFridgeItems in interface IItemService
      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

      public List<ShoppingListLoadDTO> getShoppingListItems(Long fridgeId)
      Retrieves a list of items from the shopping list for the specified fridge.
      Specified by:
      getShoppingListItems in interface IItemService
      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

      public void 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.
      Specified by:
      acceptSuggestion in interface IItemService
      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 repository
      ItemNotFoundException - if the item with the given name cannot be found in the item repository for the given store
      FridgeNotFoundException - if the fridge with the given ID cannot be found in the fridge repository
      ShoppingItemsNotFoundException - 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 the UnitParser.parse(String).