Repository navigation
Expand file tree
/
Copy pathUser.java
More file actions
126 lines (100 loc) · 2.94 KB
/
Copy pathUser.java
File metadata and controls
126 lines (100 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package codesquad.domain;
import codesquad.UnAuthorizedException;
import codesquad.dto.UserDto;
import com.fasterxml.jackson.annotation.JsonIgnore;
import support.domain.AbstractEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.validation.constraints.Size;
import java.util.Objects;
@Entity
public class User extends AbstractEntity {
public static final GuestUser GUEST_USER = new GuestUser();
@Size(min = 3, max = 20)
@Column(unique = true, nullable = false, length = 20)
private String userId;
@Size(min = 3, max = 20)
@Column(nullable = false, length = 20)
@JsonIgnore
private String password;
@Size(min = 3, max = 20)
@Column(nullable = false, length = 20)
private String name;
public User() {
}
public User(String userId, String password, String name) {
this(0L, userId, password, name);
}
public User(long id, String userId, String password, String name) {
super(id);
this.userId = userId;
this.password = password;
this.name = name;
}
public String getUserId() {
return userId;
}
public User setUserId(String userId) {
this.userId = userId;
return this;
}
public String getPassword() {
return password;
}
public User setPassword(String password) {
this.password = password;
return this;
}
public String getName() {
return name;
}
public User setName(String name) {
this.name = name;
return this;
}
private boolean matchUserId(String userId) {
return this.userId.equals(userId);
}
public void update(User loginUser, User target) {
if (!matchUserId(loginUser.getUserId())) {
throw new UnAuthorizedException();
}
if (!matchPassword(target.getPassword())) {
return;
}
this.name = target.name;
}
public boolean matchPassword(String password) {
return this.password.equals(password);
}
public UserDto _toUserDto() {
return new UserDto(this.userId, this.password, this.name);
}
@JsonIgnore
public boolean isGuestUser() {
return false;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
User user = (User) o;
return Objects.equals(userId, user.userId) &&
Objects.equals(name, user.name);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), userId, name);
}
private static class GuestUser extends User {
@Override
public boolean isGuestUser() {
return true;
}
}
@Override
public String toString() {
return "User [userId=" + userId + ", password=" + password + ", name=" + name + "]";
}
}