Skip to content

Commit

Permalink
Merge pull request #11 from ConnorMarcus/vahid
Browse files Browse the repository at this point in the history
Added form + endpoints to allow for a prof to create a project
  • Loading branch information
va9id authored Nov 5, 2023
2 parents a57b4c8 + 19a8a80 commit e14ae20
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,53 @@
package sysc4806.project.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import sysc4806.project.models.ApplicationUser;
import sysc4806.project.models.Professor;
import sysc4806.project.models.Project;
import sysc4806.project.models.UserDetails;
import sysc4806.project.repositories.ApplicationUserRepository;
import sysc4806.project.repositories.ProjectRepository;
import sysc4806.project.util.AuthenticationHelper;

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

@Controller
public class ProjectController {

@Autowired
private ProjectRepository projectRepository;
@Autowired
private ApplicationUserRepository applicationUserRepository;

@GetMapping(path = "/createProject")
@Secured(PROFESSOR_ROLE)
public String createProjectPage() {
public String showProjectForm(Model model) {
Project project = new Project();
project.setMaxStudents(1);
model.addAttribute("project", project);
return "createProject";
}

@PostMapping(path="/createProject")
@Secured(PROFESSOR_ROLE)
public String createProject(@ModelAttribute("project") Project project) {
Professor professor = (Professor) this.getCurrentUser();
project.setProfessor(professor);
projectRepository.save(project);
return "redirect:/home"; //Need to redirect to project list VIEW --> NOAH
}

private ApplicationUser getCurrentUser() {
UserDetails user = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
return applicationUserRepository.findApplicationUserByEmail(user.getUsername());
}
}
34 changes: 31 additions & 3 deletions src/main/java/sysc4806/project/models/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class Project {

private int maxStudents;

private List<Program> restrictions;

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

Expand All @@ -29,13 +31,14 @@ public class Project {

public Project() {
this.students = new ArrayList<>();
this.restrictions = new ArrayList<>();
}

public Project(String title, String description, int maxStudents, Professor professor) {
this();
this.title= title;
this.description = description;
this.maxStudents = maxStudents;
this.students = new ArrayList<>();
this.professor = professor;
}

Expand Down Expand Up @@ -85,13 +88,21 @@ public void setDescription(String description) {
}

/**
* Get project professor(s)
* Get project professor
* @return Professor
*/
public Professor getProfessors() {
public Professor getProfessor() {
return professor;
}

/**
* Set project professor
* @param professor Professor
*/
public void setProfessor(Professor professor) {
this.professor = professor;
}

/**
* Get maximum number of students assigned to a project
* @return int maximum amount of students on project
Expand All @@ -108,6 +119,23 @@ public void setMaxStudents(int maxStudents) {
this.maxStudents = maxStudents;
}


/**
* Get program restrictions of the project
* @return List of eligible Programs for the project
*/
public List<Program> getRestrictions() {
return restrictions;
}

/**
* Set a list of eligible programs for the project
* @param restrictions List of Programs that are eligible for the project
*/
public void setRestrictions(List<Program> restrictions) {
this.restrictions = restrictions;
}

/**
* Get students assigned to the project
* @return List of students on the project
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package sysc4806.project.repositories;

import org.springframework.data.repository.CrudRepository;
import sysc4806.project.models.Project;

public interface ProjectRepository extends CrudRepository<Project, Long> {

}
58 changes: 58 additions & 0 deletions src/main/resources/static/css/ProjectCreateStyles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
margin: 0;
padding: 0;
}

h1 {
text-align: center;
color: #333;
}

form {
max-width: 500px;
width: 100%;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

label {
display: block;
font-weight: bold;
margin-top: 10px;
color: #333;
}

input[type="text"],
input[type="number"],
select {
width: 100%;
padding: 10px;
margin: 5px 0;
border: 1px solid #ccc;
border-radius: 3px;
box-sizing: border-box;
font-size: 16px;
}

select {
height: auto;
}

button {
background-color: #007bff;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}

button:hover {
background-color: #0056b3;
}
25 changes: 23 additions & 2 deletions src/main/resources/templates/createProject.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<title>Create Project</title>
<link type="text/css" rel="stylesheet" th:href="@{/css/ProjectCreateStyles.css}" />
</head>
<body>
<h1>Create Project</h1>
<h1>Create a New 4th Year Project</h1>
<form action="#" th:action="@{createProject}" th:object="${project}" method="post">

<label for="project-title">Project Title: </label>
<input type="text" id="project-title" required="required" th:field="*{title}"><br/>

<label for="project-description">Project Description: </label>
<input type="text" id="project-description" required="required" th:field="*{description}"><br/>

<label for="project-restrictions">Program Restrictions: </label>
<select id="project-restrictions" multiple="multiple" required="required" th:field="*{restrictions}">
<option th:each="restriction : ${T(sysc4806.project.models.Program).values()}"
th:text="${restriction}" th:value="${restriction}"></option>
</select>

<label for="project-max-students">Required Number of Students: </label>
<input type="number" id="project-max-students" required="required" min="1" max="4" th:field="*{maxStudents}"><br/>

<button type="submit">Create Project</button>
<button type="reset">Clear</button>
</form>
</body>
</html>

0 comments on commit e14ae20

Please sign in to comment.