Advertisement
andersonalmada

Untitled

Jul 29th, 2022
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. package br.ufc.mandacaru5.service;
  2.  
  3. import java.util.List;
  4. import java.util.Optional;
  5.  
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8.  
  9. import br.ufc.mandacaru5.model.Product;
  10. import br.ufc.mandacaru5.repository.ProductRepository;
  11.  
  12. @Service
  13. public class ProductService {
  14.  
  15.     @Autowired
  16.     ProductRepository productRepository;
  17.  
  18.     public void save(int id, Product entity) {
  19.         if(id != 0) {
  20.             entity.setId(id);
  21.         }
  22.         productRepository.save(entity);
  23.     }
  24.  
  25.     public void delete(int id) {
  26.         Product product = find(id);
  27.         productRepository.delete(product);
  28.     }
  29.  
  30.     public Product find(int id) {
  31.         if(id < 1) {
  32.             return null;
  33.         }
  34.        
  35.         Optional<Product> product = productRepository.findById(id);
  36.        
  37.         if(product.isPresent()) {
  38.             return product.get();  
  39.         }
  40.        
  41.         return null;       
  42.     }
  43.  
  44.     public List<Product> findAll() {
  45.         return productRepository.findAll();
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement