11package com .github .throyer .common .springboot .domain .user .entity ;
22
3+ import com .fasterxml .jackson .annotation .JsonIgnore ;
4+ import com .fasterxml .jackson .annotation .JsonProperty ;
5+ import com .github .throyer .common .springboot .domain .management .entity .Auditable ;
6+ import com .github .throyer .common .springboot .domain .management .model .Addressable ;
7+ import com .github .throyer .common .springboot .domain .role .entity .Role ;
8+ import com .github .throyer .common .springboot .domain .session .model .Authorized ;
9+ import com .github .throyer .common .springboot .domain .user .model .CreateUserProps ;
10+ import com .github .throyer .common .springboot .domain .user .model .UpdateUserProps ;
11+ import lombok .Getter ;
12+ import lombok .Setter ;
13+ import org .hibernate .Hibernate ;
14+ import org .hibernate .annotations .Where ;
15+
16+ import javax .persistence .*;
17+ import java .io .Serial ;
318import java .io .Serializable ;
419import java .security .Principal ;
520import java .util .List ;
21+ import java .util .Map ;
622import java .util .Objects ;
723
8- import javax .persistence .CascadeType ;
9- import javax .persistence .Column ;
10- import javax .persistence .Entity ;
11- import javax .persistence .FetchType ;
12- import javax .persistence .GeneratedValue ;
13- import javax .persistence .GenerationType ;
14- import javax .persistence .Id ;
15- import javax .persistence .JoinColumn ;
16- import javax .persistence .JoinTable ;
17- import javax .persistence .ManyToMany ;
18- import javax .persistence .PrePersist ;
19- import javax .persistence .Table ;
20-
21- import com .fasterxml .jackson .annotation .JsonIgnore ;
22- import com .fasterxml .jackson .annotation .JsonProperty ;
23- import com .fasterxml .jackson .annotation .JsonProperty .Access ;
24- import com .github .throyer .common .springboot .domain .user .model .CreateUserProps ;
25-
26- import com .github .throyer .common .springboot .domain .session .model .Authorized ;
27- import com .github .throyer .common .springboot .domain .user .model .UpdateUserProps ;
28- import com .github .throyer .common .springboot .domain .management .entity .Auditable ;
29- import com .github .throyer .common .springboot .domain .role .entity .Role ;
30- import com .github .throyer .common .springboot .domain .management .model .Addressable ;
24+ import static com .fasterxml .jackson .annotation .JsonProperty .Access .WRITE_ONLY ;
3125import static com .github .throyer .common .springboot .domain .management .repository .Queries .NON_DELETED_CLAUSE ;
3226import static com .github .throyer .common .springboot .utils .Constants .SECURITY .PASSWORD_ENCODER ;
27+ import static com .github .throyer .common .springboot .utils .JsonUtils .toJson ;
28+ import static java .util .Optional .ofNullable ;
29+ import static javax .persistence .CascadeType .DETACH ;
30+ import static javax .persistence .FetchType .LAZY ;
31+ import static javax .persistence .GenerationType .IDENTITY ;
3332
34- import lombok .Data ;
35-
36- import org .hibernate .annotations .Where ;
37- import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
38-
39- @ Data
4033@ Entity
34+ @ Getter
35+ @ Setter
4136@ Table (name = "user" )
4237@ Where (clause = NON_DELETED_CLAUSE )
4338public class User extends Auditable implements Serializable , Addressable {
44-
45- public static final Integer PASSWORD_STRENGTH = 10 ;
46-
39+ @ Serial
4740 private static final long serialVersionUID = -8080540494839892473L ;
4841
4942 @ Id
50- @ GeneratedValue (strategy = GenerationType . IDENTITY )
43+ @ GeneratedValue (strategy = IDENTITY )
5144 private Long id ;
5245
5346 @ Column (name = "name" , nullable = false )
@@ -60,11 +53,11 @@ public class User extends Auditable implements Serializable, Addressable {
6053 @ Column (name = "deleted_email" )
6154 private String deletedEmail ;
6255
63- @ JsonProperty (access = Access . WRITE_ONLY )
56+ @ JsonProperty (access = WRITE_ONLY )
6457 @ Column (name = "password" , nullable = false )
6558 private String password ;
6659
67- @ ManyToMany (cascade = CascadeType . DETACH , fetch = FetchType . LAZY )
60+ @ ManyToMany (cascade = DETACH , fetch = LAZY )
6861 @ JoinTable (name = "user_role" ,
6962 joinColumns = {
7063 @ JoinColumn (name = "user_id" )},
@@ -110,38 +103,16 @@ public String getEmail() {
110103 return email ;
111104 }
112105
113- public void merge (UpdateUserProps dto ) {
114- setName ( dto . getName () );
115- setEmail ( dto . getEmail () );
106+ public void merge (UpdateUserProps props ) {
107+ this . name = props . getName ();
108+ this . email = props . getEmail ();
116109 }
117110
118111 public void updatePassword (String newPassword ) {
119- this .password = new BCryptPasswordEncoder ( PASSWORD_STRENGTH )
112+ this .password = PASSWORD_ENCODER
120113 .encode (newPassword );
121114 }
122115
123- @ Override
124- public int hashCode () {
125- int hash = 7 ;
126- hash = 97 * hash + Objects .hashCode (this .id );
127- return hash ;
128- }
129-
130- @ Override
131- public boolean equals (Object obj ) {
132- if (this == obj ) {
133- return true ;
134- }
135- if (obj == null ) {
136- return false ;
137- }
138- if (getClass () != obj .getClass ()) {
139- return false ;
140- }
141- final User other = (User ) obj ;
142- return Objects .equals (this .id , other .id );
143- }
144-
145116 public boolean isPrincipal (Principal principal ) {
146117 if (Objects .nonNull (principal ) && principal instanceof Authorized authorized ) {
147118 return getId ().equals (authorized .getId ());
@@ -167,6 +138,22 @@ private void created() {
167138
168139 @ Override
169140 public String toString () {
170- return Objects .nonNull (getName ()) ? name : "null" ;
141+ return toJson (Map .of (
142+ "name" , ofNullable (this .name ).orElse ("" ),
143+ "email" , ofNullable (this .email ).orElse ("" )
144+ ));
145+ }
146+
147+ @ Override
148+ public boolean equals (Object o ) {
149+ if (this == o ) return true ;
150+ if (o == null || Hibernate .getClass (this ) != Hibernate .getClass (o )) return false ;
151+ User user = (User ) o ;
152+ return id != null && Objects .equals (id , user .id );
153+ }
154+
155+ @ Override
156+ public int hashCode () {
157+ return getClass ().hashCode ();
171158 }
172159}
0 commit comments