PromoteurService.java

  1. package com.sintia.ffl.admin.optique.services.services;

  2. import com.sintia.ffl.admin.optique.dal.entities.Promoteur;
  3. import com.sintia.ffl.admin.optique.dal.repositories.PromoteurRepository;
  4. import com.sintia.ffl.admin.optique.services.mappers.PromoteurMapper;
  5. import com.sintia.ffl.adminui.commons.dto.PromoteurDTO;
  6. import lombok.RequiredArgsConstructor;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.stereotype.Service;

  9. import java.util.List;
  10. import java.util.Optional;
  11. import java.util.stream.Collectors;
  12. import java.util.stream.StreamSupport;

  13. @Slf4j
  14. @Service
  15. @RequiredArgsConstructor
  16. public class PromoteurService {

  17.     private final PromoteurRepository promoteurRepository;

  18.     private final PromoteurMapper promoteurMapper;
  19.    
  20.     /**
  21.      * List the promoteur that may exist in the database, but must not be considered by the application
  22.      */
  23.     private static List<String> PROMOTEUR_UNUSED = List.of("ITE");

  24.     /**
  25.      * Return the list of all the Promoteur that are "really used" by the application.
  26.      *
  27.      * @return
  28.      */
  29.     public List<Promoteur> getAllPromoteursUsed() {
  30.         List<Promoteur> result;
  31.         result = promoteurRepository.findByCodePromoteurNotIn(PROMOTEUR_UNUSED);
  32.         return result;
  33.     }
  34.    
  35.     public List<Promoteur> getAllPromoteurs() {
  36.         List<Promoteur> result = StreamSupport.stream(promoteurRepository.findAll().spliterator(),false).collect(Collectors.toList());
  37.         return result;
  38.     }

  39.     public Optional<PromoteurDTO> saveLogo(Integer idPromoteur, byte[] logo ){
  40.         Optional<Promoteur> optionalPromoteur = promoteurRepository.findById(idPromoteur);
  41.         if (optionalPromoteur.isEmpty())
  42.             return Optional.empty();
  43.         Promoteur promoteur = optionalPromoteur.get();
  44.         promoteur.setLogoFile(logo);
  45.         promoteur = promoteurRepository.save(promoteur);
  46.         log.info("promoteur {}", promoteur);
  47.         PromoteurDTO promoteurDTO = promoteurMapper.toDto(promoteur);
  48.        
  49.         return Optional.of(promoteurDTO);
  50.     }
  51.    
  52.     public Optional<PromoteurDTO> saveManuel(Integer idPromoteur, byte[] docFile ){
  53.         Optional<Promoteur> optionalPromoteur = promoteurRepository.findById(idPromoteur);
  54.         if (optionalPromoteur.isEmpty())
  55.             return Optional.empty();
  56.         Promoteur promoteur = optionalPromoteur.get();
  57.         promoteur.setManuelUtilisateurFile(docFile);
  58.         promoteur = promoteurRepository.save(promoteur);
  59.         PromoteurDTO promoteurDTO = promoteurMapper.toDto(promoteur);
  60.         return Optional.of(promoteurDTO);
  61.     }
  62.    
  63. }