Skip to content

Commit 9d3bf3a

Browse files
authored
[refactor] 빌더 패턴 적용 및 서비스 메서드 로직 변경
[refactor] 빌더 패턴 적용 및 서비스 메서드 로직 변경
2 parents 2c6e0de + 9aae871 commit 9d3bf3a

34 files changed

Lines changed: 663 additions & 724 deletions

MathCaptain/weakness/src/main/java/MathCaptain/weakness/TestInit.java

Lines changed: 32 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import MathCaptain.weakness.domain.Group.repository.RelationRepository;
1010
import MathCaptain.weakness.domain.Record.entity.ActivityRecord;
1111
import MathCaptain.weakness.domain.Record.repository.RecordRepository;
12+
import MathCaptain.weakness.domain.Recruitment.dto.request.CreateRecruitmentRequest;
1213
import MathCaptain.weakness.domain.Recruitment.entity.Comment;
1314
import MathCaptain.weakness.domain.Recruitment.entity.Recruitment;
1415
import MathCaptain.weakness.domain.Recruitment.enums.RecruitmentStatus;
@@ -90,7 +91,7 @@ public void init() {
9091
userRepository.save(user);
9192
}
9293

93-
log.info("======== 👤테스트 유저 생성 완료 =========");
94+
log.info("======== 👤테스트 유저 데이터 생성 완료 =========");
9495

