diff --git a/backend/src/main/java/de/hft/geotime/GeotimeApplication.java b/backend/src/main/java/de/hft/geotime/GeotimeApplication.java index 859b276..561fec1 100644 --- a/backend/src/main/java/de/hft/geotime/GeotimeApplication.java +++ b/backend/src/main/java/de/hft/geotime/GeotimeApplication.java @@ -4,6 +4,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; +import org.springframework.data.projection.SpelAwareProxyProjectionFactory; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @SpringBootApplication @@ -19,4 +20,9 @@ public class GeotimeApplication { return new BCryptPasswordEncoder(); } + @Bean + public SpelAwareProxyProjectionFactory projectionFactory() { + return new SpelAwareProxyProjectionFactory(); + } + } diff --git a/backend/src/main/java/de/hft/geotime/controllers/UserController.java b/backend/src/main/java/de/hft/geotime/controllers/UserController.java index 1aa5494..3a52cb4 100644 --- a/backend/src/main/java/de/hft/geotime/controllers/UserController.java +++ b/backend/src/main/java/de/hft/geotime/controllers/UserController.java @@ -1,7 +1,9 @@ package de.hft.geotime.controllers; import de.hft.geotime.entities.TimetrackUser; +import de.hft.geotime.entities.projections.UserWithRoleProjection; import de.hft.geotime.repositories.TimetrackUserRepository; +import org.springframework.data.projection.ProjectionFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; @@ -16,16 +18,18 @@ public class UserController { private final TimetrackUserRepository userRepository; private final BCryptPasswordEncoder bCryptPasswordEncoder; + private final ProjectionFactory projectionFactory; - public UserController(TimetrackUserRepository userRepository, BCryptPasswordEncoder bCryptPasswordEncoder) { + public UserController(TimetrackUserRepository userRepository, BCryptPasswordEncoder bCryptPasswordEncoder, ProjectionFactory projectionFactory) { this.userRepository = userRepository; this.bCryptPasswordEncoder = bCryptPasswordEncoder; + this.projectionFactory = projectionFactory; } @GetMapping("/whoami") - public String getUsername(Authentication authentication) { - TimetrackUser timetrackUser = userRepository.findFirstByUsername(authentication.getName()); - return "Welcome back " + timetrackUser.getFirstname() + " " + timetrackUser.getLastname(); + public UserWithRoleProjection getUsername(Authentication authentication) { + TimetrackUser user = userRepository.findFirstByUsername(authentication.getName()); + return projectionFactory.createProjection(UserWithRoleProjection.class, user); } @PostMapping("/sign-up") diff --git a/backend/src/main/java/de/hft/geotime/entities/TimetrackUser.java b/backend/src/main/java/de/hft/geotime/entities/TimetrackUser.java index 4b81905..af6deb5 100644 --- a/backend/src/main/java/de/hft/geotime/entities/TimetrackUser.java +++ b/backend/src/main/java/de/hft/geotime/entities/TimetrackUser.java @@ -1,8 +1,6 @@ package de.hft.geotime.entities; import com.fasterxml.jackson.annotation.JsonIgnore; -import de.hft.geotime.entities.Role; -import de.hft.geotime.entities.TimetrackAccount; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;