feat: finished creating services from controllers, continued implementing entrepreneurServiceApi with some validation
Some checks failed
Format / formatting (push) Failing after 6s
CI / build (push) Successful in 11s

This commit is contained in:
Théo Le Lez 2025-02-26 17:35:52 +01:00
parent e75a5c9d2c
commit f9de5ed6bf
11 changed files with 219 additions and 125 deletions

View File

@ -79,4 +79,8 @@ public class SectionCell {
public void setModificationDate(LocalDateTime modificationDate) {
this.modificationDate = modificationDate;
}
public Project getProjectSectionCell() {
return projectSectionCell;
}
}

View File

@ -6,4 +6,4 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource
public interface ReportRepository extends JpaRepository<Report, Integer> {}
public interface ReportRepository extends JpaRepository<Report, Long> {}

View File

@ -1,10 +1,16 @@
package enseirb.myinpulse.service;
import enseirb.myinpulse.model.Entrepreneur;
import enseirb.myinpulse.model.Project;
import enseirb.myinpulse.model.SectionCell;
import enseirb.myinpulse.model.User;
import enseirb.myinpulse.service.database.EntrepreneurService;
import enseirb.myinpulse.service.database.ProjectService;
import enseirb.myinpulse.service.database.SectionCellService;
import enseirb.myinpulse.service.database.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
@ -13,13 +19,31 @@ import org.springframework.web.server.ResponseStatusException;
@Service
public class EntrepreneurApiService {
protected static final Logger logger = LogManager.getLogger();
private final SectionCellService sectionCellService;
private final ProjectService projectService;
private final UserService userService;
private final EntrepreneurService entrepreneurService;
@Autowired
EntrepreneurApiService(SectionCellService sectionCellService, ProjectService projectService) {
EntrepreneurApiService(
SectionCellService sectionCellService,
ProjectService projectService,
UserService userService,
EntrepreneurService entrepreneurService) {
this.sectionCellService = sectionCellService;
this.projectService = projectService;
this.userService = userService;
this.entrepreneurService = entrepreneurService;
}
// Create utils file ?
Boolean isAllowedToCheckProject(String mail, long projectId) {
User user = this.userService.getUserByEmail(mail);
Entrepreneur entrepreneur = this.entrepreneurService.getEntrepreneurById(user.getIdUser());
Project project = this.projectService.getProjectById(projectId);
return entrepreneur.getProjectParticipation() == project;
}
public void editSectionCell(Long sectionCellId, SectionCell sectionCell, String mail) {
@ -29,6 +53,20 @@ public class EntrepreneurApiService {
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas");
}
if (!isAllowedToCheckProject(mail, this.sectionCellService.getProjectId(sectionCellId))) {
logger.warn(
"User {} tried to edit section cells {} of the project {} but is not allowed to.",
mail,
sectionCellId,
this.sectionCellService.getProjectId(sectionCellId));
throw new ResponseStatusException(
HttpStatus.UNAUTHORIZED, "You're not allowed to check this project");
}
logger.info(
"User {} edited section cell {} of the project with id {}",
mail,
sectionCellId,
this.sectionCellService.getProjectId(sectionCellId));
sectionCellService.updateSectionCell(
sectionCellId,
sectionCell.getSectionId(),
@ -43,6 +81,20 @@ public class EntrepreneurApiService {
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas");
}
if (!isAllowedToCheckProject(mail, this.sectionCellService.getProjectId(sectionCellId))) {
logger.warn(
"User {} tried to remove section cells {} of the project {} but is not allowed to.",
mail,
sectionCellId,
this.sectionCellService.getSectionCellById(sectionCellId));
throw new ResponseStatusException(
HttpStatus.UNAUTHORIZED, "You're not allowed to check this project");
}
logger.info(
"User {} removed section cell {} of the project with id {}",
mail,
sectionCellId,
this.sectionCellService.getProjectId(sectionCellId));
sectionCellService.removeSectionCellById(sectionCellId);
}
@ -52,14 +104,29 @@ public class EntrepreneurApiService {
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST, "La cellule de section fournie est vide");
}
if (!isAllowedToCheckProject(
mail, this.sectionCellService.getProjectId(sectionCell.getIdSectionCell()))) {
logger.warn(
"User {} tried to add a section cell to the project {} but is not allowed to.",
mail,
this.sectionCellService.getProjectId(sectionCell.getIdSectionCell()));
throw new ResponseStatusException(
HttpStatus.UNAUTHORIZED, "You're not allowed to check this project");
}
logger.info(
"User {} added a new section cell {} to the project with id {}",
mail,
sectionCell.getIdSectionCell(),
this.sectionCellService.getProjectId(sectionCell.getIdSectionCell()));
sectionCellService.addNewSectionCell(sectionCell);
}
public void requestNewProject(Project project, String mail) {
if (project == null) {
System.err.println("Trying to request the creation of a null project");
logger.error("Trying to request the creation of a null project");
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Le projet fourni est vide");
}
logger.info("User {} created a new project with id {}", mail, project.getIdProject());
project.setProjectStatus("PENDING");
projectService.addNewProject(project);
}

View File

@ -3,6 +3,8 @@ package enseirb.myinpulse.service.database;
import enseirb.myinpulse.model.Annotation;
import enseirb.myinpulse.repository.AnnotationRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
@ -13,6 +15,8 @@ import java.util.Optional;
@Service
public class AnnotationService {
protected static final Logger logger = LogManager.getLogger();
private final AnnotationRepository annotationRepository;
@Autowired
@ -27,6 +31,7 @@ public class AnnotationService {
public Annotation getAnnotationById(Long id) {
Optional<Annotation> annotation = annotationRepository.findById(id);
if (annotation.isEmpty()) {
logger.error("getAnnotationById : No annotation found with id {}", id);
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "Cette annotation n'existe pas");
}
@ -44,7 +49,9 @@ public class AnnotationService {
public Annotation updateAnnotation(Long id, String comment) {
Optional<Annotation> annotation = annotationRepository.findById(id);
if (annotation.isEmpty()) {
return null;
logger.error("updateAnnotation : No annotation found with id {}", id);
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "Cette annotation n'existe pas");
}
if (comment != null) {
annotation.get().setComment(comment);

View File

@ -1,69 +1,79 @@
package enseirb.myinpulse.service.database.old_controllers_to_convert_to_services;
import enseirb.myinpulse.model.Appointment;
import enseirb.myinpulse.repository.AppointmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Optional;
@RestController
public class AppointmentController {
@Autowired AppointmentRepository appointmentRepository;
@GetMapping("/Appointment")
@ResponseBody
public Iterable<Appointment> allAppointments() {
return this.appointmentRepository.findAll();
}
@GetMapping("/Appointment/{id}")
public Appointment getAppointmentById(@PathVariable Long id) {
Optional<Appointment> appointment = this.appointmentRepository.findById(id);
if (appointment.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Ce rendez vous n'existe pas");
}
return appointment.get();
}
@PostMapping("/Appointment")
public Appointment addAppointment(@RequestBody Appointment appointment) {
return this.appointmentRepository.save(appointment);
}
@PostMapping("/Appointment/{id}")
public Appointment updateAppointment(
@PathVariable Long id,
LocalDate appointmentDate,
LocalTime appointmentTime,
LocalTime appointmentDuration,
String appointmentPlace,
String appointmentSubject) {
Optional<Appointment> appointment = this.appointmentRepository.findById(id);
if (appointment.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Ce rendez vous n'existe pas");
}
if (appointmentDate != null) {
appointment.get().setAppointmentDate(appointmentDate);
}
if (appointmentTime != null) {
appointment.get().setAppointmentTime(appointmentTime);
}
if (appointmentDuration != null) {
appointment.get().setAppointmentDuration(appointmentDuration);
}
if (appointmentPlace != null) {
appointment.get().setAppointmentPlace(appointmentPlace);
}
if (appointmentSubject != null) {
appointment.get().setAppointmentSubject(appointmentSubject);
}
return this.appointmentRepository.save(appointment.get());
}
}
package enseirb.myinpulse.service.database;
import enseirb.myinpulse.model.Appointment;
import enseirb.myinpulse.repository.AppointmentRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Optional;
@Service
public class AppointmentService {
private static final Logger logger = LogManager.getLogger(AppointmentService.class);
private AppointmentRepository appointmentRepository;
@Autowired
AppointmentService(AppointmentRepository appointmentRepository) {
this.appointmentRepository = appointmentRepository;
}
public Iterable<Appointment> getallAppointments() {
return this.appointmentRepository.findAll();
}
public Appointment getAppointmentById(Long id) {
Optional<Appointment> appointment = this.appointmentRepository.findById(id);
if (appointment.isEmpty()) {
logger.error("getAppointmentById : No appointment found with id {}", id);
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Ce rendez vous n'existe pas");
}
return appointment.get();
}
public Appointment addNewAppointment(Appointment appointment) {
return this.appointmentRepository.save(appointment);
}
public void deleteAppointmentById(Long id) {
this.appointmentRepository.deleteById(id);
}
public Appointment updateAppointment(
Long id,
LocalDate appointmentDate,
LocalTime appointmentTime,
LocalTime appointmentDuration,
String appointmentPlace,
String appointmentSubject) {
Optional<Appointment> appointment = this.appointmentRepository.findById(id);
if (appointment.isEmpty()) {
logger.error("updateAppointment : No appointment found with id {}", id);
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Ce rendez vous n'existe pas");
}
if (appointmentDate != null) {
appointment.get().setAppointmentDate(appointmentDate);
}
if (appointmentTime != null) {
appointment.get().setAppointmentTime(appointmentTime);
}
if (appointmentDuration != null) {
appointment.get().setAppointmentDuration(appointmentDuration);
}
if (appointmentPlace != null) {
appointment.get().setAppointmentPlace(appointmentPlace);
}
if (appointmentSubject != null) {
appointment.get().setAppointmentSubject(appointmentSubject);
}
return this.appointmentRepository.save(appointment.get());
}
}

View File

@ -4,9 +4,10 @@ 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.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.util.Optional;
@ -14,6 +15,8 @@ import java.util.Optional;
@Service
public class EntrepreneurService {
protected static final Logger logger = LogManager.getLogger();
private final EntrepreneurRepository entrepreneurRepository;
EntrepreneurService(EntrepreneurRepository entrepreneurRepository) {
@ -27,6 +30,7 @@ public class EntrepreneurService {
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");
}
@ -41,6 +45,7 @@ public class EntrepreneurService {
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");
}

View File

@ -34,7 +34,7 @@ public class ProjectService {
public Project getProjectById(Long id) {
Optional<Project> project = this.projectRepository.findById(id);
if (project.isEmpty()) {
System.err.println("Project with id " + id + " not found");
logger.error("No project found with id {}", id);
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Ce projet n'existe pas");
}
return project.get();
@ -77,7 +77,7 @@ public class ProjectService {
if (projectStatus != null) {
if (!validateStatus(projectStatus)) {
System.err.println("updateProjectStatus: Invalid status " + projectStatus);
logger.error("updateProjectStatus: Invalid status {}", projectStatus);
throw new ResponseStatusException(
HttpStatus.NOT_ACCEPTABLE, "Ce status n'est pas accepté");
}

View File

@ -3,6 +3,8 @@ package enseirb.myinpulse.service.database;
import enseirb.myinpulse.model.Report;
import enseirb.myinpulse.repository.ReportRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
@ -12,6 +14,9 @@ import java.util.Optional;
@Service
public class ReportService {
protected static final Logger logger = LogManager.getLogger();
private final ReportRepository reportRepository;
@Autowired
@ -19,16 +24,37 @@ public class ReportService {
this.reportRepository = reportRepository;
}
Report getReportById(int id) {
Optional<Report> report = reportRepository.findById(id);
public Iterable<Report> getAllReports() {
return this.reportRepository.findAll();
}
public Report getReportById(Long id) {
Optional<Report> report = this.reportRepository.findById(id);
if (report.isEmpty()) {
logger.error("getReportById : No report found with id {}", id);
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Ce compte rendu n'existe pas");
}
return report.get();
}
// TODO: do some validation
void saveReport(Report report) {
reportRepository.save(report);
public Report addNewReport(Report report) {
return this.reportRepository.save(report);
}
public void deleteReportById(Long id) {
this.reportRepository.deleteById(id);
}
public Report updateReport(Long id, String reportContent) {
Optional<Report> report = this.reportRepository.findById(id);
if (report.isEmpty()) {
logger.error("updateReport : No report found with id {}", id);
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Ce compte rendu n'existe pas");
}
if (reportContent != null) {
report.get().setReportContent(reportContent);
}
return this.reportRepository.save(report.get());
}
}

View File

@ -4,6 +4,8 @@ import enseirb.myinpulse.model.Project;
import enseirb.myinpulse.model.SectionCell;
import enseirb.myinpulse.repository.SectionCellRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
@ -15,6 +17,8 @@ import java.util.Optional;
@Service
public class SectionCellService {
protected static final Logger logger = LogManager.getLogger();
private final SectionCellRepository sectionCellRepository;
@Autowired
@ -29,6 +33,7 @@ public class SectionCellService {
public SectionCell getSectionCellById(Long id) {
Optional<SectionCell> sectionCell = this.sectionCellRepository.findById(id);
if (sectionCell.isEmpty()) {
logger.error("getSectionCellById : No sectionCell found with id {}", id);
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas");
}
@ -47,6 +52,7 @@ public class SectionCellService {
Long id, Long sectionId, String contentSectionCell, LocalDateTime modificationDate) {
Optional<SectionCell> sectionCell = this.sectionCellRepository.findById(id);
if (sectionCell.isEmpty()) {
logger.error("updateSectionCell : No sectionCell found with id {}", id);
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas");
}
@ -65,4 +71,10 @@ public class SectionCellService {
public Iterable<SectionCell> getSectionCellsByProject(Project project, Long sectionId) {
return this.sectionCellRepository.findByProjectSectionCellAndSectionId(project, sectionId);
}
public Long getProjectId(Long sectionCellId) {
SectionCell sectionCell = getSectionCellById(sectionCellId);
Project sectionProject = sectionCell.getProjectSectionCell();
return sectionProject.getIdProject();
}
}

View File

@ -3,6 +3,8 @@ package enseirb.myinpulse.service.database;
import enseirb.myinpulse.model.User;
import enseirb.myinpulse.repository.UserRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
@ -14,6 +16,9 @@ import java.util.Optional;
@Service
public class UserService {
protected static final Logger logger = LogManager.getLogger();
private final UserRepository userRepository;
@Autowired
@ -30,8 +35,8 @@ public class UserService {
Optional<User> opt_user = this.userRepository.findByPrimaryMail(email);
if (opt_user.isEmpty()) {
System.err.println("Couldn't find user with email " + email);
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
logger.error("getUserByEmail : No user found with email {}", email);
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Cet utilisateur n'existe pas");
}
return opt_user.get();
}
@ -53,6 +58,7 @@ public class UserService {
String phoneNumber) {
Optional<User> user = userRepository.findById(id);
if (user.isEmpty()) {
logger.error("updateUser : No user found with id {}", id);
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Cet utilisateur n'existe pas");
}
if (userName != null) {

View File

@ -1,43 +0,0 @@
package enseirb.myinpulse.service.database.old_controllers_to_convert_to_services;
import org.springframework.web.bind.annotation.*;
@RestController
public class ReportController {
/*
private final ReportRepository reportRepository;
@Autowired
public ReportController(ReportRepository reportRepository) {
this.reportRepository = reportRepository;
}
@GetMapping("/Report")
@ResponseBody
public Iterable<Report> allReports() {
System.out.println("\n\n");
System.out.println(ReportRepository);
System.out.println("\n\n");
return this.reportRepository.findAll();
}
@PostMapping("/Report")
public Report addReport(@RequestBody Report report) {
return this.reportRepository.save(report);
}
@PostMapping("/Report/{id}")
public Report updateProject(@PathVariable Long id, String reportContent) {
Optional<Report> report = this.reportRepository.findById(id);
if (report.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Ce compte rendu n'existe pas");
}
if (reportContent != null) {
report.get().setReportContent(reportContent);
}
return this.reportRepository.save(report.get());
}
*/
}