-
Notifications
You must be signed in to change notification settings - Fork 2
/
Member.java
123 lines (106 loc) · 3.92 KB
/
Member.java
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
package com.amorgakco.backend.member.domain;
import com.amorgakco.backend.global.BaseTime;
import com.amorgakco.backend.global.exception.IllegalAccessException;
import com.amorgakco.backend.global.exception.IllegalFormatException;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Version;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Member extends BaseTime {
private static final String HTTPS_GITHUB_PREFIX = "https://github";
private static final String GITHUB_PREFIX = "github";
private static final Integer MAX_MOGAKCO_TEMPERATURE = 100;
private static final Integer MIN_MOGAKCO_TEMPERATURE = -100;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Enumerated(EnumType.STRING)
private Oauth2ProviderType oauth2ProviderType;
private String oauth2Id;
private String imgUrl;
private String nickname;
private Integer moGakCoTemperature;
private String phoneNumber;
private String githubUrl;
private boolean smsNotificationSetting;
private String cellToken;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private final List<Roles> roleNames = new ArrayList<>();
@Version
private Integer version;
@Builder
public Member(
final Oauth2ProviderType oauth2ProviderType,
final String oauth2Id,
final String imgUrl,
final String nickname) {
this.oauth2ProviderType = oauth2ProviderType;
this.oauth2Id = oauth2Id;
this.imgUrl = imgUrl;
this.nickname = nickname;
this.roleNames.add(new Roles(Role.ROLE_MEMBER));
this.moGakCoTemperature = 0;
this.smsNotificationSetting = false;
}
public void updateNicknameAndImgUrl(final String nickname, final String imgUrl) {
this.nickname = nickname;
this.imgUrl = imgUrl;
}
public void validateAndUpdateAdditionalInfo(
final String githubUrl,
final String phoneNumber,
final boolean setting,
final String cellToken) {
validateGithubUrl(githubUrl);
validatePhoneNumber(phoneNumber);
this.githubUrl = githubUrl;
this.phoneNumber = phoneNumber;
this.smsNotificationSetting = setting;
this.cellToken = cellToken;
}
private void validateGithubUrl(final String githubUrl) {
if (!githubUrl.startsWith(GITHUB_PREFIX) && !githubUrl.startsWith(HTTPS_GITHUB_PREFIX)) {
throw IllegalFormatException.invalidGithubUrl();
}
}
private void validatePhoneNumber(final String phoneNumber) {
if (phoneNumber.contains("-")) {
throw IllegalFormatException.dashNotAllowed();
}
}
public Integer increaseMoGakCoTemperature() {
if (moGakCoTemperature + 1 > MAX_MOGAKCO_TEMPERATURE) {
throw IllegalAccessException.canNotExceedPositive100();
}
return ++moGakCoTemperature;
}
public Integer decreaseMoGakCoTemperature() {
if (moGakCoTemperature - 1 < MIN_MOGAKCO_TEMPERATURE) {
throw IllegalAccessException.canNotUnderNegative100();
}
return --moGakCoTemperature;
}
public boolean isEquals(final Long memberId) {
return this.id.equals(memberId);
}
public boolean isSmsNotificationActivated() {
return smsNotificationSetting;
}
}