Skip to content

Commit

Permalink
Refactor: 러닝 레코드 Repository 리팩터링 (#297)
Browse files Browse the repository at this point in the history
* Rename: findTotalDistanceMeterByMemberId 메소드명을 findTotalDistanceMeterByMemberIdWithRangeDate로 변경

* Fix: warning 제거(get(0) replace to getFirst())

* Style: 리파지 토리 함수 순서 변경 (find -> has -> save -> delete 순)

* Rename: 메소드명 변경 (findAvgDistanceMeterByMemberIdAndDateRange -> findAvgDistanceMeterByMemberIdWithDateRange)

* Rename: 메소드명 변경 ByDateRange 또는 AndDateRange 접미사를 WithDateRange로 변경하기

* Style: RepositoryImpl 순서 변경(조회 -> update -> save -> delete 순으로, 그 안에서는 jpa -> jooq순으로)

* Feat: 사용자 id로 전체 러닝 거리 합을 구하는 리파지토리 함수 추가

* Refactor: 회원 id + 기간으로 전체 회원의 러닝 거리를 조회 -> 회원아이디에 따라 전체 러닝 거리를 구하는 함수로 변경
  • Loading branch information
hee9841 authored Nov 6, 2024
1 parent a3c395f commit 51075f1
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ public RunningRecordWeeklySummaryResponse getWeeklySummary(
double conversionFactor;

if (summaryType.equals(RunningRecordWeeklySummaryType.DISTANCE)) {
weekSummaries = runningRecordRepository.findDailyDistancesMeterByDateRange(
weekSummaries = runningRecordRepository.findDailyDistancesMeterWithDateRange(
memberId, startWeekDate, nextOfEndWeekDate);
avgValue = runningRecordRepository.findAvgDistanceMeterByMemberIdAndDateRange(
avgValue = runningRecordRepository.findAvgDistanceMeterByMemberIdWithDateRange(
memberId, startWeekDate.minusDays(7), nextOfEndWeekDate.minusDays(7));
conversionFactor = METERS_IN_A_KILOMETER;
} else {
weekSummaries = runningRecordRepository.findDailyDurationsSecByDateRange(
weekSummaries = runningRecordRepository.findDailyDurationsSecWithDateRange(
memberId, startWeekDate, nextOfEndWeekDate);
avgValue = runningRecordRepository.findAvgDurationSecByMemberIdAndDateRange(
avgValue = runningRecordRepository.findAvgDurationSecByMemberIdWithDateRange(
memberId, startWeekDate.minusDays(7), nextOfEndWeekDate.minusDays(7));
conversionFactor = SECONDS_PER_HOUR;
}
Expand Down Expand Up @@ -198,7 +198,7 @@ public RunningRecordAddResultResponse addRunningRecord(long memberId, RunningRec
.build());

OffsetDateTime now = OffsetDateTime.now();
int totalDistance = runningRecordRepository.findTotalDistanceMeterByMemberId(memberId, BASE_TIME, now);
int totalDistance = runningRecordRepository.findTotalDistanceMeterByMemberId(memberId);
Duration totalDuration = runningRecordRepository.findTotalDurationByMemberId(memberId, BASE_TIME, now);

eventPublisher.publishEvent(new RunningRecordAddedEvent(member, record, totalDistance, totalDuration));
Expand Down Expand Up @@ -226,7 +226,7 @@ public RunningRecordMonthlySummaryResponse getMonthlyRunningSummery(long memberI

int monthValue = startDateOfMonth.getMonthValue();

int monthlyTotalDistance = runningRecordRepository.findTotalDistanceMeterByMemberId(
int monthlyTotalDistance = runningRecordRepository.findTotalDistanceMeterByMemberIdWithRangeDate(
memberId, startDateOfMonth, startDateOfNextMonth);

MemberLevel.Current currentMemberLevel = memberLevelRepository.findByMemberIdWithLevel(memberId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.List;

Expand Down Expand Up @@ -71,9 +70,7 @@ private List<ScaleCoursesResponse.AchievedCourse> getAchievedCourses(
private ScaleCoursesResponse.CurrentCourse calculateCurrentScaleLeftMeter(
List<ScaleAchievementLog> scaleAchievementLogs, long memberId) {

OffsetDateTime now = OffsetDateTime.now();
OffsetDateTime start = OffsetDateTime.of(LocalDate.of(1, 1, 1).atStartOfDay(), now.getOffset());
int memberRunMeterSum = runningRecordRepository.findTotalDistanceMeterByMemberId(memberId, start, now);
int memberRunMeterSum = runningRecordRepository.findTotalDistanceMeterByMemberId(memberId);

ScaleAchievementLog currentScale = scaleAchievementLogs.stream()
.filter(log -> log.achievedDate() == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,32 @@
public interface RunningRecordRepository {
Optional<RunningRecord> findById(long runningRecordId);

RunningRecord save(RunningRecord runningRecord);

void deleteByMemberId(long memberId);

List<RunningRecord> findByMemberIdAndStartAtBetween(
long memberId, OffsetDateTime startTime, OffsetDateTime endTime);

boolean hasByMemberIdAndStartAtBetween(long memberId, OffsetDateTime startTime, OffsetDateTime endTime);
List<RunningRecord> findByMember(Member member);

int findTotalDistanceMeterByMemberId(long memberId, OffsetDateTime startDate, OffsetDateTime endDate);
int findTotalDistanceMeterByMemberId(long memberId);

Duration findTotalDurationByMemberId(long memberId, OffsetDateTime startDate, OffsetDateTime endDate);
int findTotalDistanceMeterByMemberIdWithRangeDate(long memberId, OffsetDateTime startDate, OffsetDateTime endDate);

List<RunningRecord> findByMember(Member member);
Duration findTotalDurationByMemberId(long memberId, OffsetDateTime startDate, OffsetDateTime endDate);

List<DailyRunningRecordSummary> findDailyDistancesMeterByDateRange(
List<DailyRunningRecordSummary> findDailyDistancesMeterWithDateRange(
long memberId, OffsetDateTime startDate, OffsetDateTime nextDateOfEndDate);

List<DailyRunningRecordSummary> findDailyDurationsSecByDateRange(
List<DailyRunningRecordSummary> findDailyDurationsSecWithDateRange(
long memberId, OffsetDateTime startDate, OffsetDateTime nextDateOfEndDate);

int findAvgDistanceMeterByMemberIdAndDateRange(
int findAvgDistanceMeterByMemberIdWithDateRange(
long memberId, OffsetDateTime startDate, OffsetDateTime nextDateOfEndDate);

int findAvgDurationSecByMemberIdAndDateRange(
int findAvgDurationSecByMemberIdWithDateRange(
long memberId, OffsetDateTime startDate, OffsetDateTime nextDateOfEndDate);

boolean hasByMemberIdAndStartAtBetween(long memberId, OffsetDateTime startTime, OffsetDateTime endTime);

RunningRecord save(RunningRecord runningRecord);

void deleteByMemberId(long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ public Optional<RunningRecord> findById(long runningRecordId) {
return jpaRunningRecordRepository.findById(runningRecordId).map(RunningRecordEntity::toDomain);
}

@Override
public RunningRecord save(RunningRecord runningRecord) {
return jpaRunningRecordRepository
.save(RunningRecordEntity.from(runningRecord))
.toDomain();
}

@Override
public void deleteByMemberId(long memberId) {
jpaRunningRecordRepository.deleteByMemberId(memberId);
}

@Override
public List<RunningRecord> findByMemberIdAndStartAtBetween(
long memberId, OffsetDateTime startTime, OffsetDateTime endTime) {
Expand All @@ -49,13 +37,21 @@ public List<RunningRecord> findByMemberIdAndStartAtBetween(
}

@Override
public boolean hasByMemberIdAndStartAtBetween(long memberId, OffsetDateTime startTime, OffsetDateTime endTime) {
return jpaRunningRecordRepository.existsByMemberIdAndStartAtBetween(memberId, startTime, endTime);
public List<RunningRecord> findByMember(Member member) {
return jpaRunningRecordRepository.findByMemberId(member.memberId()).stream()
.map(RunningRecordEntity::toDomain)
.toList();
}

@Override
public int findTotalDistanceMeterByMemberId(long memberId, OffsetDateTime startDate, OffsetDateTime endtDate) {
return jooqRunningRecordRepository.findTotalDistanceMeterByMemberId(memberId, startDate, endtDate);
public int findTotalDistanceMeterByMemberId(long memberId) {
return jooqRunningRecordRepository.findTotalDistanceMeterByMemberId(memberId);
}

@Override
public int findTotalDistanceMeterByMemberIdWithRangeDate(
long memberId, OffsetDateTime startDate, OffsetDateTime endDate) {
return jooqRunningRecordRepository.findTotalDistanceMeterByMemberIdWithRangeDate(memberId, startDate, endDate);
}

@Override
Expand All @@ -65,36 +61,46 @@ public Duration findTotalDurationByMemberId(long memberId, OffsetDateTime startD
}

@Override
public List<RunningRecord> findByMember(Member member) {
return jpaRunningRecordRepository.findByMemberId(member.memberId()).stream()
.map(RunningRecordEntity::toDomain)
.toList();
public List<DailyRunningRecordSummary> findDailyDistancesMeterWithDateRange(
long memberId, OffsetDateTime startDate, OffsetDateTime nextDateOfEndDate) {
return jooqRunningRecordRepository.findDailyDistancesMeterWithDateRange(memberId, startDate, nextDateOfEndDate);
}

@Override
public List<DailyRunningRecordSummary> findDailyDistancesMeterByDateRange(
public List<DailyRunningRecordSummary> findDailyDurationsSecWithDateRange(
long memberId, OffsetDateTime startDate, OffsetDateTime nextDateOfEndDate) {
return jooqRunningRecordRepository.findDailyDistancesMeterByDateRange(memberId, startDate, nextDateOfEndDate);
return jooqRunningRecordRepository.findDailyDurationsSecMeterWithDateRange(
memberId, startDate, nextDateOfEndDate);
}

@Override
public List<DailyRunningRecordSummary> findDailyDurationsSecByDateRange(
public int findAvgDistanceMeterByMemberIdWithDateRange(
long memberId, OffsetDateTime startDate, OffsetDateTime nextDateOfEndDate) {
return jooqRunningRecordRepository.findDailyDurationsSecMeterByDateRange(
return jooqRunningRecordRepository.findAvgDistanceMeterByMemberIdWithDateRange(
memberId, startDate, nextDateOfEndDate);
}

@Override
public int findAvgDistanceMeterByMemberIdAndDateRange(
public int findAvgDurationSecByMemberIdWithDateRange(
long memberId, OffsetDateTime startDate, OffsetDateTime nextDateOfEndDate) {
return jooqRunningRecordRepository.findAvgDistanceMeterByMemberIdAndDateRange(
return jooqRunningRecordRepository.findAvgDurationSecByMemberIdWithDateRange(
memberId, startDate, nextDateOfEndDate);
}

@Override
public int findAvgDurationSecByMemberIdAndDateRange(
long memberId, OffsetDateTime startDate, OffsetDateTime nextDateOfEndDate) {
return jooqRunningRecordRepository.findAvgDurationSecByMemberIdAndDateRange(
memberId, startDate, nextDateOfEndDate);
public boolean hasByMemberIdAndStartAtBetween(long memberId, OffsetDateTime startTime, OffsetDateTime endTime) {
return jpaRunningRecordRepository.existsByMemberIdAndStartAtBetween(memberId, startTime, endTime);
}

@Override
public RunningRecord save(RunningRecord runningRecord) {
return jpaRunningRecordRepository
.save(RunningRecordEntity.from(runningRecord))
.toDomain();
}

@Override
public void deleteByMemberId(long memberId) {
jpaRunningRecordRepository.deleteByMemberId(memberId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@
public class JooqRunningRecordRepository {
private final DSLContext dsl;

public int findTotalDistanceMeterByMemberId(long memberId, OffsetDateTime startDate, OffsetDateTime endDate) {
public int findTotalDistanceMeterByMemberId(long memberId) {
Record1<Integer> result = dsl.select(sum(RUNNING_RECORD.DISTANCE_METER).cast(Integer.class))
.from(RUNNING_RECORD)
.where(RUNNING_RECORD.MEMBER_ID.eq(memberId))
.fetchOne();
if (result != null && result.value1() != null) {
return result.value1();
}
return 0;
}

public int findTotalDistanceMeterByMemberIdWithRangeDate(
long memberId, OffsetDateTime startDate, OffsetDateTime endDate) {
Record1<Integer> result = dsl.select(sum(RUNNING_RECORD.DISTANCE_METER).cast(Integer.class))
.from(RUNNING_RECORD)
.where(RUNNING_RECORD.MEMBER_ID.eq(memberId))
Expand All @@ -34,7 +46,20 @@ public int findTotalDistanceMeterByMemberId(long memberId, OffsetDateTime startD
return 0;
}

public int findAvgDistanceMeterByMemberIdAndDateRange(
public long findTotalDurationByMemberId(long memberId, OffsetDateTime startDate, OffsetDateTime nextDateOfEndDate) {
Record1<Long> result = dsl.select(sum(RUNNING_RECORD.DURATION_SECONDS).cast(Long.class))
.from(RUNNING_RECORD)
.where(RUNNING_RECORD.MEMBER_ID.eq(memberId))
.and(RUNNING_RECORD.START_AT.ge(startDate))
.and(RUNNING_RECORD.START_AT.lt(nextDateOfEndDate))
.fetchOne();
if (result != null && result.value1() != null) {
return result.value1();
}
return 0;
}

public int findAvgDistanceMeterByMemberIdWithDateRange(
long memberId, OffsetDateTime startDate, OffsetDateTime nextDateOfEndDate) {
Record1<Integer> result = dsl.select(avg(RUNNING_RECORD.DISTANCE_METER).cast(Integer.class))
.from(RUNNING_RECORD)
Expand All @@ -48,7 +73,7 @@ public int findAvgDistanceMeterByMemberIdAndDateRange(
return 0;
}

public int findAvgDurationSecByMemberIdAndDateRange(
public int findAvgDurationSecByMemberIdWithDateRange(
long memberId, OffsetDateTime startDate, OffsetDateTime nextDateOfEndDate) {
Record1<Integer> result = dsl.select(
avg(RUNNING_RECORD.DURATION_SECONDS).cast(Integer.class))
Expand All @@ -71,7 +96,7 @@ public int findAvgDurationSecByMemberIdAndDateRange(
* @return 기간 내 각 날짜별 달린 거리 합계를 포함한 리스트.
* 각 요소는 날짜와 해당 날짜의 거리 합계를 나타내는 {@link DailyRunningRecordSummary} 객체입니다.
*/
public List<DailyRunningRecordSummary> findDailyDistancesMeterByDateRange(
public List<DailyRunningRecordSummary> findDailyDistancesMeterWithDateRange(
long memberId, OffsetDateTime startDate, OffsetDateTime nextDateOfEndDate) {

return dsl.select(
Expand All @@ -94,7 +119,7 @@ public List<DailyRunningRecordSummary> findDailyDistancesMeterByDateRange(
* @return 기간 내 각 날짜별 달린 시간 합계를 포함한 리스트.
* 각 요소는 날짜와 해당 날짜의 달린 시간 합계를 나타내는 {@link DailyRunningRecordSummary} 객체입니다.
*/
public List<DailyRunningRecordSummary> findDailyDurationsSecMeterByDateRange(
public List<DailyRunningRecordSummary> findDailyDurationsSecMeterWithDateRange(
long memberId, OffsetDateTime startDate, OffsetDateTime nextDateOfEndDate) {

return dsl.select(
Expand All @@ -109,19 +134,6 @@ public List<DailyRunningRecordSummary> findDailyDurationsSecMeterByDateRange(
.fetch(new DailyRunningSummary());
}

public long findTotalDurationByMemberId(long memberId, OffsetDateTime startDate, OffsetDateTime nextDateOfEndDate) {
Record1<Long> result = dsl.select(sum(RUNNING_RECORD.DURATION_SECONDS).cast(Long.class))
.from(RUNNING_RECORD)
.where(RUNNING_RECORD.MEMBER_ID.eq(memberId))
.and(RUNNING_RECORD.START_AT.ge(startDate))
.and(RUNNING_RECORD.START_AT.lt(nextDateOfEndDate))
.fetchOne();
if (result != null && result.value1() != null) {
return result.value1();
}
return 0;
}

private static class DailyRunningSummary implements RecordMapper<Record, DailyRunningRecordSummary> {
@Override
public DailyRunningRecordSummary map(Record record) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ void getMonthlyRunningSummery() {
.when(() -> OffsetDateTime.now(ZoneId.of(SERVER_TIMEZONE)))
.thenReturn(fixedDate);

given(runningRecordRepository.findTotalDistanceMeterByMemberId(eq(memberId), any(), any()))
given(runningRecordRepository.findTotalDistanceMeterByMemberIdWithRangeDate(eq(memberId), any(), any()))
.willReturn(45_780);

given(memberLevelRepository.findByMemberIdWithLevel(memberId))
Expand Down Expand Up @@ -461,10 +461,10 @@ void getWeeklySummary_Distance() {

OffsetDateTime runningDate = startWeekDate.plusDays(2);

given(runningRecordRepository.findDailyDistancesMeterByDateRange(memberId, startWeekDate, nextOfEndWeekDate))
given(runningRecordRepository.findDailyDistancesMeterWithDateRange(memberId, startWeekDate, nextOfEndWeekDate))
.willReturn(List.of(new DailyRunningRecordSummary(runningDate.toLocalDate(), 3567)));

given(runningRecordRepository.findAvgDistanceMeterByMemberIdAndDateRange(
given(runningRecordRepository.findAvgDistanceMeterByMemberIdWithDateRange(
memberId, startWeekDate.minusDays(7), nextOfEndWeekDate.minusDays(7)))
.willReturn(800);

Expand Down Expand Up @@ -496,10 +496,10 @@ void getWeeklySummary_Duration() {

OffsetDateTime runningDate = startWeekDate.plusDays(2);

given(runningRecordRepository.findDailyDurationsSecByDateRange(memberId, startWeekDate, nextOfEndWeekDate))
given(runningRecordRepository.findDailyDurationsSecWithDateRange(memberId, startWeekDate, nextOfEndWeekDate))
.willReturn(List.of(new DailyRunningRecordSummary(runningDate.toLocalDate(), runningDurationSec)));

given(runningRecordRepository.findAvgDurationSecByMemberIdAndDateRange(
given(runningRecordRepository.findAvgDurationSecByMemberIdWithDateRange(
memberId, startWeekDate.minusDays(7), nextOfEndWeekDate.minusDays(7)))
.willReturn(runningDurationSec);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -56,7 +54,7 @@ void getAchievements() {
new ScaleAchievementLog(scale2, null),
new ScaleAchievementLog(scale3, null)));
int runningMeterSum = 50;
given(runningRecordRepository.findTotalDistanceMeterByMemberId(eq(memberId), any(), any()))
given(runningRecordRepository.findTotalDistanceMeterByMemberId(memberId))
.willReturn(runningMeterSum);

// when
Expand All @@ -78,7 +76,7 @@ void getAchievementsWithAchievedCourse() {
new ScaleAchievementLog(scale1, OffsetDateTime.now()),
new ScaleAchievementLog(scale2, null),
new ScaleAchievementLog(scale3, null)));
given(runningRecordRepository.findTotalDistanceMeterByMemberId(eq(memberId), any(), any()))
given(runningRecordRepository.findTotalDistanceMeterByMemberId(memberId))
.willReturn(1000);

// when
Expand All @@ -88,8 +86,8 @@ void getAchievementsWithAchievedCourse() {
assertNotNull(response);
assertEquals(1, response.achievedCourses().size());

assertEquals(scale1.name(), response.achievedCourses().get(0).name());
assertEquals("0.2km", response.achievedCourses().get(0).totalDistance());
assertEquals(scale1.name(), response.achievedCourses().getFirst().name());
assertEquals("0.2km", response.achievedCourses().getFirst().totalDistance());

assertEquals(scale2.name(), response.currentCourse().name());
assertEquals("800m", response.currentCourse().achievedDistance());
Expand Down
Loading

0 comments on commit 51075f1

Please sign in to comment.