/
itokar98
/
HomeWorkPatternsSolidTask1
Обзор
Документация
Войти
/
itokar98
/
HomeWorkPatternsSolidTask1
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/service/ProductService.java
77 строк
3 KB
Ilya Tokar
first_commit
19 ноя 2025, 01:14
19 ноя 2025, 01:14
8a62050
Код
Авторство
О чём код?
package service; import model.Product; import repository.ProductRepository; import java.util.List; public class ProductService { private final ProductRepository productRepository; public ProductService(ProductRepository productRepository) { this.productRepository = productRepository; } public List<Product> getAllProducts() { return productRepository.findAll(); } public Product getProductById(Long id) { return productRepository.findById(id) .orElseThrow(() -> new IllegalArgumentException("Product not found with id: " + id)); } public List<Product> getProductsByCategory(String category) { return productRepository.findByCategory(category); } public List<Product> getProductsByManufacturer(String manufacturer) { return productRepository.findByManufacturer(manufacturer); } public List<Product> searchProducts(String keyword) { return productRepository.searchByName(keyword); } public List<Product> getProductsByPriceRange(double minPrice, double maxPrice) { if (minPrice < 0 || maxPrice < 0) { throw new IllegalArgumentException("Prices cannot be negative"); } if (minPrice > maxPrice) { throw new IllegalArgumentException("Min price cannot be greater than max price"); } return productRepository.findByPriceRange(minPrice, maxPrice); } public void addProduct(Product product) { validateProduct(product); productRepository.addProduct(product); } public void updateProduct(Product product) { validateProduct(product); productRepository.updateProduct(product); } public void deleteProduct(Long id) { productRepository.deleteProduct(id); } private void validateProduct(Product product) { if (product == null) { throw new IllegalArgumentException("Product cannot be null"); } if (product.getName() == null || product.getName().trim().isEmpty()) { throw new IllegalArgumentException("Product name cannot be null or empty"); } if (product.getManufacturer() == null || product.getManufacturer().trim().isEmpty()) { throw new IllegalArgumentException("Manufacturer cannot be null or empty"); } if (product.getPrice() <= 0) { throw new IllegalArgumentException("Price must be positive"); } if (product.getCategory() == null || product.getCategory().trim().isEmpty()) { throw new IllegalArgumentException("Category cannot be null or empty"); } } }