-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClub.java
More file actions
147 lines (132 loc) · 6.53 KB
/
Club.java
File metadata and controls
147 lines (132 loc) · 6.53 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.example.moim.club.entity;
import com.example.moim.club.dto.request.ClubInput;
import com.example.moim.club.dto.request.ClubUpdateInput;
import com.example.moim.club.exception.advice.ClubControllerAdvice;
import com.example.moim.global.entity.BaseEntity;
import com.example.moim.global.enums.*;
import com.example.moim.global.exception.ResponseCode;
import com.example.moim.global.util.file.model.FileInfo;
import com.example.moim.match.entity.Match;
import com.example.moim.statistic.entity.Statistic;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@NoArgsConstructor
public class Club extends BaseEntity {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String explanation;
@Column(length = 500)
private String introduction;
@Enumerated(value = EnumType.STRING)
private ClubCategory clubCategory;
private String university;
@Enumerated(value = EnumType.STRING)
private Gender gender;
@Enumerated(value = EnumType.STRING)
private ActivityArea activityArea;
@Enumerated(value = EnumType.STRING)
private SportsType sportsType;
@Enumerated(value = EnumType.STRING)
private AgeRange ageRange;
private String clubPassword;
private String imgUrl; // 프론트에 제공할 URL
private String storedImgName; // UUID 로 저장된 이름
private String originalImgName; // 파일 업로할 때 이름
private String mainUniformColor;
private String subUniformColor;
@OneToOne(mappedBy = "club") // 검색을 위한 테이블 매핑
private ClubSearch clubSearch;
private Integer memberCount;
private Integer scheduleCount;
private Integer matchCount;
@OneToMany(mappedBy = "club", cascade = CascadeType.REMOVE)
private List<UserClub> userClub = new ArrayList<>();
@OneToMany(mappedBy = "homeClub", cascade = CascadeType.REMOVE)
private List<Match> homeMatches = new ArrayList<>();
@OneToMany(mappedBy = "awayClub", cascade = CascadeType.REMOVE)
private List<Match> awayMatches = new ArrayList<>();
@OneToMany(mappedBy = "club", cascade = CascadeType.REMOVE)
private List<Notice> notices = new ArrayList<>();
/**
* TODO : university 는 없을 수도 있으므로, null 일 경우를 처리해주기
*/
public static Club from(ClubInput clubInput, FileInfo fileInfo) {
Club club = new Club();
club.title = clubInput.getTitle();
club.explanation = clubInput.getExplanation();
club.introduction = clubInput.getIntroduction();
club.clubCategory = ClubCategory.fromKoreanName(clubInput.getClubCategory()).orElseThrow(() -> new ClubControllerAdvice(ResponseCode.INVALID_CLUB_CATEGORY));
club.university = clubInput.getUniversity();
club.gender = Gender.fromKoreanName(clubInput.getGender()).orElseThrow(() -> new ClubControllerAdvice(ResponseCode.INVALID_GENDER));
club.activityArea = ActivityArea.fromKoreanName(clubInput.getActivityArea()).orElseThrow(() -> new ClubControllerAdvice(ResponseCode.INVALID_ACTIVITY_AREA));
club.sportsType = SportsType.fromKoreanName(clubInput.getSportsType()).orElseThrow(() -> new ClubControllerAdvice(ResponseCode.INVALID_SPORTS_TYPE));
club.ageRange = AgeRange.fromKoreanName(clubInput.getAgeRange()).orElseThrow(() -> new ClubControllerAdvice(ResponseCode.INVALID_AGE_RANGE));
club.clubPassword = clubInput.getClubPassword();
if (fileInfo != null) {
club.imgUrl = fileInfo.getFileUrl();
club.originalImgName = fileInfo.getOriginalFileName();
club.storedImgName = fileInfo.getStoredFileName();
}
club.mainUniformColor = clubInput.getMainUniformColor();
club.subUniformColor = clubInput.getSubUniformColor();
club.memberCount = 1;
Statistic.createStatistic(club, SportsType.OVERALL);
Statistic.createStatistic(club, SportsType.FUTSAL);
Statistic.createStatistic(club, SportsType.SOCCER);
return club;
}
public Club updateSearch(ClubSearch clubSearch) {
this.clubSearch = clubSearch;
return this;
}
public void changeProfileImg(String newImgPath) {
this.imgUrl = newImgPath;
}
public void plusMemberCount() {
memberCount++;
}
public void update(ClubUpdateInput clubUpdateInput, FileInfo fileInfo) {
if (StringUtils.hasText(clubUpdateInput.getTitle())) {
this.title = clubUpdateInput.getTitle();
}
if (StringUtils.hasText(clubUpdateInput.getExplanation())) {
this.explanation = clubUpdateInput.getExplanation();
}
if (StringUtils.hasText(clubUpdateInput.getIntroduction())) {
this.introduction = clubUpdateInput.getIntroduction();
}
if (StringUtils.hasText(clubUpdateInput.getClubCategory())) {
this.clubCategory = ClubCategory.fromKoreanName(clubUpdateInput.getClubCategory()).orElseThrow(() -> new ClubControllerAdvice(ResponseCode.INVALID_CLUB_CATEGORY));
}
if (StringUtils.hasText(clubUpdateInput.getOrganization())) {
this.university = clubUpdateInput.getOrganization();
}
if (StringUtils.hasText(clubUpdateInput.getGender())) {
this.gender = Gender.fromKoreanName(clubUpdateInput.getGender()).orElseThrow(() -> new ClubControllerAdvice(ResponseCode.INVALID_GENDER));
}
if (StringUtils.hasText(clubUpdateInput.getActivityArea())) {
this.activityArea = ActivityArea.fromKoreanName(clubUpdateInput.getActivityArea()).orElseThrow(() -> new ClubControllerAdvice(ResponseCode.INVALID_ACTIVITY_AREA));
}
if (StringUtils.hasText(clubUpdateInput.getAgeRange())) {
this.ageRange = AgeRange.fromKoreanName(clubUpdateInput.getAgeRange()).orElseThrow(() -> new ClubControllerAdvice(ResponseCode.INVALID_AGE_RANGE));
}
if (StringUtils.hasText(clubUpdateInput.getSportsType())) {
this.sportsType = SportsType.fromKoreanName(clubUpdateInput.getSportsType()).orElseThrow(() -> new ClubControllerAdvice(ResponseCode.INVALID_SPORTS_TYPE));
}
if (fileInfo != null) {
this.imgUrl = fileInfo.getFileUrl();
this.originalImgName = fileInfo.getOriginalFileName();
this.storedImgName = fileInfo.getStoredFileName();
}
}
public void updatePassword(String newPassword) {
this.clubPassword = newPassword;
}
}