PromoteurService.java

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

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

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@Slf4j
@Service
@RequiredArgsConstructor
public class PromoteurService {

	private final PromoteurRepository promoteurRepository;

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

	/**
	 * Return the list of all the Promoteur that are "really used" by the application.
	 * 
	 * @return
	 */
	public List<Promoteur> getAllPromoteursUsed() {
		List<Promoteur> result;
		result = promoteurRepository.findByCodePromoteurNotIn(PROMOTEUR_UNUSED);
		return result;
	}
	
	public List<Promoteur> getAllPromoteurs() {
		List<Promoteur> result = StreamSupport.stream(promoteurRepository.findAll().spliterator(),false).collect(Collectors.toList());
		return result;
	}

	public Optional<PromoteurDTO> saveLogo(Integer idPromoteur, byte[] logo ){
		Optional<Promoteur> optionalPromoteur = promoteurRepository.findById(idPromoteur);
		if (optionalPromoteur.isEmpty())
			return Optional.empty();
		Promoteur promoteur = optionalPromoteur.get();
		promoteur.setLogoFile(logo);
		promoteur = promoteurRepository.save(promoteur);
		log.info("promoteur {}", promoteur);
		PromoteurDTO promoteurDTO = promoteurMapper.toDto(promoteur);
		
		return Optional.of(promoteurDTO);
	}
	
	public Optional<PromoteurDTO> saveManuel(Integer idPromoteur, byte[] docFile ){
		Optional<Promoteur> optionalPromoteur = promoteurRepository.findById(idPromoteur);
		if (optionalPromoteur.isEmpty())
			return Optional.empty();
		Promoteur promoteur = optionalPromoteur.get();
		promoteur.setManuelUtilisateurFile(docFile);
		promoteur = promoteurRepository.save(promoteur);
		PromoteurDTO promoteurDTO = promoteurMapper.toDto(promoteur);
		return Optional.of(promoteurDTO);
	}
	
}