Skip to content

Commit 0335dae

Browse files
authored
[DDING-121] 활동보고서 현재 회차 조회 로직 수정 (#284)
1 parent 83b9d02 commit 0335dae

File tree

5 files changed

+59
-65
lines changed

5 files changed

+59
-65
lines changed

src/main/java/ddingdong/ddingdongBE/domain/activityreport/controller/ClubActivityReportApiController.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import ddingdong.ddingdongBE.domain.activityreport.service.dto.query.ActivityReportQuery;
1818
import ddingdong.ddingdongBE.domain.activityreport.service.dto.query.ActivityReportTermInfoQuery;
1919
import ddingdong.ddingdongBE.domain.user.entity.User;
20+
import java.time.LocalDateTime;
2021
import java.util.List;
2122
import lombok.RequiredArgsConstructor;
2223
import org.springframework.web.bind.annotation.RestController;
@@ -29,7 +30,8 @@ public class ClubActivityReportApiController implements ClubActivityReportApi {
2930

3031
@Override
3132
public CurrentTermResponse getCurrentTerm() {
32-
String currentTerm = facadeClubActivityReportService.getCurrentTerm();
33+
LocalDateTime now = LocalDateTime.now();
34+
String currentTerm = facadeClubActivityReportService.getCurrentTerm(now);
3335
return CurrentTermResponse.from(currentTerm);
3436
}
3537

src/main/java/ddingdong/ddingdongBE/domain/activityreport/service/ActivityReportTermInfoService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ddingdong.ddingdongBE.domain.activityreport.domain.ActivityReportTermInfo;
44
import java.time.LocalDate;
5+
import java.time.LocalDateTime;
56
import java.util.List;
67

78
public interface ActivityReportTermInfoService {
@@ -10,5 +11,5 @@ public interface ActivityReportTermInfoService {
1011

1112
void create(LocalDate startDate, int totalTermCount);
1213

13-
String getCurrentTerm();
14+
String getCurrentTerm(LocalDateTime now);
1415
}
Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package ddingdong.ddingdongBE.domain.activityreport.service;
22

3-
import ddingdong.ddingdongBE.common.exception.PersistenceException.ResourceNotFound;
43
import ddingdong.ddingdongBE.domain.activityreport.domain.ActivityReportTermInfo;
54
import ddingdong.ddingdongBE.domain.activityreport.repository.ActivityReportTermInfoRepository;
6-
import java.time.Duration;
75
import java.time.LocalDate;
6+
import java.time.LocalDateTime;
87
import java.util.List;
98
import java.util.stream.Collectors;
109
import java.util.stream.IntStream;
@@ -17,9 +16,7 @@
1716
@Transactional(readOnly = true)
1817
public class ActivityReportTermInfoServiceImpl implements ActivityReportTermInfoService {
1918

20-
private static final int DEFAULT_TERM = 8;
21-
private static final int CORRECTION_VALUE = 8;
22-
private static final int TERM_LENGTH_OF_DAYS = 14;
19+
private static final int NON_ACTIVITY_REPORT_TERM = 0;
2320

2421
private final ActivityReportTermInfoRepository activityReportTermInfoRepository;
2522

@@ -47,28 +44,20 @@ public void create(LocalDate startDate, int totalTermCount) {
4744
}
4845

4946
@Override
50-
public String getCurrentTerm() {
51-
ActivityReportTermInfo activityReportTermInfo = activityReportTermInfoRepository.findFirstByOrderByStartDateAsc()
52-
.orElseThrow(() -> new ResourceNotFound("활동보고서 기간이 존재하지 않습니다."));
53-
LocalDate startDate = activityReportTermInfo.getStartDate();
54-
LocalDate currentDate = LocalDate.now();
55-
56-
int gapOfDays = calculateGapOfDays(startDate, currentDate);
57-
return calculateCurrentTerm(gapOfDays);
58-
}
59-
60-
private int calculateGapOfDays(final LocalDate startDate, final LocalDate currentDate) {
61-
return (int) Duration.between(startDate.atStartOfDay(), currentDate.atStartOfDay())
62-
.toDays();
47+
public String getCurrentTerm(LocalDateTime now) {
48+
List<ActivityReportTermInfo> allActivityReportTermInfo = activityReportTermInfoRepository.findAll();
49+
Integer currentTerm = allActivityReportTermInfo.stream()
50+
.filter((activityReportTermInfo) -> isBelongingTerm(activityReportTermInfo.getStartDate(),
51+
activityReportTermInfo.getEndDate(), now))
52+
.map(ActivityReportTermInfo::getTerm)
53+
.findFirst()
54+
.orElse(NON_ACTIVITY_REPORT_TERM);
55+
return String.valueOf(currentTerm);
6356
}
6457

65-
private String calculateCurrentTerm(final int days) {
66-
int result = CORRECTION_VALUE + (days / TERM_LENGTH_OF_DAYS);
67-
68-
if (result <= CORRECTION_VALUE) {
69-
result = DEFAULT_TERM;
70-
}
71-
72-
return String.valueOf(result);
58+
private boolean isBelongingTerm(LocalDate startDate, LocalDate endDate, LocalDateTime now) {
59+
LocalDate nowDate = now.toLocalDate();
60+
return (nowDate.isEqual(startDate) || nowDate.isAfter(startDate)) &&
61+
(nowDate.isEqual(endDate) || nowDate.isBefore(endDate));
7362
}
7463
}

src/main/java/ddingdong/ddingdongBE/domain/activityreport/service/FacadeClubActivityReportService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import ddingdong.ddingdongBE.domain.activityreport.service.dto.query.ActivityReportQuery;
77
import ddingdong.ddingdongBE.domain.activityreport.service.dto.query.ActivityReportTermInfoQuery;
88
import ddingdong.ddingdongBE.domain.user.entity.User;
9+
import java.time.LocalDateTime;
910
import java.util.List;
1011

1112
public interface FacadeClubActivityReportService {
@@ -16,7 +17,7 @@ public interface FacadeClubActivityReportService {
1617

1718
List<ActivityReportTermInfoQuery> getActivityReportTermInfos();
1819

19-
String getCurrentTerm();
20+
String getCurrentTerm(LocalDateTime now);
2021

2122
void create(User user, List<CreateActivityReportCommand> commands);
2223

src/main/java/ddingdong/ddingdongBE/domain/activityreport/service/FacadeClubActivityReportServiceImpl.java

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import ddingdong.ddingdongBE.domain.user.entity.User;
1616
import ddingdong.ddingdongBE.file.service.S3FileService;
1717
import ddingdong.ddingdongBE.file.service.dto.query.UploadedFileUrlQuery;
18+
import java.time.LocalDateTime;
1819
import java.util.List;
1920
import java.util.Map;
2021
import java.util.stream.Collectors;
@@ -26,7 +27,7 @@
2627
@Service
2728
@Transactional(readOnly = true)
2829
@RequiredArgsConstructor
29-
public class FacadeClubActivityReportServiceImpl implements FacadeClubActivityReportService{
30+
public class FacadeClubActivityReportServiceImpl implements FacadeClubActivityReportService {
3031

3132
private final ActivityReportService activityReportService;
3233
private final ActivityReportTermInfoService activityReportTermInfoService;
@@ -36,14 +37,14 @@ public class FacadeClubActivityReportServiceImpl implements FacadeClubActivityRe
3637

3738
@Override
3839
public List<ActivityReportQuery> getActivityReport(
39-
String term,
40-
String clubName
40+
String term,
41+
String clubName
4142
) {
4243
List<ActivityReport> activityReports = activityReportService.getActivityReport(clubName, term);
4344

4445
return activityReports.stream()
45-
.map(this::parseToQuery)
46-
.toList();
46+
.map(this::parseToQuery)
47+
.toList();
4748
}
4849

4950
@Override
@@ -57,20 +58,20 @@ public List<ActivityReportListQuery> getMyActivityReports(User user) {
5758
public List<ActivityReportTermInfoQuery> getActivityReportTermInfos() {
5859
List<ActivityReportTermInfo> termInfos = activityReportTermInfoService.getActivityReportTermInfos();
5960
return termInfos.stream()
60-
.map(ActivityReportTermInfoQuery::from)
61-
.toList();
61+
.map(ActivityReportTermInfoQuery::from)
62+
.toList();
6263
}
6364

6465
@Override
65-
public String getCurrentTerm() {
66-
return activityReportTermInfoService.getCurrentTerm();
66+
public String getCurrentTerm(LocalDateTime now) {
67+
return activityReportTermInfoService.getCurrentTerm(now);
6768
}
6869

6970
@Transactional
7071
@Override
7172
public void create(
72-
User user,
73-
List<CreateActivityReportCommand> commands
73+
User user,
74+
List<CreateActivityReportCommand> commands
7475
) {
7576
Club club = clubService.getByUserId(user.getId());
7677
commands.forEach(command -> {
@@ -84,25 +85,25 @@ public void create(
8485
@Transactional
8586
@Override
8687
public void update(
87-
User user,
88-
String term,
89-
List<UpdateActivityReportCommand> commands
88+
User user,
89+
String term,
90+
List<UpdateActivityReportCommand> commands
9091
) {
9192
Club club = clubService.getByUserId(user.getId());
9293
List<ActivityReport> activityReports = activityReportService.getActivityReportOrThrow(club.getName(), term);
9394

9495
IntStream.range(0, commands.size())
95-
.forEach(index -> {
96-
ActivityReport activityReport = activityReports.get(index);
97-
ActivityReport updateActivityReport = commands.get(index).toEntity();
98-
activityReportService.update(activityReport, updateActivityReport);
99-
100-
fileMetaDataService.update(
101-
commands.get(index).imageId(),
102-
DomainType.ACTIVITY_REPORT_IMAGE,
103-
activityReport.getId()
104-
);
105-
});
96+
.forEach(index -> {
97+
ActivityReport activityReport = activityReports.get(index);
98+
ActivityReport updateActivityReport = commands.get(index).toEntity();
99+
activityReportService.update(activityReport, updateActivityReport);
100+
101+
fileMetaDataService.update(
102+
commands.get(index).imageId(),
103+
DomainType.ACTIVITY_REPORT_IMAGE,
104+
activityReport.getId()
105+
);
106+
});
106107
}
107108

108109
@Transactional
@@ -112,25 +113,25 @@ public void delete(User user, String term) {
112113
List<ActivityReport> activityReports = activityReportService.getActivityReportOrThrow(club.getName(), term);
113114
activityReportService.deleteAll(activityReports);
114115
activityReports.forEach(activityReport -> {
115-
fileMetaDataService.updateStatusToDelete(DomainType.ACTIVITY_REPORT_IMAGE, activityReport.getId());
116+
fileMetaDataService.updateStatusToDelete(DomainType.ACTIVITY_REPORT_IMAGE, activityReport.getId());
116117
});
117118
}
118119

119120
private ActivityReportQuery parseToQuery(ActivityReport activityReport) {
120121
UploadedFileUrlQuery image = fileMetaDataService
121-
.getCoupledAllByDomainTypeAndEntityId(DomainType.ACTIVITY_REPORT_IMAGE, activityReport.getId())
122-
.stream()
123-
.map(fileMetaData -> s3FileService.getUploadedFileUrl(fileMetaData.getFileKey()))
124-
.findFirst()
125-
.orElse(null);
122+
.getCoupledAllByDomainTypeAndEntityId(DomainType.ACTIVITY_REPORT_IMAGE, activityReport.getId())
123+
.stream()
124+
.map(fileMetaData -> s3FileService.getUploadedFileUrl(fileMetaData.getFileKey()))
125+
.findFirst()
126+
.orElse(null);
126127
return ActivityReportQuery.of(activityReport, image);
127128
}
128129

129130
private List<ActivityReportListQuery> parseToListQuery(final List<ActivityReport> activityReports) {
130131
Map<String, Map<String, List<Long>>> groupedData = activityReports.stream().collect(
131-
Collectors.groupingBy(activityReport -> activityReport.getClub().getName(),
132-
Collectors.groupingBy(ActivityReport::getTerm,
133-
Collectors.mapping(ActivityReport::getId, Collectors.toList()))));
132+
Collectors.groupingBy(activityReport -> activityReport.getClub().getName(),
133+
Collectors.groupingBy(ActivityReport::getTerm,
134+
Collectors.mapping(ActivityReport::getId, Collectors.toList()))));
134135

135136
return groupedData.entrySet().stream().flatMap(entry -> {
136137
String clubName = entry.getKey();
@@ -139,8 +140,8 @@ private List<ActivityReportListQuery> parseToListQuery(final List<ActivityReport
139140
return termMap.entrySet().stream().map(termEntry -> {
140141
String term = termEntry.getKey();
141142
List<ActivityReportInfo> activityReportInfos = termEntry.getValue().stream()
142-
.map(ActivityReportInfo::new)
143-
.toList();
143+
.map(ActivityReportInfo::new)
144+
.toList();
144145
return ActivityReportListQuery.of(clubName, term, activityReportInfos);
145146
});
146147

0 commit comments

Comments
 (0)