From 201c9803e3f50a24ac1195840f950e4b8f474c89 Mon Sep 17 00:00:00 2001 From: Stian Lyng <stianlyng@protonmail.com> Date: Tue, 18 Apr 2023 14:36:22 +0200 Subject: [PATCH] add service --- .../SmartMat/service/ShoppingListService.java | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/main/java/ntnu/idatt2016/v233/SmartMat/service/ShoppingListService.java diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ShoppingListService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ShoppingListService.java new file mode 100644 index 00000000..5afda59f --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ShoppingListService.java @@ -0,0 +1,84 @@ +package ntnu.idatt2016.v233.SmartMat.service; + +import java.util.List; +import java.util.Optional; + +import ntnu.idatt2016.v233.SmartMat.model.ShoppingList; +import ntnu.idatt2016.v233.SmartMat.repository.ShoppingListReposity; + +/** + * Service for the shopping list + * + * @author Stian Lyng + * @version 1.0 + */ +public class ShoppingListService { + + ShoppingListReposity shoppingListRepository; + /** + * Creates a new ShoppingListService + * + * @param shoppingListRepository The repository to use + */ + public ShoppingListService(ShoppingListReposity shoppingListRepository) { + this.shoppingListRepository = shoppingListRepository; + } + + /** + * Saves a shopping list to the database + * @param shoppingList the shopping list to save + * @return the saved shopping list + */ + public ShoppingList saveShoppingList(ShoppingList shoppingList) { + return shoppingListRepository.save(shoppingList); + } + + /** + * Gets a shopping list by its ID + * + * @param id the ID of the shopping list + * @return an optional containing the shopping list if it exists + */ + public Optional<ShoppingList> getShoppingListById(int id) { + return shoppingListRepository.getById(id); + } + + /** + * Gets a shopping list by its group ID + * + * @param id the ID of the group + * @return an optional containing the shopping list if it exists + */ + public Optional<ShoppingList> getShoppingListByGroupId(int id) { + return shoppingListRepository.getByGroupID(id); + } + + /** + * Gets all shopping lists + * + * @return an optional containing a list of all shopping lists if they exist + */ + public Optional<List<ShoppingList>> getAllShoppingLists() { + return shoppingListRepository.getAll(); + } + + /** + * Gets all shopping lists by group ID + * + * @param id the ID of the group + * @return an optional containing a list of all shopping lists if they exist + */ + public Optional<List<ShoppingList>> getAllShoppingListsByGroupId(int id) { + return shoppingListRepository.getAllByGroupID(id); + } + + /** + * Deletes a shopping list by its ID + * + * @param id the ID of the shopping list + */ + public void deleteShoppingListById(int id) { + shoppingListRepository.deleteById(id); + } + +} -- GitLab