Théo Le Lez f9de5ed6bf
Some checks failed
Format / formatting (push) Failing after 6s
CI / build (push) Successful in 11s
feat: finished creating services from controllers, continued implementing entrepreneurServiceApi with some validation
2025-02-26 17:35:52 +01:00

68 lines
2.4 KiB
Java

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<Entrepreneur> getAllEntrepreneurs() {
return this.entrepreneurRepository.findAll();
}
public Entrepreneur getEntrepreneurById(Long id) {
Optional<Entrepreneur> 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> 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<Entrepreneur> GetEntrepreneurByProject(Project project) {
return this.entrepreneurRepository.getEntrepreneurByProjectParticipation(project);
}
}