feat: implemented date filtration and a utils service to prevent code ducplication
This commit is contained in:
parent
b5c03798fc
commit
80b2d087e4
@ -1,8 +0,0 @@
|
||||
package enseirb.myinpulse.model;
|
||||
|
||||
public class DelAppointment {
|
||||
int validated;
|
||||
int[] akserId;
|
||||
int[] destId;
|
||||
String date; // TODO: date type ?
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package enseirb.myinpulse.model;
|
||||
|
||||
public class DelProject {
|
||||
int projectId;
|
||||
String projectName;
|
||||
String projectDescription;
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package enseirb.myinpulse.model;
|
||||
|
||||
public class DelReport {
|
||||
int projectId;
|
||||
String reportContent;
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package enseirb.myinpulse.model;
|
||||
|
||||
// TODO: is this redundant with the Section class from the database ?
|
||||
// TODO: In the one hand it represent the same data, and on the other it should be much lighter.
|
||||
// TODO: btw why does a LC section have an administrator ?
|
||||
|
||||
public class LCSection {}
|
@ -6,8 +6,13 @@ import enseirb.myinpulse.model.SectionCell;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@RepositoryRestResource
|
||||
public interface SectionCellRepository extends JpaRepository<SectionCell, Long> {
|
||||
|
||||
Iterable<SectionCell> findByProjectSectionCellAndSectionId(Project project, long sectionId);
|
||||
|
||||
Iterable<SectionCell> findByProjectSectionCellAndSectionIdAndModificationDateBefore(
|
||||
Project project, long sectionId, LocalDateTime date);
|
||||
}
|
||||
|
@ -1,13 +1,9 @@
|
||||
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;
|
||||
@ -23,28 +19,19 @@ public class EntrepreneurApiService {
|
||||
|
||||
private final SectionCellService sectionCellService;
|
||||
private final ProjectService projectService;
|
||||
private final UserService userService;
|
||||
private final EntrepreneurService entrepreneurService;
|
||||
private final UtilsService utilsService;
|
||||
|
||||
@Autowired
|
||||
EntrepreneurApiService(
|
||||
SectionCellService sectionCellService,
|
||||
ProjectService projectService,
|
||||
UserService userService,
|
||||
EntrepreneurService entrepreneurService) {
|
||||
UtilsService utilsService) {
|
||||
this.sectionCellService = sectionCellService;
|
||||
this.projectService = projectService;
|
||||
this.userService = userService;
|
||||
this.entrepreneurService = entrepreneurService;
|
||||
this.utilsService = utilsService;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
SectionCell editSectionCell = sectionCellService.getSectionCellById(sectionCellId);
|
||||
@ -53,7 +40,7 @@ public class EntrepreneurApiService {
|
||||
throw new ResponseStatusException(
|
||||
HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas");
|
||||
}
|
||||
if (!isAllowedToCheckProject(mail, this.sectionCellService.getProjectId(sectionCellId))) {
|
||||
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,
|
||||
@ -81,7 +68,7 @@ public class EntrepreneurApiService {
|
||||
throw new ResponseStatusException(
|
||||
HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas");
|
||||
}
|
||||
if (!isAllowedToCheckProject(mail, this.sectionCellService.getProjectId(sectionCellId))) {
|
||||
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,
|
||||
@ -104,7 +91,7 @@ public class EntrepreneurApiService {
|
||||
throw new ResponseStatusException(
|
||||
HttpStatus.BAD_REQUEST, "La cellule de section fournie est vide");
|
||||
}
|
||||
if (!isAllowedToCheckProject(
|
||||
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.",
|
||||
|
@ -10,6 +10,8 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -18,57 +20,32 @@ 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;
|
||||
private final SectionCellService sectionCellService;
|
||||
private final AppointmentService appointmentService;
|
||||
|
||||
private final UtilsService utilsService;
|
||||
|
||||
@Autowired
|
||||
SharedApiService(
|
||||
AdministratorService administratorService,
|
||||
UserService userService,
|
||||
ProjectService projectService,
|
||||
EntrepreneurService entrepreneurService,
|
||||
SectionCellService sectionCellService,
|
||||
AppointmentService appointmentService) {
|
||||
this.administratorService = administratorService;
|
||||
this.userService = userService;
|
||||
AppointmentService appointmentService,
|
||||
UtilsService utilsService) {
|
||||
this.projectService = projectService;
|
||||
this.entrepreneurService = entrepreneurService;
|
||||
this.sectionCellService = sectionCellService;
|
||||
this.appointmentService = appointmentService;
|
||||
}
|
||||
|
||||
// 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;
|
||||
this.utilsService = utilsService;
|
||||
}
|
||||
|
||||
// TODO filter this with date
|
||||
public Iterable<SectionCell> getSectionCells(
|
||||
long projectId, long sectionId, String date, String mail) {
|
||||
|
||||
if (!isAllowedToCheckProject(mail, projectId)) {
|
||||
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
|
||||
logger.warn(
|
||||
"User {} tried to check section cells of the project {} but is not allowed to.",
|
||||
mail,
|
||||
@ -76,13 +53,18 @@ public class SharedApiService {
|
||||
throw new ResponseStatusException(
|
||||
HttpStatus.UNAUTHORIZED, "You're not allowed to check this project");
|
||||
}
|
||||
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
|
||||
LocalDateTime dateTime = LocalDateTime.parse(date, formatter);
|
||||
|
||||
Project project = this.projectService.getProjectById(projectId);
|
||||
return this.sectionCellService.getSectionCellsByProject(project, sectionId);
|
||||
return this.sectionCellService.getSectionCellsByProjectAndSectionIdBeforeDate(
|
||||
project, sectionId, dateTime);
|
||||
}
|
||||
|
||||
// TODO: test
|
||||
public Iterable<Entrepreneur> getEntrepreneursByProjectId(long projectId, String mail) {
|
||||
if (!isAllowedToCheckProject(mail, projectId)) {
|
||||
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
|
||||
logger.warn(
|
||||
"User {} tried to check the member of the project {} but is not allowed to.",
|
||||
mail,
|
||||
@ -94,9 +76,9 @@ public class SharedApiService {
|
||||
return this.entrepreneurService.GetEntrepreneurByProject(project);
|
||||
}
|
||||
|
||||
// TODO: test, protect via email
|
||||
// TODO: test
|
||||
public Administrator getAdminByProjectId(long projectId, String mail) {
|
||||
if (!isAllowedToCheckProject(mail, projectId)) {
|
||||
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
|
||||
logger.warn(
|
||||
"User {} tried to check the admin of the project {} but is not allowed to.",
|
||||
mail,
|
||||
@ -110,7 +92,7 @@ public class SharedApiService {
|
||||
|
||||
// TODO
|
||||
public Iterable<Appointment> getAppointmentsByProjectId(long projectId, String mail) {
|
||||
if (!isAllowedToCheckProject(mail, projectId)) {
|
||||
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
|
||||
logger.warn(
|
||||
"User {} tried to check the appointments related to the project {} but is not allowed to.",
|
||||
mail,
|
||||
@ -145,7 +127,7 @@ public class SharedApiService {
|
||||
.getFirst()
|
||||
.getProjectSectionCell()
|
||||
.getIdProject();
|
||||
if (!isAllowedToCheckProject(mail, projectId)) {
|
||||
if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
|
||||
logger.warn(
|
||||
"User {} tried to generate the PDF report {} related to the appointment {} but is not allowed to.",
|
||||
mail,
|
||||
|
@ -0,0 +1,62 @@
|
||||
package enseirb.myinpulse.service;
|
||||
|
||||
import enseirb.myinpulse.model.Administrator;
|
||||
import enseirb.myinpulse.model.Entrepreneur;
|
||||
import enseirb.myinpulse.model.Project;
|
||||
import enseirb.myinpulse.model.User;
|
||||
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.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
@Service
|
||||
public class UtilsService {
|
||||
|
||||
protected static final Logger logger = LogManager.getLogger();
|
||||
|
||||
private final UserService userService;
|
||||
private final ProjectService projectService;
|
||||
private final EntrepreneurService entrepreneurService;
|
||||
private final AdministratorService administratorService;
|
||||
|
||||
@Autowired
|
||||
UtilsService(
|
||||
ProjectService projectService,
|
||||
UserService userService,
|
||||
EntrepreneurService entrepreneurService,
|
||||
AdministratorService administratorService) {
|
||||
this.userService = userService;
|
||||
this.projectService = projectService;
|
||||
this.entrepreneurService = entrepreneurService;
|
||||
this.administratorService = administratorService;
|
||||
}
|
||||
|
||||
// TODO: test?
|
||||
public 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: 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -84,4 +84,10 @@ public class SectionCellService {
|
||||
SectionCell sectionCell = getSectionCellById(sectionCellId);
|
||||
return sectionCell.getAppointmentSectionCell();
|
||||
}
|
||||
|
||||
public Iterable<SectionCell> getSectionCellsByProjectAndSectionIdBeforeDate(
|
||||
Project project, long sectionId, LocalDateTime date) {
|
||||
return sectionCellRepository.findByProjectSectionCellAndSectionIdAndModificationDateBefore(
|
||||
project, sectionId, date);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user