Advertisement
andersonalmada2

Untitled

Jul 30th, 2022
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 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.Feedback;
  10. import br.ufc.mandacaru5.model.Product;
  11. import br.ufc.mandacaru5.repository.FeedbackRepository;
  12. import br.ufc.mandacaru5.repository.ProductRepository;
  13.  
  14. @Service
  15. public class FeedbackService {
  16.  
  17.     @Autowired
  18.     FeedbackRepository feedbackRepository;
  19.    
  20.     @Autowired
  21.     ProductRepository productRepository;
  22.  
  23.     public void update(int id, Feedback entity) {
  24.         Feedback feedback = find(id);      
  25.         feedback.setMessage(entity.getMessage());
  26.        
  27.         feedbackRepository.save(feedback);             
  28.     }
  29.    
  30.     public void save(int product_id, Feedback entity) {
  31.         Product product = productRepository.findById(product_id).get();
  32.         entity.setProduct(product);
  33.         feedbackRepository.save(entity);               
  34.     }
  35.  
  36.     public void delete(int id) {
  37.         Feedback feedback = find(id);
  38.         feedbackRepository.delete(feedback);
  39.     }
  40.  
  41.     public Feedback find(int id) {
  42.         if (id < 1) {
  43.             return null;
  44.         }
  45.  
  46.         Optional<Feedback> feedback = feedbackRepository.findById(id);
  47.  
  48.         if (feedback.isPresent()) {
  49.             return feedback.get();
  50.         }
  51.  
  52.         return null;
  53.     }
  54.  
  55.     public List<Feedback> findAll(int product_id) {
  56.         return feedbackRepository.findByProductId(product_id);
  57.     }
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement