Skip to content

Commit

Permalink
Added join and leave actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Noahh01 committed Nov 9, 2023
1 parent 891fb12 commit cb1de62
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 46 deletions.
90 changes: 53 additions & 37 deletions src/main/java/sysc4806/project/controllers/ProjectController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import sysc4806.project.models.*;
import sysc4806.project.repositories.ApplicationUserRepository;
import sysc4806.project.repositories.ProjectRepository;
import java.util.ArrayList;
import sysc4806.project.services.ApplicationUserService;
import java.util.List;
import java.util.Objects;
import java.util.Optional;


import static sysc4806.project.util.AuthenticationHelper.*;

Expand All @@ -24,6 +22,9 @@ public class ProjectController {
@Autowired
private ApplicationUserRepository applicationUserRepository;

@Autowired
private ApplicationUserService applicationUserService;

@GetMapping(path = "/createProject")
@Secured(PROFESSOR_ROLE)
public String showProjectForm(Model model) {
Expand All @@ -36,7 +37,7 @@ public String showProjectForm(Model model) {
@PostMapping(path="/createProject")
@Secured(PROFESSOR_ROLE)
public String createProject(@ModelAttribute("project") Project project) {
Professor professor = (Professor) this.getCurrentUser();
Professor professor = (Professor) applicationUserService.getCurrentUser();
project.setProfessor(professor);
projectRepository.save(project);
return "redirect:/viewProjects";
Expand All @@ -46,16 +47,24 @@ public String createProject(@ModelAttribute("project") Project project) {
public String viewProjectsPage(Model model) throws Exception {
List<Project> projects = (List<Project>) projectRepository.findAll();
model.addAttribute("projects", projects);
model.addAttribute("isProfessor", isCurrentUserProfessor());
model.addAttribute("userProjects", getUserProjects());

boolean isProfessor = applicationUserService.isCurrentUserProfessor();
model.addAttribute("isProfessor", isProfessor);

if (isProfessor) {
Professor professor = (Professor) applicationUserService.getCurrentUser();
model.addAttribute("professorUser", professor);
} else {
Student student = (Student) applicationUserService.getCurrentUser();
model.addAttribute("studentUser", student);
}
return "viewProjects";
}

@DeleteMapping( "/deleteProject/{projectID}")
public String deleteProject(@PathVariable Long projectID) {
Optional<Project> projectWithID = projectRepository.findById(projectID);
if (projectWithID.isPresent()) {
Project project = projectWithID.get();
@DeleteMapping( "/deleteProject/{projectId}")
public String deleteProject(@PathVariable Long projectId) {
Project project = projectRepository.findById(projectId).orElse(null);
if (project != null) {
disassociateProject(project);
projectRepository.delete(project);
return "viewProjects";
Expand All @@ -64,36 +73,43 @@ public String deleteProject(@PathVariable Long projectID) {
}
}

private void disassociateProject(Project project) {
for (Student s: project.getStudents()) {
s.setProject(null);
@PatchMapping("/project/{projectId}/addStudent/{userId}")
public String addStudentToProject(@PathVariable Long projectId, @PathVariable Long userId) {
Project project = projectRepository.findById(projectId).orElse(null);
Student student = (Student) applicationUserRepository.findById(userId).orElse(null);
if (project != null && student != null) {
project.addStudent(student);
student.setProject(project);
projectRepository.save(project);
return "viewProjects";
}
project.getProfessor().removeProject(project);
return "error";
}

private ApplicationUser getCurrentUser() {
UserDetails user = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
return applicationUserRepository.findApplicationUserByEmail(user.getUsername());
}

private List<Long> getUserProjects() throws Exception {
List<Long> listOfProjectIDs = new ArrayList<>();
ApplicationUser currentUser = getCurrentUser();

if (!isCurrentUserProfessor()) {
listOfProjectIDs.add(((Student)currentUser).getProject().getId());
}
else {
for (Project project: ((Professor)currentUser).getProjects()) {
listOfProjectIDs.add(project.getId());
}
@PatchMapping("/project/{projectId}/removeStudent/{userId}")
public String removeStudentFromProject(@PathVariable Long projectId, @PathVariable Long userId) {
Project project = projectRepository.findById(projectId).orElse(null);
Student student = (Student) applicationUserRepository.findById(userId).orElse(null);
if (project != null && student != null) {
project.removeStudent(student);
student.setProject(null);
projectRepository.save(project);
return "viewProjects";
}
return listOfProjectIDs;
return "error";
}

private boolean isCurrentUserProfessor() throws Exception {
ApplicationUser currentUser = getCurrentUser();
return Objects.equals(getUserRole(currentUser), PROFESSOR_ROLE);
/**
* Disassociates a project from students and professor
* @param project The project to disassociate
*/
private void disassociateProject(Project project) {
for (Student s: project.getStudents()) {
s.setProject(null);
applicationUserRepository.save(s);
}
Professor professor = project.getProfessor();
professor.removeProject(project);
applicationUserRepository.save(professor);
}

}
8 changes: 4 additions & 4 deletions src/main/java/sysc4806/project/models/Project.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package sysc4806.project.models;

import jakarta.persistence.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
Expand All @@ -23,7 +21,7 @@ public class Project {

private List<Program> restrictions;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "project")
@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER, mappedBy = "project")
private List<Student> students;

@ManyToOne
Expand Down Expand Up @@ -149,7 +147,9 @@ public List<Student> getStudents() {
* @param student Student to be added to the project
*/
public void addStudent(Student student) {
students.add(student);
if (students.size() < maxStudents) {
students.add(student);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package sysc4806.project.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import sysc4806.project.models.*;
import sysc4806.project.repositories.ApplicationUserRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static sysc4806.project.util.AuthenticationHelper.PROFESSOR_ROLE;
import static sysc4806.project.util.AuthenticationHelper.getUserRole;

@Service
public class ApplicationUserService {
@Autowired
ApplicationUserRepository applicationUserRepository;

/**
* Gets the current user
* @return The current application user
*/
public ApplicationUser getCurrentUser() {
UserDetails user = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
return applicationUserRepository.findApplicationUserByEmail(user.getUsername());
}


/**
* Check if the current user is Professor
* @return True if the current user is Professor
*/
public boolean isCurrentUserProfessor() throws Exception {
ApplicationUser currentUser = getCurrentUser();
return Objects.equals(getUserRole(currentUser), PROFESSOR_ROLE);
}
}
13 changes: 8 additions & 5 deletions src/main/resources/templates/viewProjects.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,22 @@ <h1>List of 4th Year Projects:</h1>
<td th:attr="project-id=${project.getId()}">
<button
class="delete"
th:if="${isProfessor && userProjects.contains(project.getId())}">
th:if="${isProfessor && professorUser.getProjects().contains(project)}">
Delete Project
</button>
<button
class="join"
th:attr="user-id=${userID}"
th:if="${!isProfessor && userProjects.isEmpty()}">
th:attr="user-id=${studentUser.getId()}"
th:if="${!isProfessor
&& studentUser.project == null
&& project.getStudents().size() < project.getMaxStudents()
&& project.getRestrictions().contains(studentUser.getProgram())}">
Join Project
</button>
<button
class="leave"
th:attr="user-id=${userID}"
th:if="${!isProfessor && userProjects.contains(project.getId())}">
th:attr="user-id=${studentUser.getId()}"
th:if="${!isProfessor && studentUser.getProject()?.getId() == project.getId()}">
Leave Project
</button>
</td>
Expand Down

0 comments on commit cb1de62

Please sign in to comment.