FabricantService.java

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

import com.sintia.ffl.admin.optique.dal.entities.catalogue.FabricantCatalogue;
import com.sintia.ffl.admin.optique.dal.repositories.FabricantConventionneRepository;
import com.sintia.ffl.admin.optique.dal.repositories.catalogue.FabricantCatalogueRepository;
import com.sintia.ffl.admin.optique.services.dto.FabricantDistributeur;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;

@Service
public class FabricantService {

	@Autowired
	private FabricantCatalogueRepository fabricantRepository;

	@Autowired
	private FabricantConventionneRepository fabricantConventionneRepository;

	/**
	 * Return true if a fabricant already exists in the database with the same cOptoCodeFabricant
	 *
	 * @param cOptoCodeFabricant
	 * @return
	 */
	public boolean exist(String cOptoCodeFabricant) {

		if (cOptoCodeFabricant == null) {
			return false;
		}

		return fabricantRepository.findBycodeOptoCodeFabricant(cOptoCodeFabricant).isPresent();
	}
	
	public FabricantCatalogue getFabricant(String cOptoCodeFabricant) {
		return fabricantRepository
				.findBycodeOptoCodeFabricant(cOptoCodeFabricant)
				.orElse(null);
	}

	/**
	 * Return true if the given cOptoCodeFabricant is present in the FabricantConventionne table
	 *
	 * @param cOptoCodeFabricant
	 * @return
	 */
	public boolean isMakerWithAgreement(String cOptoCodeFabricant) {
		return fabricantConventionneRepository.findBycOptoCodeFabricant(cOptoCodeFabricant).isPresent();
	}

	/**
	 * @param makerProvider
	 * @param creationDate
	 */
	public FabricantCatalogue addMaker(FabricantDistributeur makerProvider, LocalDateTime creationDate) {
		Timestamp creationTimestamp = Timestamp.valueOf(creationDate);
		FabricantCatalogue maker = new FabricantCatalogue(makerProvider.getCOptoCodeFabricant(), makerProvider.getLFabricant(), creationTimestamp.toLocalDateTime(), creationTimestamp.toLocalDateTime());
		return fabricantRepository.save(maker);
	}
}