package enseirb.myinpulse.service.database; import enseirb.myinpulse.model.Entrepreneur; import enseirb.myinpulse.model.MakeAppointment; 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 void updateEntrepreneurSchool(long idEntrepreneur, String school) { Entrepreneur entrepreneur = getEntrepreneurById(idEntrepreneur); entrepreneur.setSchool(school); this.entrepreneurRepository.save(entrepreneur); } public void updateEntrepreneurCourse(long idEntrepreneur, String course) { Entrepreneur entrepreneur = getEntrepreneurById(idEntrepreneur); entrepreneur.setCourse(course); this.entrepreneurRepository.save(entrepreneur); } public void updateEntrepreneurSneeStatus(long idEntrepreneur, boolean status) { Entrepreneur entrepreneur = getEntrepreneurById(idEntrepreneur); entrepreneur.setSneeStatus(status); this.entrepreneurRepository.save(entrepreneur); } public void updateEntrepreneurProjectParticipation( long idEntrepreneur, Project projectParticipation) { Entrepreneur entrepreneur = getEntrepreneurById(idEntrepreneur); entrepreneur.setProjectParticipation(projectParticipation); this.entrepreneurRepository.save(entrepreneur); } public void updateEntrepreneurProjectProposed(long idEntrepreneur, Project projectProposed) { Entrepreneur entrepreneur = getEntrepreneurById(idEntrepreneur); entrepreneur.setProjectParticipation(projectProposed); this.entrepreneurRepository.save(entrepreneur); } public void updateEntrepreneurMakeAppointment( long idEntrepreneur, MakeAppointment makeAppointment) { Entrepreneur entrepreneur = getEntrepreneurById(idEntrepreneur); entrepreneur.setMakeAppointment(makeAppointment); this.entrepreneurRepository.save(entrepreneur); } public Entrepreneur updateEntrepreneur( Long id, String school, String course, Boolean sneeStatus, Project projectParticipation, Project projectProposed, MakeAppointment makeAppointment) { 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); } if (projectParticipation != null) { entrepreneur.get().setProjectParticipation(projectParticipation); } if (projectProposed != null) { entrepreneur.get().setProjectParticipation(projectProposed); } if (makeAppointment != null) { entrepreneur.get().setMakeAppointment(makeAppointment); } return this.entrepreneurRepository.save(entrepreneur.get()); } public Iterable GetEntrepreneurByProject(Project project) { return this.entrepreneurRepository.getEntrepreneurByProjectParticipation(project); } public void deleteEntrepreneur(Entrepreneur e) { this.entrepreneurRepository.delete(e); } public void validateEntrepreneurById(Long id) { Optional e = this.entrepreneurRepository.findById(id); if (e.isEmpty()) { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Entrepreneur n'existe pas"); } e.get().setPending(false); this.entrepreneurRepository.save(e.get()); } }