From bf145aed440f42c7da6a97e35af4466fe505c70f Mon Sep 17 00:00:00 2001 From: neviyn Date: Wed, 31 Mar 2021 10:10:33 +0100 Subject: [PATCH] Creation of new projects --- .../uk/co/neviyn/projectplanner/Entities.kt | 7 ++++- .../neviyn/projectplanner/HtmlController.kt | 11 +++++-- .../uk/co/neviyn/projectplanner/Requests.kt | 2 +- src/main/resources/templates/projectlist.html | 30 +++++++++++++++++++ 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/uk/co/neviyn/projectplanner/Entities.kt b/src/main/kotlin/uk/co/neviyn/projectplanner/Entities.kt index 8b87534..3ae7f0f 100644 --- a/src/main/kotlin/uk/co/neviyn/projectplanner/Entities.kt +++ b/src/main/kotlin/uk/co/neviyn/projectplanner/Entities.kt @@ -39,7 +39,12 @@ class Project( @JsonIgnore var events: MutableSet = mutableSetOf(), @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long? = null -) +) { + fun addMember(member: User){ + members.add(member) + member.projects.add(this) + } +} @Entity class Event( diff --git a/src/main/kotlin/uk/co/neviyn/projectplanner/HtmlController.kt b/src/main/kotlin/uk/co/neviyn/projectplanner/HtmlController.kt index 97f584d..37a5671 100644 --- a/src/main/kotlin/uk/co/neviyn/projectplanner/HtmlController.kt +++ b/src/main/kotlin/uk/co/neviyn/projectplanner/HtmlController.kt @@ -70,16 +70,21 @@ class HtmlController @Autowired constructor(val userRepository: UserRepository, } @PostMapping("/newproject") - fun createNewProject(@RequestBody newProject: NewProject){ - val project = Project(title = newProject.name) + @Transactional + fun createNewProject(@ModelAttribute newProject: NewProject, @AuthenticationPrincipal userDetails: CustomUserDetails) : String { + val project = Project(title = newProject.title) + val user = entityManager.merge(userDetails.user) + project.addMember(user) projectRepository.save(project) + return "redirect:/project/" + project.id } @GetMapping("/projects") @Transactional fun listUserProjects(model: Model, @AuthenticationPrincipal userDetails: CustomUserDetails) : String { val user = entityManager.merge(userDetails.user) // Reattach User entity - model.addAttribute("projects", user.projects) + model.addAttribute("projects", user.projects.sortedBy { it.id }) + model.addAttribute("newProject", NewProject("")) return "projectlist" } diff --git a/src/main/kotlin/uk/co/neviyn/projectplanner/Requests.kt b/src/main/kotlin/uk/co/neviyn/projectplanner/Requests.kt index 3ac90a9..2313c73 100644 --- a/src/main/kotlin/uk/co/neviyn/projectplanner/Requests.kt +++ b/src/main/kotlin/uk/co/neviyn/projectplanner/Requests.kt @@ -8,6 +8,6 @@ data class SimpleUser(val id: Long, val username: String) data class DisplayUser(val id: Long, val username: String, val email: String, val password: String, val oldPassword: String) -data class NewProject(val name: String) +data class NewProject(val title: String) data class NewEvent(val title: String, val description: String, val start: LocalDateTime, val end: LocalDateTime) \ No newline at end of file diff --git a/src/main/resources/templates/projectlist.html b/src/main/resources/templates/projectlist.html index f53618f..9a8c18b 100644 --- a/src/main/resources/templates/projectlist.html +++ b/src/main/resources/templates/projectlist.html @@ -23,6 +23,36 @@ +
+
+
+ +
+
+
+ + + \ No newline at end of file