feat: added most of shared API calls
Some checks failed
Format / formatting (push) Failing after 5s
CI / build (push) Successful in 10s

This commit is contained in:
Pierre Tellier
2025-02-26 15:31:02 +01:00
parent 1a6db7c953
commit 8d4dc7916d
10 changed files with 117 additions and 25 deletions

View File

@ -1,7 +1,14 @@
package enseirb.myinpulse.service;
import enseirb.myinpulse.model.*;
import enseirb.myinpulse.service.database.AdministratorService;
import enseirb.myinpulse.service.database.EntrepreneurService;
import enseirb.myinpulse.service.database.ProjectService;
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;
import org.springframework.web.server.ResponseStatusException;
@ -9,17 +16,80 @@ import org.springframework.web.server.ResponseStatusException;
@Service
public class SharedApiService {
protected static final Logger logger = LogManager.getLogger();
private final AdministratorService administratorService;
private final UserService userService;
private final ProjectService projectService;
private final EntrepreneurService entrepreneurService;
@Autowired
SharedApiService(
AdministratorService administratorService,
UserService userService,
ProjectService projectService,
EntrepreneurService entrepreneurService) {
this.administratorService = administratorService;
this.userService = userService;
this.projectService = projectService;
this.entrepreneurService = entrepreneurService;
}
// TODO: test
Boolean isAnAdmin(String mail) {
try {
long userId = this.userService.getUserByEmail(mail).getIdUser();
Administrator a = this.administratorService.getAdministratorById(userId);
return true;
} catch (ResponseStatusException e) {
logger.info(e);
return false;
}
}
// TODO: test
Boolean isAllowedToCheckProject(String mail, long projectId) {
if (isAnAdmin(mail)) {
return true;
}
User user = this.userService.getUserByEmail(mail);
Entrepreneur entrepreneur = this.entrepreneurService.getEntrepreneurById(user.getIdUser());
Project project = this.projectService.getProjectById(projectId);
return entrepreneur.getProjectParticipation() == project;
}
// TODO
public Iterable<SectionCell> getLCSection(
String projectId, String title, String date, String mail) {
throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet");
}
public Iterable<Entrepreneur> getEntrepreneursByProjectId(int projectId, String mail) {
throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet");
// TODO: test, protect via email
public Iterable<Entrepreneur> getEntrepreneursByProjectId(long projectId, String mail) {
if (!isAllowedToCheckProject(mail, projectId)) {
logger.warn(
"User {} tried to check the member of the project {} but is not allowed to.",
mail,
projectId);
throw new ResponseStatusException(
HttpStatus.UNAUTHORIZED, "You're not allowed to check this project");
}
Project project = this.projectService.getProjectById(projectId);
return this.entrepreneurService.GetEntrepreneurByProject(project);
}
public Iterable<Administrator> getAdminByProjectId(int projectId, String mail) {
throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet");
// TODO: test, protect via email
public Administrator getAdminByProjectId(long projectId, String mail) {
if (!isAllowedToCheckProject(mail, projectId)) {
logger.warn(
"User {} tried to check the admin of the project {} but is not allowed to.",
mail,
projectId);
throw new ResponseStatusException(
HttpStatus.UNAUTHORIZED, "You're not allowed to check this project");
}
Project project = this.projectService.getProjectById(projectId);
return project.getAdministrator();
}
public Iterable<Appointment> getAppointmentsByProjectId(int projectId, String mail) {