package enseirb.myinpulse.service.database; import enseirb.myinpulse.model.Entrepreneur; import enseirb.myinpulse.model.Project; import enseirb.myinpulse.repository.EntrepreneurRepository; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; import java.util.Optional; @Service public class EntrepreneurService { protected static final Logger logger = LogManager.getLogger(); private final EntrepreneurRepository entrepreneurRepository; EntrepreneurService(EntrepreneurRepository entrepreneurRepository) { this.entrepreneurRepository = entrepreneurRepository; } public Iterable getAllEntrepreneurs() { return this.entrepreneurRepository.findAll(); } public Entrepreneur getEntrepreneurById(Long id) { Optional entrepreneur = entrepreneurRepository.findById(id); if (entrepreneur.isEmpty()) { logger.error("getEntrepreneurById : No entrepreneur found with id {}", id); throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Cet entrepreneur n'existe pas"); } return entrepreneur.get(); } public Entrepreneur addEntrepreneur(Entrepreneur entrepreneur) { return this.entrepreneurRepository.save(entrepreneur); } public Entrepreneur updateEntrepreneur( Long id, String school, String course, Boolean sneeStatus) { Optional entrepreneur = entrepreneurRepository.findById(id); if (entrepreneur.isEmpty()) { logger.error("updateEntrepreneur : No entrepreneur found with id {}", id); throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Cet entrepreneur n'existe pas"); } if (school != null) { entrepreneur.get().setSchool(school); } if (course != null) { entrepreneur.get().setCourse(course); } if (sneeStatus != null) { entrepreneur.get().setSneeStatus(sneeStatus); } return this.entrepreneurRepository.save(entrepreneur.get()); } public Iterable GetEntrepreneurByProject(Project project) { return this.entrepreneurRepository.getEntrepreneurByProjectParticipation(project); } }