From e1b44cb92c0ad7b5cc888e5c0dc0d95d92706e98 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 25 Sep 2023 11:45:33 +0200 Subject: [PATCH] List all Taskgroups of a User --- .../java/core/api/controller/TaskgroupController.java | 8 ++++++++ .../repositories/timemanager/TaskgroupRepository.java | 6 ++++++ backend/src/main/java/core/services/TaskgroupService.java | 5 +++++ 3 files changed, 19 insertions(+) diff --git a/backend/src/main/java/core/api/controller/TaskgroupController.java b/backend/src/main/java/core/api/controller/TaskgroupController.java index bd53a1e..8841c8b 100644 --- a/backend/src/main/java/core/api/controller/TaskgroupController.java +++ b/backend/src/main/java/core/api/controller/TaskgroupController.java @@ -14,6 +14,7 @@ import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; +import java.util.List; @CrossOrigin(origins = "*", maxAge = 3600) @RestController @@ -70,4 +71,11 @@ public class TaskgroupController { taskgroupService.deleteTaskgroup(taskgroupPermissionResult.getResult()); return ResponseEntity.ok(new SimpleStatusResponse("success")); } + + @GetMapping("/taskgroups") + public ResponseEntity> listTaskgroupsOfUser() { + List taskgroups = taskgroupService.getTaskgroupsByUser(SecurityContextHolder.getContext().getAuthentication().getName()); + List taskgroupEntityInfos = taskgroups.stream().map(TaskgroupEntityInfo::new).toList(); + return ResponseEntity.ok(taskgroupEntityInfos); + } } diff --git a/backend/src/main/java/core/repositories/timemanager/TaskgroupRepository.java b/backend/src/main/java/core/repositories/timemanager/TaskgroupRepository.java index ddcd272..6441145 100644 --- a/backend/src/main/java/core/repositories/timemanager/TaskgroupRepository.java +++ b/backend/src/main/java/core/repositories/timemanager/TaskgroupRepository.java @@ -2,11 +2,17 @@ package core.repositories.timemanager; import core.entities.User; import core.entities.timemanager.Taskgroup; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface TaskgroupRepository extends CrudRepository { boolean existsByTaskgroupNameAndUser(String name, User user); + + @Query("SELECT tg FROM Taskgroup tg WHERE tg.user.username = ?1") + List findAllByUser(String username); } diff --git a/backend/src/main/java/core/services/TaskgroupService.java b/backend/src/main/java/core/services/TaskgroupService.java index 5a224cd..07cd12e 100644 --- a/backend/src/main/java/core/services/TaskgroupService.java +++ b/backend/src/main/java/core/services/TaskgroupService.java @@ -12,6 +12,7 @@ import org.springframework.scheduling.config.Task; import org.springframework.security.core.parameters.P; import org.springframework.stereotype.Service; +import java.util.List; import java.util.NoSuchElementException; import java.util.Optional; @@ -66,4 +67,8 @@ public class TaskgroupService { public void deleteTaskgroup(Taskgroup taskgroup) { taskgroupRepository.delete(taskgroup); } + + public List getTaskgroupsByUser(String username) { + return taskgroupRepository.findAllByUser(username); + } }