-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatch.java
More file actions
171 lines (140 loc) · 5.45 KB
/
Match.java
File metadata and controls
171 lines (140 loc) · 5.45 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.example.moim.match.entity;
import com.example.moim.global.enums.AgeRange;
import com.example.moim.global.enums.Gender;
import com.example.moim.global.enums.SportsType;
import com.example.moim.global.exception.ResponseCode;
import com.example.moim.match.exception.advice.MatchControllerAdvice;
import com.example.moim.schedule.dto.ScheduleInput;
import com.example.moim.club.entity.Club;
import com.example.moim.schedule.entity.Schedule;
import com.example.moim.match.exception.MatchRecordExpireException;
import com.example.moim.global.entity.BaseEntity;
import com.example.moim.match.dto.MatchInput;
import com.example.moim.match.dto.MatchRegInput;
import jakarta.persistence.*;
import lombok.Getter;
import java.time.LocalDateTime;
import java.util.Objects;
import static com.example.moim.match.entity.MatchHalf.*;
import static com.example.moim.match.entity.MatchStatus.*;
@Entity
@Getter
@Table(name = "matches")
public class Match extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "match_id")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "home_club_id")
private Club homeClub;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "away_club_id")
private Club awayClub;
private int homeScore;
private int awayScore;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "schedule_id")
private Schedule schedule;
private String name;
@Enumerated(EnumType.STRING)
private SportsType event;
@Enumerated(EnumType.STRING)
private MatchSize matchSize;
private LocalDateTime startTime;
private LocalDateTime endTime;
private String location;
private int fee;
private String bank;
private String account;
private int minParticipants; //최소 참가자 수 (필요한가?)
// 자동 등록
@Enumerated(EnumType.STRING)
private MatchStatus matchStatus;
@Enumerated(EnumType.STRING)
private Gender gender;
@Enumerated(EnumType.STRING)
private AgeRange ageRange; // Enum 으로 변경
private boolean isBall;
private String note;
@Enumerated(EnumType.STRING)
private MatchHalf matchHalf;
//매치 생성
public static Match createMatch(Club club, MatchInput matchInput) {
Match match = new Match();
match.homeClub = club;
match.name = createMatchName(club, matchInput);
match.event = SportsType.fromKoreanName(matchInput.getEvent()).orElseThrow(() -> new MatchControllerAdvice(ResponseCode.INVALID_SPORTS_TYPE));
match.matchSize = MatchSize.fromKoreanName(matchInput.getMatchSize());
match.startTime = matchInput.getStartTime();
match.endTime = matchInput.getEndTime();
match.location = matchInput.getLocation();
match.fee = matchInput.getFee();
match.bank = matchInput.getBank();
match.account = matchInput.getAccount();
match.minParticipants = matchInput.getMinParticipants();
match.gender = club.getGender();
match.ageRange = club.getAgeRange();
match.matchStatus = PENDING; // 초기 상태는 매치 대기
match.matchHalf = (matchInput.getStartTime().getMonth().getValue() <= 6) ? FIRST_HALF : SECOND_HALF;
match.homeScore = 0;
match.awayScore = 0;
return match;
}
public void setSchedule(Schedule schedule) {
this.schedule = schedule;
}
//매치 등록
public void completeMatch(MatchRegInput matchRegInput) {
this.isBall = matchRegInput.isBall();
this.note = matchRegInput.getNote();
this.matchStatus = REGISTERED;
}
public void confirmMatch(Club awayClub) {
this.awayClub = awayClub;
this.matchStatus = CONFIRMED;
}
//매치 실패
public void failMatch() {
this.matchStatus = FAILED;
}
// 매치 확정 취소
public void cancelConfirmation() {
this.awayClub = null;
this.matchStatus = MatchStatus.REGISTERED;
}
private static String createMatchName(Club club, MatchInput matchInput) {
String clubName = club.getTitle();
String matchType = matchInput.getEvent(); // 종목 (축구, 풋살 등)
String participants = matchInput.getMatchSize(); // 인원수
return String.format("%s팀의 %s %s 매치", clubName, participants, matchType);
}
public ScheduleInput createScheduleFromMatch() {
return new ScheduleInput(
getId(),
getName(),
getLocation(),
getStartTime(),
getEndTime(),
getMinParticipants(),
"친선 매치",
getNote());
}
public Club findOpponentClub(Club club) {
if (Objects.equals(club.getId(), this.getAwayClub().getId())) {
return this.getHomeClub();
}
return this.getAwayClub();
}
public void setMatchScore(int homeScore, int awayScore) {
this.homeScore = homeScore;
this.awayScore = awayScore;
}
public void timeDuplicationCheck(LocalDateTime startTime, LocalDateTime endTime) {
if ((startTime.isBefore(this.startTime) && endTime.isBefore(this.startTime)) ||
(startTime.isAfter(this.endTime) && endTime.isAfter(this.endTime))) {
// throw new MatchRecordExpireException("해당 시간대에 다른 매치 일정이 있습니다");
throw new MatchControllerAdvice(ResponseCode.MATCH_DUPLICATED);
}
}
}