diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/ProductRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/ProductRepository.java index 0682ee55f3ffd9c5a5f35d72dac089d302f59543..08360a6ab573fb7cd79b1a1e3e30677fd9d45cbd 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/ProductRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/ProductRepository.java @@ -34,7 +34,7 @@ public interface ProductRepository { * @param id the ID of the group * @return an optional containing the product if it exists */ - Optional<Product> getByProductName(int id); + Optional<Product> getByProductName(String name); /** * Gets all Products diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ProductService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ProductService.java new file mode 100644 index 0000000000000000000000000000000000000000..55ed3ef64d74dafa458178a5e06125050b914479 --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ProductService.java @@ -0,0 +1,86 @@ +package ntnu.idatt2016.v233.SmartMat.service; + +import lombok.AllArgsConstructor; +import ntnu.idatt2016.v233.SmartMat.model.product.Product; +import ntnu.idatt2016.v233.SmartMat.repository.ProductRepository; +import ntnu.idatt2016.v233.SmartMat.util.ProductUtil; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; +import java.util.Optional; + +/** + * Service for Products + * uses both the ProductRepository and the ProductUtil + * @author Birk + * @version 1.0 + * @since 04.04.2023 + */ +public class ProductService { + ProductRepository productRepository; + + + /** + * Creates a new ProductService + * @param productRepository The repository to use + */ + public ProductService(ProductRepository productRepository) { + this.productRepository = productRepository; + } + + /** + * Gets a product by its ID + * @param id The ID of the product to get + * @return The product with the given ID, if it exists + */ + public Optional<Product> getProductById(int id) { + return productRepository.getById(id); + } + + /** + * Gets all products in the database + * @return All products in the database + */ + public Optional<List<Product>> getAllProducts() { + return productRepository.getAll(); + } + + /** + * Saves a product to the database + * @param product The product to save + * @return The saved product + */ + public Product saveProduct(Product product) { + return productRepository.save(product); + } + + /** + * Deletes a product by its ID + * @param id The ID of the product to delete + */ + public void deleteProductById(int id) { + productRepository.deleteById(id); + } + + /** + * @param name The name of the product to get + * @return The product with the given name, if it exists + */ + public Optional<Product> getProductByName(String name) { + return productRepository.getByProductName(name); + } + + /** + * Gets the volume of a product + * @param id The id of the product to get the volume from + * @return The volume of the product, if it exists + */ + public Optional<String> getProductVolume(int id) { + if(productRepository.getById(id).isEmpty()) + return Optional.empty(); + + return ProductUtil.getVolumeFromProduct(productRepository.getById(id).get()); + } + + +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/ProductUtil.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/ProductUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..d21433675267df8a221a4843435fb1b41abc7da4 --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/ProductUtil.java @@ -0,0 +1,45 @@ +package ntnu.idatt2016.v233.SmartMat.util; + +import ntnu.idatt2016.v233.SmartMat.model.product.Product; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +/** + * Utility class for products + * @author Birk + * @version 1.0 + * @since 04.04.2023 + */ +public class ProductUtil { + + + private static final String[] VOLUME_UNITS = {"ml", "cl", "dl", "l", "g", "kg"}; + + /** + * Gets the volume of a product, if it exists + * By looking at the name and description of the product + * it will try to first find the volume in the name, and then in the description + * It uses pre defined volume units to find the volume + * Todo: Make it be able to find volume if the size is definde by count + * @param product The product to get the volume from + * @return The volume of the product, if it exists + */ + public static Optional<String> getVolumeFromProduct(Product product) { + for (String desc : Arrays.asList(product.name(), product.description())) { + List<String> words = List.of(desc.split(" ")); + if (words.size() > 1) { + String volume = ""; + for (String unit : VOLUME_UNITS) { + int i = words.indexOf(unit); + if (i != -1) { + return Optional.of(words.get(i - 1) + " " + unit); + } + } + } + } + + return Optional.empty(); + } +}