feat: implemented most of the backend api for administrator
This commit is contained in:
		| @@ -26,8 +26,8 @@ public class AdminApi { | ||||
|      * @return a list of all project managed by the current admin user | ||||
|      */ | ||||
|     @GetMapping("/admin/projects") | ||||
|     public Iterable<Administrator> getProjects() { | ||||
|         return adminApiService.getProjects(); | ||||
|     public Iterable<Project> getProjects(@AuthenticationPrincipal Jwt principal) { | ||||
|         return adminApiService.getProjectsOfAdmin(principal.getClaimAsString("email")); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -53,7 +53,7 @@ public class AdminApi { | ||||
|     /** | ||||
|      * Endpoint used to make a decision about a project. | ||||
|      * | ||||
|      * <p>The decision must contains the administrator | ||||
|      * <p>The decision must contain the administrator | ||||
|      * | ||||
|      * @return the status code of the request | ||||
|      */ | ||||
| @@ -96,7 +96,7 @@ public class AdminApi { | ||||
|      * @return the status code of the request | ||||
|      */ | ||||
|     @DeleteMapping("/admin/projects/remove/{projectId}") | ||||
|     public void deleteProject(@PathVariable String projectId) { | ||||
|     public void deleteProject(@PathVariable long projectId) { | ||||
|         adminApiService.deleteProject(projectId); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -11,34 +11,26 @@ import java.util.List; | ||||
| @Table(name = "project") | ||||
| public class Project { | ||||
|  | ||||
|     @OneToMany(mappedBy = "projectParticipation", fetch = FetchType.LAZY, orphanRemoval = true) | ||||
|     private final List<Entrepreneur> listEntrepreneurParticipation = new ArrayList<>(); | ||||
|     @OneToMany(mappedBy = "projectSectionCell", fetch = FetchType.LAZY, orphanRemoval = true) | ||||
|     private final List<SectionCell> listSectionCell = new ArrayList<>(); | ||||
|     @Id | ||||
|     @NotNull | ||||
|     @GeneratedValue(strategy = GenerationType.IDENTITY) | ||||
|     private Long idProject; | ||||
|  | ||||
|     @Column(length = 255) | ||||
|     private String projectName; | ||||
|  | ||||
|     private byte[] logo; | ||||
|  | ||||
|     private LocalDate creationDate; | ||||
|  | ||||
|     @Column(length = 255) | ||||
|     private String projectStatus; | ||||
|  | ||||
|     @ManyToOne(fetch = FetchType.LAZY) | ||||
|     @JoinColumn(name = "idAdministrator") | ||||
|     private Administrator projectAdministrator; | ||||
|  | ||||
|     @OneToMany(mappedBy = "projectParticipation", fetch = FetchType.LAZY, orphanRemoval = true) | ||||
|     private List<Entrepreneur> listEntrepreneurParticipation = new ArrayList<>(); | ||||
|  | ||||
|     @OneToOne(mappedBy = "projectProposed", fetch = FetchType.LAZY, orphanRemoval = true) | ||||
|     private Entrepreneur entrepreneurProposed; | ||||
|  | ||||
|     @OneToMany(mappedBy = "projectSectionCell", fetch = FetchType.LAZY, orphanRemoval = true) | ||||
|     private List<SectionCell> listSectionCell = new ArrayList<>(); | ||||
|  | ||||
|     public Project() {} | ||||
|  | ||||
|     public Project( | ||||
| @@ -93,4 +85,8 @@ public class Project { | ||||
|     public void setProjectStatus(String projectStatus) { | ||||
|         this.projectStatus = projectStatus; | ||||
|     } | ||||
|  | ||||
|     public void setAdministrator(Administrator administrator) { | ||||
|         this.projectAdministrator = administrator; | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| package enseirb.myinpulse.model; | ||||
|  | ||||
| public class ProjectDecision { | ||||
|     int projectId; | ||||
|     int adminId; | ||||
|     int isAccepted; | ||||
|     public long projectId; | ||||
|     public long adminId; | ||||
|     public long isAccepted; | ||||
| } | ||||
|   | ||||
| @@ -20,7 +20,7 @@ public class User { | ||||
|     private String userName; | ||||
|  | ||||
|     @Column(length = 255) | ||||
|     private String mainMail; | ||||
|     private String primaryMail; | ||||
|  | ||||
|     @Column(length = 255) | ||||
|     private String secondaryMail; | ||||
| @@ -34,13 +34,13 @@ public class User { | ||||
|             Long idUser, | ||||
|             String userSurname, | ||||
|             String userName, | ||||
|             String mainMail, | ||||
|             String primaryMail, | ||||
|             String secondaryMail, | ||||
|             String phoneNumber) { | ||||
|         this.idUser = idUser; | ||||
|         this.userSurname = userSurname; | ||||
|         this.userName = userName; | ||||
|         this.mainMail = mainMail; | ||||
|         this.primaryMail = primaryMail; | ||||
|         this.secondaryMail = secondaryMail; | ||||
|         this.phoneNumber = phoneNumber; | ||||
|     } | ||||
| @@ -69,12 +69,12 @@ public class User { | ||||
|         userName = userName; | ||||
|     } | ||||
|  | ||||
|     public String getMainMail() { | ||||
|         return mainMail; | ||||
|     public String getPrimaryMail() { | ||||
|         return primaryMail; | ||||
|     } | ||||
|  | ||||
|     public void setMainMail(String mainMail) { | ||||
|         this.mainMail = mainMail; | ||||
|     public void setPrimaryMail(String mainMail) { | ||||
|         this.primaryMail = mainMail; | ||||
|     } | ||||
|  | ||||
|     public String getSecondaryMail() { | ||||
|   | ||||
| @@ -1,9 +1,14 @@ | ||||
| package enseirb.myinpulse.repository; | ||||
|  | ||||
| import enseirb.myinpulse.model.Administrator; | ||||
| import enseirb.myinpulse.model.Project; | ||||
|  | ||||
| import org.springframework.data.jpa.repository.JpaRepository; | ||||
| import org.springframework.data.rest.core.annotation.RepositoryRestResource; | ||||
|  | ||||
| @RepositoryRestResource | ||||
| public interface ProjectRepository extends JpaRepository<Project, Long> {} | ||||
| public interface ProjectRepository extends JpaRepository<Project, Long> { | ||||
|     Iterable<Project> findByProjectAdministrator(Administrator administrator); | ||||
|  | ||||
|     Iterable<Project> findByProjectStatus(String status); | ||||
| } | ||||
|   | ||||
| @@ -5,8 +5,11 @@ import enseirb.myinpulse.model.User; | ||||
| import org.springframework.data.jpa.repository.JpaRepository; | ||||
| import org.springframework.data.rest.core.annotation.RepositoryRestResource; | ||||
|  | ||||
| import java.util.Optional; | ||||
|  | ||||
| @RepositoryRestResource | ||||
| public interface UserRepository extends JpaRepository<User, Long> { | ||||
|     Optional<User> findByPrimaryMail(String email); | ||||
|  | ||||
|     /* @Query("SELECT u from User u") | ||||
|     User findAllUser(); */ | ||||
|   | ||||
| @@ -1,16 +1,37 @@ | ||||
| package enseirb.myinpulse.service; | ||||
|  | ||||
| import enseirb.myinpulse.model.*; | ||||
| import enseirb.myinpulse.service.database.AdministratorService; | ||||
| import enseirb.myinpulse.service.database.ProjectService; | ||||
| import enseirb.myinpulse.service.database.UserService; | ||||
|  | ||||
| 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 AdminApiService { | ||||
|     // TODO | ||||
|     public Iterable<Administrator> getProjects() { | ||||
|         throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet"); | ||||
|  | ||||
|     private final ProjectService projectService; | ||||
|     private final UserService userService; | ||||
|     private final AdministratorService administratorService; | ||||
|  | ||||
|     @Autowired | ||||
|     AdminApiService( | ||||
|             ProjectService projectService, | ||||
|             UserService userService, | ||||
|             AdministratorService administratorService) { | ||||
|         this.projectService = projectService; | ||||
|         this.userService = userService; | ||||
|         this.administratorService = administratorService; | ||||
|     } | ||||
|  | ||||
|     // TODO: test | ||||
|     public Iterable<Project> getProjectsOfAdmin(String email) { | ||||
|         return projectService.getProjectsByAdminId( | ||||
|                 administratorService.getAdministratorById( | ||||
|                         this.userService.getIdUserByEmail(email))); | ||||
|     } | ||||
|  | ||||
|     // TODO | ||||
| @@ -18,19 +39,25 @@ public class AdminApiService { | ||||
|         throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet"); | ||||
|     } | ||||
|  | ||||
|     // TODO | ||||
|     // TODO: test | ||||
|     public Iterable<Project> getPendingProjects() { | ||||
|         throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet"); | ||||
|         return this.projectService.getPendingProjects(); | ||||
|     } | ||||
|  | ||||
|     // TODO | ||||
|     // TODO: test | ||||
|     public void validateProject(ProjectDecision decision) { | ||||
|         throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet"); | ||||
|         projectService.updateProject( | ||||
|                 decision.projectId, | ||||
|                 null, | ||||
|                 null, | ||||
|                 null, | ||||
|                 "ACTIVE", | ||||
|                 this.administratorService.getAdministratorById(decision.projectId)); | ||||
|     } | ||||
|  | ||||
|     // TODO | ||||
|     // TODO: solve todo + test | ||||
|     public void addNewProject(Project project) { | ||||
|         throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet"); | ||||
|         projectService.addNewProject(project); // TODO: how can the user know the ID ? | ||||
|     } | ||||
|  | ||||
|     // TODO | ||||
| @@ -38,8 +65,8 @@ public class AdminApiService { | ||||
|         throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet"); | ||||
|     } | ||||
|  | ||||
|     // TODO | ||||
|     public void deleteProject(String projectId) { | ||||
|         throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet"); | ||||
|     // TODO: test | ||||
|     public void deleteProject(long projectId) { | ||||
|         this.projectService.deleteProjectById(projectId); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,4 +1,43 @@ | ||||
| package enseirb.myinpulse.service.database; | ||||
|  | ||||
| import enseirb.myinpulse.model.Administrator; | ||||
| import enseirb.myinpulse.repository.AdministratorRepository; | ||||
|  | ||||
| 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; | ||||
|  | ||||
| import java.util.Optional; | ||||
|  | ||||
| @Service | ||||
| public class AdministratorService { | ||||
|     protected static final Logger logger = LogManager.getLogger(); | ||||
|  | ||||
|     private final AdministratorRepository administratorRepository; | ||||
|  | ||||
|     @Autowired | ||||
|     AdministratorService(AdministratorRepository administratorRepository) { | ||||
|         this.administratorRepository = administratorRepository; | ||||
|     } | ||||
|  | ||||
|     public Iterable<Administrator> allAdministrators() { | ||||
|         return this.administratorRepository.findAll(); | ||||
|     } | ||||
|  | ||||
|     public Administrator getAdministratorById(long id) { | ||||
|         Optional<Administrator> administrator = this.administratorRepository.findById(id); | ||||
|         if (administrator.isEmpty()) { | ||||
|             logger.error("No administrator found with id {}", id); | ||||
|             throw new ResponseStatusException( | ||||
|                     HttpStatus.NOT_FOUND, "Cet administrateur n'existe pas"); | ||||
|         } | ||||
|         return administrator.get(); | ||||
|     } | ||||
|  | ||||
|     public Administrator addAdministrator(Administrator administrator) { | ||||
|         return this.administratorRepository.save(administrator); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,19 +1,25 @@ | ||||
| package enseirb.myinpulse.service.database; | ||||
|  | ||||
| import enseirb.myinpulse.model.Administrator; | ||||
| import enseirb.myinpulse.model.Project; | ||||
| import enseirb.myinpulse.repository.ProjectRepository; | ||||
|  | ||||
| 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; | ||||
|  | ||||
| import java.time.LocalDate; | ||||
| import java.util.List; | ||||
| import java.util.Optional; | ||||
|  | ||||
| @Service | ||||
| public class ProjectService { | ||||
|  | ||||
|     protected static final Logger logger = LogManager.getLogger(); | ||||
|  | ||||
|     private final ProjectRepository projectRepository; | ||||
|  | ||||
|     @Autowired | ||||
| @@ -25,15 +31,19 @@ public class ProjectService { | ||||
|         return this.projectRepository.findAll(); | ||||
|     } | ||||
|  | ||||
|     // TODO: change error | ||||
|     public Project getProjectById(Long id) { | ||||
|         Optional<Project> project = this.projectRepository.findById(id); | ||||
|         if (project.isEmpty()) { | ||||
|             System.err.println("Project with id " + id + " not found"); | ||||
|             throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Ce projet n'existe pas"); | ||||
|         } | ||||
|         return project.get(); | ||||
|     } | ||||
|  | ||||
|     public Iterable<Project> getProjectsByAdminId(Administrator administrator) { | ||||
|         return this.projectRepository.findByProjectAdministrator(administrator); | ||||
|     } | ||||
|  | ||||
|     // TODO: validation | ||||
|     public Project addNewProject(Project project) { | ||||
|         return this.projectRepository.save(project); | ||||
| @@ -44,23 +54,52 @@ public class ProjectService { | ||||
|             String projectName, | ||||
|             byte[] logo, | ||||
|             LocalDate creationDate, | ||||
|             String projectStatus) { | ||||
|             String projectStatus, | ||||
|             Administrator administrator) { | ||||
|         Optional<Project> project = this.projectRepository.findById(id); | ||||
|  | ||||
|         if (project.isEmpty()) { | ||||
|             logger.error("Project with id {} not found.", id); | ||||
|             throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Ce projet n'existe pas"); | ||||
|         } | ||||
|  | ||||
|         if (projectName != null) { | ||||
|             project.get().setProjectName(projectName); | ||||
|         } | ||||
|  | ||||
|         if (logo != null) { | ||||
|             project.get().setLogo(logo); | ||||
|         } | ||||
|  | ||||
|         if (creationDate != null) { | ||||
|             project.get().setCreationDate(creationDate); | ||||
|         } | ||||
|  | ||||
|         if (projectStatus != null) { | ||||
|             if (!validateStatus(projectStatus)) { | ||||
|                 System.err.println("updateProjectStatus: Invalid status " + projectStatus); | ||||
|                 throw new ResponseStatusException( | ||||
|                         HttpStatus.NOT_ACCEPTABLE, "Ce status n'est pas accepté"); | ||||
|             } | ||||
|             project.get().setProjectStatus(projectStatus); | ||||
|         } | ||||
|  | ||||
|         if (administrator != null) { | ||||
|             project.get().setAdministrator(administrator); | ||||
|         } | ||||
|  | ||||
|         return this.projectRepository.save(project.get()); | ||||
|     } | ||||
|  | ||||
|     public Boolean validateStatus(String status) { | ||||
|         return List.of("PENDING", "ACTIVE", "ENDED").contains(status); | ||||
|     } | ||||
|  | ||||
|     public Iterable<Project> getPendingProjects() { | ||||
|         return this.projectRepository.findByProjectStatus("PENDING"); | ||||
|     } | ||||
|  | ||||
|     public void deleteProjectById(Long id) { | ||||
|         this.projectRepository.deleteById(id); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,4 +1,76 @@ | ||||
| package enseirb.myinpulse.service.database; | ||||
|  | ||||
| import enseirb.myinpulse.model.User; | ||||
| import enseirb.myinpulse.repository.UserRepository; | ||||
|  | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.http.HttpStatus; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.springframework.web.bind.annotation.PathVariable; | ||||
| import org.springframework.web.bind.annotation.RequestBody; | ||||
| import org.springframework.web.server.ResponseStatusException; | ||||
|  | ||||
| import java.util.Optional; | ||||
|  | ||||
| @Service | ||||
| public class UserService { | ||||
|     private final UserRepository userRepository; | ||||
|  | ||||
|     @Autowired | ||||
|     UserService(UserRepository userRepository) { | ||||
|         this.userRepository = userRepository; | ||||
|     } | ||||
|  | ||||
|     public Iterable<User> getAllUsers() { | ||||
|         return this.userRepository.findAll(); | ||||
|     } | ||||
|  | ||||
|     // TODO | ||||
|     public long getIdUserByEmail(String email) { | ||||
|         Optional<User> opt_user = this.userRepository.findByPrimaryMail(email); | ||||
|  | ||||
|         if (opt_user.isEmpty()) { | ||||
|             System.err.println("Couldn't find user with email " + email); | ||||
|             throw new ResponseStatusException(HttpStatus.NOT_FOUND); | ||||
|         } | ||||
|         User user = opt_user.get(); | ||||
|         return user.getIdUser(); | ||||
|     } | ||||
|  | ||||
|     public Iterable<User> allUsers() { | ||||
|         return this.userRepository.findAll(); | ||||
|     } | ||||
|  | ||||
|     public User addUser(@RequestBody User user) { | ||||
|         return this.userRepository.save(user); | ||||
|     } | ||||
|  | ||||
|     public User updateUser( | ||||
|             @PathVariable Long id, | ||||
|             String userSurname, | ||||
|             String userName, | ||||
|             String mainMail, | ||||
|             String secondaryMail, | ||||
|             String phoneNumber) { | ||||
|         Optional<User> user = userRepository.findById(id); | ||||
|         if (user.isEmpty()) { | ||||
|             throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Cet utilisateur n'existe pas"); | ||||
|         } | ||||
|         if (userName != null) { | ||||
|             user.get().setUserName(userName); | ||||
|         } | ||||
|         if (userSurname != null) { | ||||
|             user.get().setUserSurname(userSurname); | ||||
|         } | ||||
|         if (mainMail != null) { | ||||
|             user.get().setPrimaryMail(mainMail); | ||||
|         } | ||||
|         if (secondaryMail != null) { | ||||
|             user.get().setSecondaryMail(secondaryMail); | ||||
|         } | ||||
|         if (phoneNumber != null) { | ||||
|             user.get().setPhoneNumber(phoneNumber); | ||||
|         } | ||||
|         return this.userRepository.save(user.get()); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -8,7 +8,7 @@ SELECT setval('report_id_report_seq', 1, false); | ||||
| SELECT setval('section_cell_id_section_cell_seq', 1, false); | ||||
| SELECT setval('user_inpulse_id_user_seq', 1, false); | ||||
|  | ||||
| INSERT INTO user_inpulse (user_surname, user_name, main_mail, secondary_mail, phone_number) | ||||
| INSERT INTO user_inpulse (user_surname, user_name, primary_mail, secondary_mail, phone_number) | ||||
| VALUES ('Dupont', 'Dupond', 'super@mail.fr', 'super2@mail.fr', '06 45 72 45 98'), | ||||
|        ('Martin', 'Matin', 'genial@mail.fr', 'genial2@mail.fr', '06 52 14 58 73'), | ||||
|        ('Charvet', 'Lautre', 'mieux@tmail.fr', 'mieux2@tmail.fr', '07 49 82 16 35'), | ||||
|   | ||||
		Reference in New Issue
	
	Block a user