Théo Le Lez c94d3654ce
All checks were successful
Format / formatting (push) Successful in 7s
Build / build (push) Successful in 31s
CI / build (push) Successful in 11s
fix: updating foreign keys when adding new entity to the db
2025-03-15 15:23:18 +01:00

136 lines
6.0 KiB
Java

package enseirb.myinpulse.service;
import static enseirb.myinpulse.model.ProjectDecisionValue.PENDING;
import enseirb.myinpulse.model.Project;
import enseirb.myinpulse.model.SectionCell;
import enseirb.myinpulse.service.database.ProjectService;
import enseirb.myinpulse.service.database.SectionCellService;
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;
@Service
public class EntrepreneurApiService {
protected static final Logger logger = LogManager.getLogger();
private final SectionCellService sectionCellService;
private final ProjectService projectService;
private final UtilsService utilsService;
@Autowired
EntrepreneurApiService(
SectionCellService sectionCellService,
ProjectService projectService,
UtilsService utilsService) {
this.sectionCellService = sectionCellService;
this.projectService = projectService;
this.utilsService = utilsService;
}
public void editSectionCell(Long sectionCellId, SectionCell sectionCell, String mail) {
SectionCell editSectionCell = sectionCellService.getSectionCellById(sectionCellId);
if (editSectionCell == null) {
System.err.println("Trying to edit unknown section cell");
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas");
}
if (!utilsService.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(),
sectionCell.getContentSectionCell(),
sectionCell.getModificationDate());
}
public void removeSectionCell(Long sectionCellId, String mail) {
SectionCell editSectionCell = sectionCellService.getSectionCellById(sectionCellId);
if (editSectionCell == null) {
System.err.println("Trying to remove unknown section cell");
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas");
}
if (!utilsService.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);
}
public void addSectionCell(SectionCell sectionCell, String mail) {
if (sectionCell == null) {
System.err.println("Trying to create an empty section cell");
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST, "La cellule de section fournie est vide");
}
if (!utilsService.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()));
SectionCell newSectionCell = sectionCellService.addNewSectionCell(sectionCell);
newSectionCell.getProjectSectionCell().updateListSectionCell(newSectionCell);
newSectionCell
.getAppointmentSectionCell()
.forEach(
appointment -> {
appointment.updateListSectionCell(newSectionCell);
});
newSectionCell
.getListAnnotation()
.forEach(
annotation -> {
annotation.setSectionCellAnnotation(newSectionCell);
});
}
public void requestNewProject(Project project, String mail) {
if (project == null) {
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);
}
}