feat: finished creating services from controllers, continued implementing entrepreneurServiceApi with some validation
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user