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, String content, String mail) { SectionCell sectionCell = sectionCellService.getSectionCellById(sectionCellId); if (sectionCell == 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, content, null, null, null); } 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.getProjectId(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); } }