9596
Users leader = userRepository.findByUserId(1L)
9697
.orElseThrow(() -> new IllegalArgumentException("해당 유저가 없습니다."));
@@ -122,79 +123,41 @@ public void init() {
122123
group3.updateWeeklyGoalAchieveMap(DayOfWeek.SUNDAY, 0);
123124
groupRepository.save(group3);
124125

125-
log.info("======== 👥 테스트 그룹 생성 완료 =========");
126-
127-
RelationBetweenUserAndGroup join1 = RelationBetweenUserAndGroup.builder()
128-
.member(users1)
129-
.groupRole(GroupRole.LEADER)
130-
.group(group1)
131-
.personalDailyGoal(2)
132-
.personalWeeklyGoal(3)
133-
.build();
134-
135-
RelationBetweenUserAndGroup join2 = RelationBetweenUserAndGroup.builder()
136-
.member(users2)
137-
.groupRole(GroupRole.LEADER)
138-
.group(group2)
139-
.personalDailyGoal(2)
140-
.personalWeeklyGoal(3)
141-
.build();
142-
143-
RelationBetweenUserAndGroup join3 = RelationBetweenUserAndGroup.builder()
144-
.member(users3)
145-
.groupRole(GroupRole.LEADER)
146-
.group(group3)
147-
.personalDailyGoal(2)
148-
.personalWeeklyGoal(3)
149-
.build();
126+
log.info("======== 👥 테스트 그룹 데이터 생성 완료 =========");
127+
128+
RelationBetweenUserAndGroup join1 = RelationBetweenUserAndGroup.of(users1, group1, groupCreateRequest1);
129+
RelationBetweenUserAndGroup join2 = RelationBetweenUserAndGroup.of(users2, group2, groupCreateRequest2);
130+
RelationBetweenUserAndGroup join3 = RelationBetweenUserAndGroup.of(users3, group3, groupCreateRequest3);
150131

151132
relationRepository.save(join1);
152133
relationRepository.save(join2);
153134
relationRepository.save(join3);
154135

155136
for (int i = 4; i <= 12; i++) {
156-
RelationBetweenUserAndGroup join = RelationBetweenUserAndGroup.builder()
157-
.member(userRepository.findByUserId((long) i)
158-
.orElseThrow(() -> new IllegalArgumentException("해당 유저가 없습니다."))
159-
)
160-
.groupRole(GroupRole.MEMBER)
161-
.group(group3)
162-
.personalDailyGoal(3)
163-
.personalWeeklyGoal(5)
164-
.build();
137+
Users member = userRepository.findByUserId((long) i)
138+
.orElseThrow(() -> new IllegalArgumentException("해당 유저가 없습니다."));
139+
RelationBetweenUserAndGroup join = RelationBetweenUserAndGroup.of(member, group3, 3, 5);
165140
relationRepository.save(join);
166141
}
167142

168-
/// 테스트 모집글 생성
169-
Recruitment recruitment = Recruitment.builder()
170-
.postId(1L)
171-
.author(users1)
172-
.recruitGroup(group1)
173-
.category(CategoryStatus.STUDY)
174-
.title("testRecruitment")
175-
.content("testContent")
176-
.recruitmentStatus(RecruitmentStatus.RECRUITING)
177-
.build();
143+
log.info("======== 👥 테스트 관계 데이터 생성 완료 =========");
178144

145+
CreateRecruitmentRequest createRecruitmentRequest = CreateRecruitmentRequest.of(group1.getId(), "testRecruitment", "testContent");
146+
Recruitment recruitment = Recruitment.of(users1, group1, createRecruitmentRequest);
179147
recruitmentRepository.save(recruitment);
148+
180149
log.info("======== 🔖테스트 모집글 생성 완료 =========");
181150

182151
/// 테스트 댓글 생성
183-
Comment comment = Comment.builder()
184-
.commentId(1L)
185-
.author(users1)
186-
.post(recruitment)
187-
.content("testComment")
188-
.build();
189-
152+
Comment comment = Comment.of(recruitment, users1, "testComment");
190153
commentRepository.save(comment);
154+
191155
log.info("======== 💬테스트 댓글 생성 완료 =========");
192156

193157
// ActivityRecord 생성 (currentProgress 설정: 10 이하로 조정)
194158

195159
// 이번 주의 시작과 끝 시간 계산
196160
LocalDateTime startOfWeek = LocalDateTime.now().with(java.time.temporal.TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
197-
LocalDateTime endOfWeek = startOfWeek.plusWeeks(1);
198161

199162
// User ID: 4 -> currentProgress: 5
200163
createActivityRecords(userRepository.findByUserId(4L).orElseThrow(() -> new IllegalArgumentException("해당 유저가 없습니다.")), group3, startOfWeek, 5);
@@ -217,14 +180,22 @@ public void init() {
217180
// Helper 메서드: ActivityRecord 생성 (이번 주 내에서만 생성되도록 수정)
218181
private void createActivityRecords(Users user, Group group, LocalDateTime startOfWeek, int recordCount) {
219182
for (int i = 0; i < recordCount; i++) {
220-
ActivityRecord record = ActivityRecord.builder()
221-
.user(user)
222-
.group(group)
223-
// 이번 주 내에서만 startTime 설정
224-
.startTime(startOfWeek.plusDays(i % 7)) // 월요일부터 시작해서 순차적으로 날짜 설정
225-
.dailyGoalAchieved(true) // 일간 목표 달성 여부 설정
226-
.weeklyGoalAchieved(false) // 주간 목표는 기본값으로 false
227-
.build();
183+
// 시작 시간은 주어진 startOfWeek에서 i일을 더한 값
184+
LocalDateTime startTime = startOfWeek.plusDays(i % 7);
185+
186+
// 종료 시간은 시작 시간에서 1시간을 더한 값으로 설정 (예시)
187+
LocalDateTime endTime = startTime.plusHours(1);
188+
189+
// 활동 시간은 60분으로 설정 (예시)
190+
Long activityTime = 60L;
191+
192+
// 요일은 시작 시간의 DayOfWeek를 사용
193+
DayOfWeek dayOfWeek = startTime.getDayOfWeek();
194+
195+
// ActivityRecord 객체 생성
196+
ActivityRecord record = ActivityRecord.of(user, group, startTime, endTime, activityTime, dayOfWeek);
197+
198+
// 저장소에 저장
228199
recordRepository.save(record);
229200
}
230201
}

MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Group/entity/RelationBetweenUserAndGroup.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.springframework.data.annotation.CreatedDate;
1212
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
1313

14+
import java.time.DayOfWeek;
1415
import java.time.LocalDate;
1516

1617
@Entity
@@ -76,19 +77,19 @@ private RelationBetweenUserAndGroup(Users member, Group group, GroupRole groupRo
7677
this.weeklyGoalAchieveStreak = 0;
7778
}
7879

79-
public static RelationBetweenUserAndGroup of(Users leader, Group group, int dailyGoal, int weeklyGoal) {
80+
public static RelationBetweenUserAndGroup of(Users member, Group group, int dailyGoal, int weeklyGoal) {
8081
return RelationBetweenUserAndGroup.builder()
81-
.member(leader)
82+
.member(member)
8283
.groupRole(GroupRole.MEMBER)
8384
.group(group)
8485
.personalDailyGoal(dailyGoal)
8586
.personalWeeklyGoal(weeklyGoal)
8687
.build();
8788
}
8889

89-
public static RelationBetweenUserAndGroup of(Users member, Group group, GroupCreateRequest groupCreateRequest) {
90+
public static RelationBetweenUserAndGroup of(Users leader, Group group, GroupCreateRequest groupCreateRequest) {
9091
return RelationBetweenUserAndGroup.builder()
91-
.member(member)
92+
.member(leader)
9293
.groupRole(GroupRole.LEADER)
9394
.group(group)
9495
.personalDailyGoal(groupCreateRequest.getPersonalDailyGoal())
@@ -125,18 +126,18 @@ public void updatePersonalDailyGoalAchieved(Long personalDailyGoalAchieved) {
125126
}
126127

127128
// 주간 목표 업데이트
128-
public void updatePersonalWeeklyGoalAchieved(int personalWeeklyGoalAchieve) {
129-
this.personalWeeklyGoalAchieve = personalWeeklyGoalAchieve;
129+
public void updatePersonalWeeklyGoalAchieved() {
130+
this.personalWeeklyGoalAchieve += 1;
130131
}
131132

132133
// 주간 목표 달성 연속 횟수 업데이트
133-
public void updateWeeklyGoalAchieveStreak(int weeklyGoalAchieveStreak) {
134-
this.weeklyGoalAchieveStreak = weeklyGoalAchieveStreak;
134+
public void updateWeeklyGoalAchieveStreak() {
135+
this.weeklyGoalAchieveStreak += 1;
135136
}
136137

137138
// 일간 목표 초기화
138139
public void resetPersonalDailyGoalAchieve() {
139-
this.personalDailyGoalAchieve = (Long) 0L;
140+
this.personalDailyGoalAchieve = 0L;
140141
}
141142

142143
// 주간 목표 초기화
@@ -153,4 +154,24 @@ public void updateRequestStatus(RequestStatus requestStatus) {
153154
this.requestStatus = requestStatus;
154155
}
155156

157+
public void addPoint(Long point) {
158+
this.group.addPoint(point);
159+
this.member.addPoint(point);
160+
}
161+
162+
public int weeklyAchieveBase() {
163+
return this.getWeeklyGoalAchieveStreak() + this.getPersonalWeeklyGoal();
164+
}
165+
166+
public void increaseWeeklyGroupCountOf(DayOfWeek dayOfWeek) {
167+
this.group.increaseWeeklyGoalAchieveMap(dayOfWeek);
168+
}
169+
170+
public Long remainingDailyGoalMinutes() {
171+
return Math.max(this.getPersonalDailyGoal() * 60L - this.getPersonalDailyGoalAchieve(), 0L);
172+
}
173+
174+
public int remainingWeeklyGoalDays() {
175+
return Math.max(this.getPersonalWeeklyGoal() - this.getPersonalWeeklyGoalAchieve(), 0);
176+
}
156177
}
Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,44 @@
11
package MathCaptain.weakness.domain.Notification.entity;
22

33
import jakarta.persistence.*;
4-
import lombok.AllArgsConstructor;
5-
import lombok.Builder;
6-
import lombok.Getter;
7-
import lombok.NoArgsConstructor;
4+
import lombok.*;
85
import org.springframework.data.annotation.CreatedDate;
96
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
107

118
import java.time.LocalDateTime;
9+
import java.util.Map;
1210

1311
@Entity
1412
@Getter
15-
@Builder
16-
@AllArgsConstructor
17-
@NoArgsConstructor
13+
@NoArgsConstructor(access = AccessLevel.PROTECTED)
1814
@EntityListeners(AuditingEntityListener.class)
1915
public class Notification {
2016

2117
@Id
2218
@GeneratedValue(strategy = GenerationType.IDENTITY)
2319
private Long id;
2420

21+
@Column(nullable = false)
2522
private String sender;
2623

2724
@CreatedDate
2825
private LocalDateTime createdAt;
2926

27+
@Column(nullable = false)
3028
private String contents;
3129

32-
30+
@Builder
31+
private Notification(String sender, LocalDateTime createdAt, String contents) {
32+
this.sender = sender;
33+
this.createdAt = createdAt;
34+
this.contents = contents;
35+
}
36+
37+
public static Notification of(Map<String, String> eventData) {
38+
return Notification.builder()
39+
.sender(eventData.get("sender"))
40+
.createdAt(LocalDateTime.parse(eventData.get("createdAt")))
41+
.contents(eventData.get("message"))
42+
.build();
43+
}
3344
}

0 commit comments

Comments
 (0)