Bugfix:
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

- Trips brauchten weitere Annotation um zyklische Abhängigkeit zu verhindern
- Anpassen FriendCode länge und generierung
This commit is contained in:
2026-02-16 15:50:10 +01:00
parent 3051bda1e4
commit 2c0dfaf77b
5 changed files with 41 additions and 17 deletions
@@ -7,6 +7,7 @@ import SuMoC.backend.trip.models.Trip;
import SuMoC.backend.user.enums.RoleName;
import SuMoC.backend.user.enums.VisibilityPrivacy;
import SuMoC.backend.user.stats.StatsList;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import lombok.Setter;
@@ -43,10 +44,11 @@ public class User {
@Column(nullable = false)
private VisibilityPrivacy visibilityPrivacy;
@Column(nullable = false, unique = true, length = 36)
@Column(nullable = false, unique = true, length = 24)
private String friendCode;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference("user-trips")
private Set<Trip> trips = new HashSet<>();
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
@@ -98,7 +100,7 @@ public class User {
this.email = email;
this.password = password;
this.visibilityPrivacy = VisibilityPrivacy.NONE;
this.friendCode = java.util.UUID.randomUUID().toString(); // generate unique friend code
this.friendCode = generateFriendCode(); // generate unique friend code
this.routingPreferences = RoutingPreferences.getDefaultPreferences(); // Init with default routing preferences
this.roles.add(RoleName.USER);
this.statsList = new StatsList(this);
@@ -110,7 +112,7 @@ public class User {
@PrePersist
private void ensureFriendCode() {
if (this.friendCode == null) {
this.friendCode = UUID.randomUUID().toString();
this.friendCode = generateFriendCode();
}
}
@@ -229,4 +231,25 @@ public class User {
public void setProfileImage(ProfileImageEntity profileImage) {
this.profileImage = profileImage;
}
// Friendcode generator
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static final java.security.SecureRandom RANDOM = new java.security.SecureRandom();
private String generateFriendCode() {
StringBuilder sb = new StringBuilder();
for (int group = 0; group < 5; group++) {
for (int i = 0; i < 4; i++) {
sb.append(CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length())));
}
if (group < 4) {
sb.append("-");
}
}
return sb.toString();
}
}