Files
sumoc-backend/src/main/java/SuMoC/backend/user/models/User.java
T
laaser 8c5fff5a3b implement gamification badges system with UserBadge and Badge models, and BadgeService for badge management
# Conflicts:
#	.gitignore
#	src/main/java/SuMoC/backend/user/models/User.java
2026-01-06 17:18:24 +01:00

180 lines
4.9 KiB
Java

package SuMoC.backend.user.models;
import SuMoC.backend.gamification.badges.models.UserBadge;
import SuMoC.backend.trip.models.Trip;
import SuMoC.backend.user.enums.RoleName;
import SuMoC.backend.user.enums.VisibilityPrivacy;
import jakarta.persistence.*;
import java.util.*;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/**
* The public name of the user, displayed in public leaderboards etc.
*/
@Column(unique = true, nullable = false, length = 50)
private String publicName;
/**
* The private name of the user, used in private leaderboards, shown to friends etc.
*/
@Column(nullable = false)
private String privateName;
@Column(unique = true, nullable = false)
private String email;
@Column(nullable = false)
private String password;
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private VisibilityPrivacy visibilityPrivacy;
@Column(nullable = false)
private String friendCode;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Trip> trips = new HashSet<>();
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Friendship> friendships = new HashSet<>();
@ElementCollection(targetClass = RoleName.class, fetch = FetchType.EAGER)
@Enumerated(EnumType.STRING)
@CollectionTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"))
@Column(name = "role")
private Set<RoleName> roles;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<UserBadge> badges = new ArrayList<>();
@ElementCollection
@CollectionTable(name = "user_stats", joinColumns = @JoinColumn(name = "user_id"))
@MapKeyColumn(name = "stat_name")
@Column(name = "stat_value")
private Map<String, Integer> stats = new HashMap<>();
public User() {}
// public User(Long id, String publicName, String privateName, String email, String password, VisibilityPrivacy visibilityPrivacy, String friendCode, Set<Trip> trips, Set<Friendship> friendships, Set<RoleName> roles) {
// this.id = id;
// this.publicName = publicName;
// this.privateName = privateName;
// this.email = email;
// this.password = password;
// this.visibilityPrivacy = visibilityPrivacy;
// this.friendCode = friendCode;
// this.trips = trips;
// this.friendships = friendships;
// this.roles = roles;
// }
public User(String publicName, String privateName, String email, String password) {
this.publicName = publicName;
this.privateName = privateName;
this.email = email;
this.password = password;
this.visibilityPrivacy = VisibilityPrivacy.NONE;
this.friendCode = java.util.UUID.randomUUID().toString(); // generate unique friend code
this.roles = new HashSet<>();
this.roles.add(RoleName.USER);
this.badges = new ArrayList<>();
this.stats = new HashMap<>();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getPublicName() {
return publicName;
}
public void setPublicName(String publicName) {
this.publicName = publicName;
}
public String getPrivateName() {
return privateName;
}
public void setPrivateName(String privateName) {
this.privateName = privateName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public VisibilityPrivacy getVisibilityPrivacy() {
return visibilityPrivacy;
}
public void setVisibilityPrivacy(VisibilityPrivacy visibilityPrivacy) {
this.visibilityPrivacy = visibilityPrivacy;
}
public String getFriendCode() {
return friendCode;
}
public void setFriendCode(String friendCode) {
this.friendCode = friendCode;
}
public Set<Trip> getTrips() {
return trips;
}
public void setTrips(Set<Trip> trips) {
this.trips = trips;
}
public Set<Friendship> getFriendships() {
return friendships;
}
public void setFriendships(Set<Friendship> friendships) {
this.friendships = friendships;
}
public Set<RoleName> getRoles() {
return roles;
}
public void setRoles(Set<RoleName> roles) {
this.roles = roles;
}
public List<UserBadge> getBadges() { return badges; }
public void setBadges(List<UserBadge> badges) { this.badges = badges; }
public Map<String, Integer> getStats() { return stats;}
public void setStats(Map<String, Integer> stats) { this.stats = stats; }
}