Skip to content
Snippets Groups Projects
Commit 90a32fa7 authored by harryTheWizzard's avatar harryTheWizzard
Browse files

added product service and utility

parent d84f1bac
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
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());
}
}
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();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment