Simplifiy user id

This commit is contained in:
Denis Natusch 2023-11-21 14:51:47 +01:00 committed by Simon Bruder
parent 317af597f5
commit 3ecdcffbcd
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
3 changed files with 7 additions and 62 deletions

View file

@ -4,8 +4,6 @@ import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import org.springframework.data.domain.AbstractAggregateRoot;
@Entity @Entity
public class Staff { public class Staff {

View file

@ -1,23 +1,18 @@
package catering.users; package catering.users;
import jakarta.persistence.Embeddable;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne; import jakarta.persistence.OneToOne;
import org.salespointframework.core.AbstractAggregateRoot;
import org.salespointframework.useraccount.Role; import org.salespointframework.useraccount.Role;
import org.salespointframework.useraccount.UserAccount; import org.salespointframework.useraccount.UserAccount;
import org.salespointframework.core.AbstractAggregateRoot;
import catering.users.User.UserIdentifier;
import java.io.Serializable;
import java.util.UUID;
import org.jmolecules.ddd.types.Identifier;
@Entity @Entity
public class User extends AbstractAggregateRoot<UserIdentifier> { public class User {
private @EmbeddedId UserIdentifier id = new UserIdentifier(); private @Id @GeneratedValue Long id;
private String address, fullName; private String address, fullName;
@OneToOne @OneToOne
@ -65,8 +60,7 @@ public class User extends AbstractAggregateRoot<UserIdentifier> {
return this.userAccount; return this.userAccount;
} }
@Override public Long getId() {
public UserIdentifier getId() {
return id; return id;
} }
@ -77,50 +71,4 @@ public class User extends AbstractAggregateRoot<UserIdentifier> {
public boolean hasRole(String role) { public boolean hasRole(String role) {
return userAccount.getRoles().stream().toList().contains(Role.of(role)); return userAccount.getRoles().stream().toList().contains(Role.of(role));
} }
@Embeddable
public static final class UserIdentifier implements Identifier, Serializable {
private static final long serialVersionUID = 7740660930809051850L;
private final UUID identifier;
UserIdentifier() {
this(UUID.randomUUID());
}
UserIdentifier(UUID identifier) {
this.identifier = identifier;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (identifier == null ? 0 : identifier.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof UserIdentifier that)) {
return false;
}
return this.identifier.equals(that.identifier);
}
@Override
public String toString(){
return identifier.toString();
}
}
} }

View file

@ -1,10 +1,9 @@
package catering.users; package catering.users;
import catering.users.User.UserIdentifier;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.data.util.Streamable; import org.springframework.data.util.Streamable;
interface UserRepository extends CrudRepository<User, UserIdentifier> { interface UserRepository extends CrudRepository<User, Long> {
@Override @Override
Streamable<User> findAll(); Streamable<User> findAll();