-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from ConnorMarcus/vahid
Added form + endpoints to allow for a prof to create a project
- Loading branch information
Showing
5 changed files
with
157 additions
and
6 deletions.
There are no files selected for viewing
38 changes: 37 additions & 1 deletion
38
src/main/java/sysc4806/project/controllers/ProjectController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/sysc4806/project/repositories/ProjectRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |