From 8d65e35f8840a884c216f5f3abbac40cf2660e64 Mon Sep 17 00:00:00 2001 From: Lukas Laaser Date: Tue, 10 Feb 2026 20:03:08 +0100 Subject: [PATCH] Bugfix: fix image, badge und challenge loading to lazy load. Set Email to lowercase --- openapi.yaml | 99 ++++++++++--------- .../gamification/badges/BadgeService.java | 2 + .../gamification/badges/models/Badge.java | 2 +- .../challenges/ChallengeService.java | 1 + .../challenges/Models/Challenge.java | 2 +- .../scoretable/LeaderboardService.java | 2 + .../services/AuthenticationService.java | 12 +-- .../user/controller/UserController.java | 6 +- .../java/SuMoC/backend/user/models/User.java | 6 +- .../backend/user/service/UserService.java | 2 + 10 files changed, 74 insertions(+), 60 deletions(-) diff --git a/openapi.yaml b/openapi.yaml index 9e607fb..801a6bb 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -692,7 +692,9 @@ paths: "200": description: Stats were found content: - application/json: {} + application/json: + schema: + $ref: "#/components/schemas/StatsList" "403": description: Not authorized content: @@ -761,7 +763,10 @@ paths: "200": description: XP successfully retrieved content: - application/json: {} + application/json: + schema: + type: integer + format: int64 "403": description: Not authorized content: @@ -788,7 +793,9 @@ paths: "200": description: Profile picture successfully retrieved content: - application/json: {} + application/json: + schema: + $ref: "#/components/schemas/ImageRequestDTO" "304": description: Profile picture not modified since last request content: @@ -1754,11 +1761,11 @@ components: type: boolean walkingRoute: type: boolean + physicalActivity: + type: boolean durationSeconds: type: integer format: int64 - physicalActivity: - type: boolean StepDTO: type: object description: A single step within a suggested alternative route. @@ -1873,6 +1880,47 @@ components: required: - email - password + Stats: + type: object + properties: + km_PUBLIC_TRANSPORT: + type: integer + format: int64 + mobility_SCORE: + type: integer + format: int64 + co2_SAVED: + type: integer + format: int64 + km_CYCLED: + type: integer + format: int64 + km_WALKED: + type: integer + format: int64 + km_DRIVEN: + type: integer + format: int64 + xp: + type: integer + format: int64 + StatsList: + type: object + properties: + id: + type: integer + format: int64 + description: Unique identifier of the StatsList. + example: 1 + totalStats: + $ref: "#/components/schemas/Stats" + description: Total stats of the user. + monthlyStats: + $ref: "#/components/schemas/Stats" + description: Monthly stats of the user. + weeklyStats: + $ref: "#/components/schemas/Stats" + description: Weekly stats of the user. Badge: type: object properties: @@ -2021,47 +2069,6 @@ components: - CAR - MOTORCYCLE - TAXI - Stats: - type: object - properties: - co2_SAVED: - type: integer - format: int64 - km_CYCLED: - type: integer - format: int64 - km_WALKED: - type: integer - format: int64 - km_DRIVEN: - type: integer - format: int64 - xp: - type: integer - format: int64 - km_PUBLIC_TRANSPORT: - type: integer - format: int64 - mobility_SCORE: - type: integer - format: int64 - StatsList: - type: object - properties: - id: - type: integer - format: int64 - description: Unique identifier of the StatsList. - example: 1 - totalStats: - $ref: "#/components/schemas/Stats" - description: Total stats of the user. - monthlyStats: - $ref: "#/components/schemas/Stats" - description: Monthly stats of the user. - weeklyStats: - $ref: "#/components/schemas/Stats" - description: Weekly stats of the user. Trip: type: object properties: diff --git a/src/main/java/SuMoC/backend/gamification/badges/BadgeService.java b/src/main/java/SuMoC/backend/gamification/badges/BadgeService.java index 4b5fdda..dadb5ca 100644 --- a/src/main/java/SuMoC/backend/gamification/badges/BadgeService.java +++ b/src/main/java/SuMoC/backend/gamification/badges/BadgeService.java @@ -10,6 +10,7 @@ import SuMoC.backend.gamification.badges.models.UserBadgeDTO; import SuMoC.backend.user.models.User; import SuMoC.backend.user.service.CurrentUserService; import SuMoC.backend.user.service.PrivacyService; +import jakarta.transaction.Transactional; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; @@ -21,6 +22,7 @@ import java.util.Map; import java.util.Objects; @Service +@Transactional public class BadgeService { private final CurrentUserService currentUser; diff --git a/src/main/java/SuMoC/backend/gamification/badges/models/Badge.java b/src/main/java/SuMoC/backend/gamification/badges/models/Badge.java index b666163..bd2d0e7 100644 --- a/src/main/java/SuMoC/backend/gamification/badges/models/Badge.java +++ b/src/main/java/SuMoC/backend/gamification/badges/models/Badge.java @@ -16,7 +16,7 @@ public class Badge { private String description; private BadgeCategory category; - @OneToOne(mappedBy = "badge", cascade = CascadeType.ALL, orphanRemoval = true) + @OneToOne(mappedBy = "badge", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) private BadgeImageEntity image; @ElementCollection diff --git a/src/main/java/SuMoC/backend/gamification/challenges/ChallengeService.java b/src/main/java/SuMoC/backend/gamification/challenges/ChallengeService.java index 778e19b..d5a392c 100644 --- a/src/main/java/SuMoC/backend/gamification/challenges/ChallengeService.java +++ b/src/main/java/SuMoC/backend/gamification/challenges/ChallengeService.java @@ -18,6 +18,7 @@ import java.util.List; import static java.lang.Math.min; @Service +@Transactional public class ChallengeService { private final CurrentUserService currentUser; diff --git a/src/main/java/SuMoC/backend/gamification/challenges/Models/Challenge.java b/src/main/java/SuMoC/backend/gamification/challenges/Models/Challenge.java index 2c0613d..fa0d6e2 100644 --- a/src/main/java/SuMoC/backend/gamification/challenges/Models/Challenge.java +++ b/src/main/java/SuMoC/backend/gamification/challenges/Models/Challenge.java @@ -17,7 +17,7 @@ public class Challenge { private String description; - @OneToOne(mappedBy = "challenge", cascade = CascadeType.ALL, orphanRemoval = true) + @OneToOne(mappedBy = "challenge", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) private ChallengeImageEntity image; @ElementCollection diff --git a/src/main/java/SuMoC/backend/gamification/scoretable/LeaderboardService.java b/src/main/java/SuMoC/backend/gamification/scoretable/LeaderboardService.java index eb30c49..8e8e233 100644 --- a/src/main/java/SuMoC/backend/gamification/scoretable/LeaderboardService.java +++ b/src/main/java/SuMoC/backend/gamification/scoretable/LeaderboardService.java @@ -9,6 +9,7 @@ import SuMoC.backend.user.service.CurrentUserService; import SuMoC.backend.user.service.FriendshipService; import SuMoC.backend.user.service.PrivacyService; import SuMoC.backend.user.stats.*; +import jakarta.transaction.Transactional; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -20,6 +21,7 @@ import java.util.List; @Service +@Transactional public class LeaderboardService { private final FriendshipService friendshipService; private final StatsListRepository statsListRepository; diff --git a/src/main/java/SuMoC/backend/security/services/AuthenticationService.java b/src/main/java/SuMoC/backend/security/services/AuthenticationService.java index b68c294..f57d573 100644 --- a/src/main/java/SuMoC/backend/security/services/AuthenticationService.java +++ b/src/main/java/SuMoC/backend/security/services/AuthenticationService.java @@ -49,7 +49,7 @@ public class AuthenticationService { public RegistrationResponseDTO registerUser(UserRegistrationDTO registrationDTO) { try { // check if mail is already in use - if (userRepository.existsByEmail(registrationDTO.getEmail())) { + if (userRepository.existsByEmail(registrationDTO.getEmail().toLowerCase())) { return new RegistrationResponseDTO(null, null, RegistrationStatus.EMAIL_EXISTS); } @@ -60,7 +60,7 @@ public class AuthenticationService { // Validate Email String emailRegex = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; - if (!registrationDTO.getEmail().matches(emailRegex)) { + if (!registrationDTO.getEmail().toLowerCase().matches(emailRegex)) { return new RegistrationResponseDTO(null, null, RegistrationStatus.EMAIL_INVALID); } @@ -68,7 +68,7 @@ public class AuthenticationService { String encodedPassword = passwordEncoder.encode(registrationDTO.getPassword()); // create and save user - User user = new User(registrationDTO.getPublicName(), registrationDTO.getPrivateName(), registrationDTO.getEmail(), encodedPassword); + User user = new User(registrationDTO.getPublicName(), registrationDTO.getPrivateName(), registrationDTO.getEmail().toLowerCase(), encodedPassword); // Sicherheitshalber nochmal explizit setzen if (user.getRoles() == null || user.getRoles().isEmpty()) { @@ -78,7 +78,7 @@ public class AuthenticationService { userRepository.save(user); // We generate the login dto to get the token right after registration - LoginResponseDTO loginResponseDTO = login(new UserAuthenticationPairDTO(registrationDTO.getEmail(), registrationDTO.getPassword())); + LoginResponseDTO loginResponseDTO = login(new UserAuthenticationPairDTO(registrationDTO.getEmail().toLowerCase(), registrationDTO.getPassword())); // But now we return RegistrationResponseDTO for better status handling return new RegistrationResponseDTO(loginResponseDTO.getUserDTO(), loginResponseDTO.getJwt(), RegistrationStatus.SUCCESS); @@ -96,10 +96,10 @@ public class AuthenticationService { try { Authentication auth = authenticationManager.authenticate( - new UsernamePasswordAuthenticationToken(authPairDTO.getEmail(), authPairDTO.getPassword()) + new UsernamePasswordAuthenticationToken(authPairDTO.getEmail().toLowerCase(), authPairDTO.getPassword()) ); - User user = userRepository.findByEmail(authPairDTO.getEmail()) + User user = userRepository.findByEmail(authPairDTO.getEmail().toLowerCase()) .orElseThrow(() -> new RuntimeException("User not found")); UserDTO userDTO = userMapper.toUserDTO(user); diff --git a/src/main/java/SuMoC/backend/user/controller/UserController.java b/src/main/java/SuMoC/backend/user/controller/UserController.java index 6c08114..840bfa5 100644 --- a/src/main/java/SuMoC/backend/user/controller/UserController.java +++ b/src/main/java/SuMoC/backend/user/controller/UserController.java @@ -191,7 +191,7 @@ public class UserController { description = "Returns all stats of a user") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Stats were found", - content = @Content(mediaType = "application/json")), + content = @Content(mediaType = "application/json", schema = @Schema(implementation = StatsList.class))), @ApiResponse(responseCode = "403", description = "Not authorized", content = @Content(mediaType = "application/json")) }) @@ -224,7 +224,7 @@ public class UserController { @Operation(summary = "Get the profile Picture of the user.", description = "A user can get their displayed profile picture.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Profile picture successfully retrieved", - content = @Content(mediaType = "application/json")), + content = @Content(mediaType = "application/json", schema = @Schema(implementation = ImageRequestDTO.class))), @ApiResponse(responseCode = "304", description = "Profile picture not modified since last request"), @ApiResponse(responseCode = "403", description = "Only authorized persons may see the profiles picture"), @ApiResponse(responseCode = "404", description = "The user does not exist") @@ -242,7 +242,7 @@ public class UserController { @Operation(summary = "Get the users own xp", description = "A user can get their own xp.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "XP successfully retrieved", - content = @Content(mediaType = "application/json")), + content = @Content(mediaType = "application/json", schema = @Schema(implementation = Long.class))), @ApiResponse(responseCode = "403", description = "Not authorized", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "404", description = "The user does not exist", diff --git a/src/main/java/SuMoC/backend/user/models/User.java b/src/main/java/SuMoC/backend/user/models/User.java index fde40ba..941b2f7 100644 --- a/src/main/java/SuMoC/backend/user/models/User.java +++ b/src/main/java/SuMoC/backend/user/models/User.java @@ -63,17 +63,17 @@ public class User { @JoinColumn(name = "id", referencedColumnName = "user_id") private NotificationToken notificationToken; - @OneToMany(mappedBy = "user", cascade = CascadeType.ALL) + @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY) private Set badges = new HashSet<>(); - @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) + @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) private StatsList statsList; @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "routing_preferences_id", unique = true) private RoutingPreferences routingPreferences; - @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) + @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) private ProfileImageEntity profileImage; public User() {} diff --git a/src/main/java/SuMoC/backend/user/service/UserService.java b/src/main/java/SuMoC/backend/user/service/UserService.java index 0d993c3..02ef363 100644 --- a/src/main/java/SuMoC/backend/user/service/UserService.java +++ b/src/main/java/SuMoC/backend/user/service/UserService.java @@ -141,6 +141,8 @@ public class UserService { User sender = currentUser.get(); System.out.println(sender.getId()); + newEmail = newEmail.toLowerCase(); + if (!sender.getId().equals(id)) { throw new ResponseStatusException( HttpStatus.FORBIDDEN,