diff --git a/src/main/kotlin/uk/co/neviyn/projectplanner/HtmlController.kt b/src/main/kotlin/uk/co/neviyn/projectplanner/HtmlController.kt index 998f82b..2ff1ea0 100644 --- a/src/main/kotlin/uk/co/neviyn/projectplanner/HtmlController.kt +++ b/src/main/kotlin/uk/co/neviyn/projectplanner/HtmlController.kt @@ -12,6 +12,11 @@ import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestParam +import org.springframework.web.bind.annotation.ResponseBody +import java.time.Instant +import java.time.LocalDateTime +import java.time.ZoneOffset +import java.time.format.DateTimeFormatter import javax.persistence.EntityManager import javax.transaction.Transactional @@ -105,19 +110,28 @@ class ProjectController @Autowired constructor(val projectRepository: ProjectRep return "project" } + val f: DateTimeFormatter = DateTimeFormatter.ISO_INSTANT + @GetMapping("/events") @PreAuthorize("hasPermission(#id, 'Long', '')") - fun getProjectEvents(@PathVariable id: Long) : Set { - return projectRepository.findById(id).get().events + @ResponseBody + fun getProjectEventsBetween(@PathVariable id: Long, @RequestParam start: String?, @RequestParam end: String?) : Set { + val startTime: LocalDateTime? = LocalDateTime.ofInstant(Instant.from(f.parse(start)), ZoneOffset.UTC) + val endTime: LocalDateTime? = LocalDateTime.ofInstant(Instant.from(f.parse(end)), ZoneOffset.UTC) + val project = projectRepository.findById(id).get() + if(startTime == null && endTime == null) return project.events + else if(startTime == null && endTime != null) return eventRepository.findByEndBeforeAndProject(endTime, project) + else if(startTime != null && endTime == null) return eventRepository.findByStartAfterAndProject(startTime, project) + return eventRepository.findByStartAfterAndEndBeforeAndProjectIs(startTime!!, endTime!!, project) } + @PostMapping("/adduser") @PreAuthorize("hasPermission(#id, 'Long', '')") fun addUserToProject(@PathVariable id: Long, @RequestParam("uid") uid: Long) : String { val user = userRepository.findById(uid).get() val project = projectRepository.findById(id).get() - project.members.add(user) - user.projects.add(project) + project.addMember(user) projectRepository.save(project) return "redirect:/project/$id" } @@ -141,4 +155,18 @@ class ProjectController @Autowired constructor(val projectRepository: ProjectRep val event = Event(title = e.title, description = e.description, start = e.start, end = e.end, project = project) eventRepository.save(event) } + + @PostMapping("/editevent") + @PreAuthorize("hasPermission(#id, 'Long', '')") + fun editEvent(@PathVariable id: Long) { + TODO() + } + + @PostMapping("/deleteevent") + @PreAuthorize("hasPermission(#id, 'Long', '')") + fun deleteEvent(@PathVariable id: Long) { + TODO() + } + + } \ No newline at end of file diff --git a/src/main/kotlin/uk/co/neviyn/projectplanner/Repositories.kt b/src/main/kotlin/uk/co/neviyn/projectplanner/Repositories.kt index 1313341..51ffc15 100644 --- a/src/main/kotlin/uk/co/neviyn/projectplanner/Repositories.kt +++ b/src/main/kotlin/uk/co/neviyn/projectplanner/Repositories.kt @@ -1,16 +1,20 @@ package uk.co.neviyn.projectplanner import org.springframework.data.repository.CrudRepository +import java.time.LocalDateTime interface UserRepository : CrudRepository{ fun findByUsername(username: String): User? - fun findByUsernameIsLike(partialUsername: String) : List fun findByIdNotIn(ids: List) : List } interface ProjectRepository : CrudRepository{} -interface EventRepository : CrudRepository{} +interface EventRepository : CrudRepository{ + fun findByStartAfterAndProject(start: LocalDateTime, project: Project) : Set + fun findByEndBeforeAndProject(end: LocalDateTime, project: Project) : Set + fun findByStartAfterAndEndBeforeAndProjectIs(start: LocalDateTime, end: LocalDateTime, project: Project) : Set +} interface TagRepository : CrudRepository{} diff --git a/src/main/resources/templates/project.html b/src/main/resources/templates/project.html index e98beb5..7779f93 100644 --- a/src/main/resources/templates/project.html +++ b/src/main/resources/templates/project.html @@ -21,18 +21,15 @@ aspectRatio: 2.2, eventAdd(addInfo) { console.log(addInfo.event) - } + }, + events: window.location.origin + window.location.pathname + "/events" }); calendar.render(); - }; - -