List all Taskgroups of a User

This commit is contained in:
Sebastian 2023-09-25 11:45:33 +02:00
parent ad407fb525
commit e1b44cb92c
3 changed files with 19 additions and 0 deletions

View File

@ -14,6 +14,7 @@ import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.List;
@CrossOrigin(origins = "*", maxAge = 3600) @CrossOrigin(origins = "*", maxAge = 3600)
@RestController @RestController
@ -70,4 +71,11 @@ public class TaskgroupController {
taskgroupService.deleteTaskgroup(taskgroupPermissionResult.getResult()); taskgroupService.deleteTaskgroup(taskgroupPermissionResult.getResult());
return ResponseEntity.ok(new SimpleStatusResponse("success")); return ResponseEntity.ok(new SimpleStatusResponse("success"));
} }
@GetMapping("/taskgroups")
public ResponseEntity<List<TaskgroupEntityInfo>> listTaskgroupsOfUser() {
List<Taskgroup> taskgroups = taskgroupService.getTaskgroupsByUser(SecurityContextHolder.getContext().getAuthentication().getName());
List<TaskgroupEntityInfo> taskgroupEntityInfos = taskgroups.stream().map(TaskgroupEntityInfo::new).toList();
return ResponseEntity.ok(taskgroupEntityInfos);
}
} }

View File

@ -2,11 +2,17 @@ package core.repositories.timemanager;
import core.entities.User; import core.entities.User;
import core.entities.timemanager.Taskgroup; import core.entities.timemanager.Taskgroup;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
@Repository @Repository
public interface TaskgroupRepository extends CrudRepository<Taskgroup, Long> { public interface TaskgroupRepository extends CrudRepository<Taskgroup, Long> {
boolean existsByTaskgroupNameAndUser(String name, User user); boolean existsByTaskgroupNameAndUser(String name, User user);
@Query("SELECT tg FROM Taskgroup tg WHERE tg.user.username = ?1")
List<Taskgroup> findAllByUser(String username);
} }

View File

@ -12,6 +12,7 @@ import org.springframework.scheduling.config.Task;
import org.springframework.security.core.parameters.P; import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Optional; import java.util.Optional;
@ -66,4 +67,8 @@ public class TaskgroupService {
public void deleteTaskgroup(Taskgroup taskgroup) { public void deleteTaskgroup(Taskgroup taskgroup) {
taskgroupRepository.delete(taskgroup); taskgroupRepository.delete(taskgroup);
} }
public List<Taskgroup> getTaskgroupsByUser(String username) {
return taskgroupRepository.findAllByUser(username);
}
} }