From 02fcaf19eb94bea3c0b68c3dbce589ce0763bc77 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 1 Aug 2024 23:50:37 +0100 Subject: [PATCH] api: don't return the user early --- .../java/cc/fascinated/services/UserService.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/API/src/main/java/cc/fascinated/services/UserService.java b/API/src/main/java/cc/fascinated/services/UserService.java index 357b572..809271b 100644 --- a/API/src/main/java/cc/fascinated/services/UserService.java +++ b/API/src/main/java/cc/fascinated/services/UserService.java @@ -58,13 +58,16 @@ public class UserService { throw new BadRequestException("Invalid steam id"); } Optional userOptional = this.userRepository.findBySteamId(steamId); + User user; + boolean shouldUpdate = false; if (userOptional.isEmpty()) { // todo: check the steam API to see if the user exists - User user = new User(UUID.randomUUID()); + user = new User(UUID.randomUUID()); user.setSteamId(steamId); - return userRepository.save(user); + shouldUpdate = true; + } else { + user = userOptional.get(); } - User user = userOptional.get(); // Ensure the users ScoreSaber account is up-to-date ScoreSaberAccount scoresaberAccount = user.getScoresaberAccount(); @@ -76,9 +79,11 @@ public class UserService { ScoreSaberAccountToken accountToken = scoreSaberService.getAccount(user); user.setScoresaberAccount(ScoreSaberAccount.getFromToken(accountToken)); // Update the ScoreSaber account user.setUsername(accountToken.getName()); // Update the username + shouldUpdate = true; + } + if (shouldUpdate) { this.saveUser(user); // Save the user } - return user; }