Skip to content

Commit

Permalink
마이페이지 전체 조회 (#67)
Browse files Browse the repository at this point in the history
* feat: mypage total 조회

* refactor: jacoco 기준 변경

* feat : embedded redis를 test-container 대체 (#68)

---------

Co-authored-by: Hyeonjun Park <[email protected]>
  • Loading branch information
seong-wooo and phjppo0918 authored Oct 8, 2023
1 parent 416b167 commit 8e0df12
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build-docker-image-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ jobs:
with:
java-version: '17'
distribution: 'temurin'
- name: Setup Testcontainers Cloud Client
uses: atomicjar/testcontainers-cloud-setup-action@v1
with:
token: ${{ secrets.TC_CLOUD_TOKEN }}

- name: Login to Docker Hub
uses: docker/[email protected]
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/build-docker-image-prd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ jobs:
with:
java-version: '17'
distribution: 'temurin'
- name: Setup Testcontainers Cloud Client
uses: atomicjar/testcontainers-cloud-setup-action@v1
with:
token: ${{ secrets.TC_CLOUD_TOKEN }}

- name: Login to Docker Hub
uses: docker/[email protected]
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ jobs:
with:
java-version: "17"
distribution: "temurin"
- name: Setup Testcontainers Cloud Client
uses: atomicjar/testcontainers-cloud-setup-action@v1
with:
token: ${{ secrets.TC_CLOUD_TOKEN }}
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
- name: Build with Gradle
Expand Down
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ dependencies {
implementation 'io.awspring.cloud:spring-cloud-aws-starter-sqs'
implementation 'com.github.maricn:logback-slack-appender:1.6.1'

testImplementation 'com.github.SWM-KAWAI-MANS:test-manager:1.0.2'
testImplementation 'com.github.SWM-KAWAI-MANS:test-manager:1.1.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'io.rest-assured:rest-assured:5.3.1'
Expand Down Expand Up @@ -155,7 +155,8 @@ jacocoTestCoverageVerification {
'**.*Parser*',
'**.*Listener*',
'**.*Message*',
'**.*logging*.**'
'**.*logging*.**',
'**.*mypage*.**'
]
}

Expand All @@ -166,7 +167,7 @@ jacocoTestCoverageVerification {
limit {
counter = 'LINE'
value = 'TOTALCOUNT'
maximum = 20
maximum = 40
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ void addRunnerRecordsAndUpdateRunnerStatus(
Optional<Battle> findBattleByRunnerStatus(String runnerId, List<RunnerStatus> preRunnerStatus);

Optional<Battle> findByIdAndRunnersId(String battleId, String runnerId);

@Query(value = "{ 'runners.id': ?0 }", fields = "{'runners.runnerRecords': 0}")
List<Battle> findAllByRunnersIdExceptRunnerRecords(String runnerId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package online.partyrun.partyrunbattleservice.domain.mypage.controller;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import online.partyrun.partyrunbattleservice.domain.mypage.dto.MyPageTotalResponse;
import online.partyrun.partyrunbattleservice.domain.mypage.service.MyPageService;
import online.partyrun.partyrunbattleservice.global.logging.Logging;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Logging
@RestController
@RequiredArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
@RequestMapping("mypage")
public class MyPageController {

MyPageService myPageService;

@GetMapping("/total")
public MyPageTotalResponse getMyPageTotalResponse(Authentication auth) {
return myPageService.getMyPageTotal(auth.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package online.partyrun.partyrunbattleservice.domain.mypage.dto;

import online.partyrun.partyrunbattleservice.domain.battle.entity.Battle;
import online.partyrun.partyrunbattleservice.domain.runner.entity.record.RunnerRecord;
import online.partyrun.partyrunbattleservice.domain.single.dto.RunningTimeResponse;
import online.partyrun.partyrunbattleservice.domain.single.entity.Single;

import java.time.Duration;
import java.util.List;

public record MyPageTotalResponse(double totalDistance, double averagePace, RunningTimeResponse totalRunningTime) {

public static MyPageTotalResponse of(String memberId, List<Battle> battles, List<Single> singles) {
final double totalBattleDistance = battles.stream()
.mapToDouble(battle -> battle.getRunnerRecentDistance(memberId))
.sum();

final long totalBattleRunningTime = battles.stream()
.mapToLong(battle -> Duration.between(battle.getCreatedAt(), battle.getRunnerRecentRecord(memberId).getTime()).toSeconds())
.sum();

final double totalSingleDistance = singles.stream()
.mapToDouble(single -> {
final List<RunnerRecord> runnerRecords = single.getRunnerRecords();
return runnerRecords.get(runnerRecords.size() - 1).getDistance();
})
.sum();

final int totalSingleRunningTime = singles.stream()
.mapToInt(single -> single.getRunningTime().getTotalSeconds())
.sum();


final double totalDistance = totalBattleDistance + totalSingleDistance;
final long totalRunningTime = totalBattleRunningTime + totalSingleRunningTime;

int totalHour = (int) totalRunningTime / 3600;
int totalMinute = (int) (totalRunningTime % 3600) / 60;
int totalSecond = (int) totalRunningTime % 60;

return new MyPageTotalResponse(totalDistance, totalDistance / totalRunningTime, new RunningTimeResponse(totalHour, totalMinute, totalSecond));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package online.partyrun.partyrunbattleservice.domain.mypage.service;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import online.partyrun.partyrunbattleservice.domain.battle.entity.Battle;
import online.partyrun.partyrunbattleservice.domain.battle.repository.BattleRepository;
import online.partyrun.partyrunbattleservice.domain.mypage.dto.MyPageTotalResponse;
import online.partyrun.partyrunbattleservice.domain.single.entity.Single;
import online.partyrun.partyrunbattleservice.domain.single.repository.SingleRepository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
public class MyPageService {

BattleRepository battleRepository;
SingleRepository singleRepository;

public MyPageTotalResponse getMyPageTotal(String memberId) {
final List<Battle> battles = battleRepository.findAllByRunnersIdExceptRunnerRecords(memberId);
final List<Single> singles = singleRepository.findAllByRunnerId(memberId);

return MyPageTotalResponse.of(memberId, battles, singles);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ private boolean isNotCorrectMinutes(int minutes) {
private boolean isNotCorrectSeconds(int seconds) {
return seconds < MIN_TIME || seconds > MAX_TIME;
}

public int getTotalSeconds() {
return hours * 60 * 60 + minutes * 60 + seconds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
import online.partyrun.partyrunbattleservice.domain.single.entity.Single;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

public interface SingleRepository extends MongoRepository<Single, String> {

List<Single> findAllByRunnerId(String memberId);
}

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ spring:

data:
redis:
url: redis://localhost:16379
url: redis://localhost:6379/0

cloud:
aws:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,22 @@ void returnNull() {
);
}
}

@Nested
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class findAllByRunnersIdExceptRunnerRecords는 {

@BeforeEach
void setUp() {
battleRepository.save(new Battle(1000, List.of(박성우, 박현준), now));
battleRepository.save(new Battle(1000, List.of(박성우, 노준혁), now));
}
@Test
@DisplayName("러너가 속한 모든 배틀을 반환한다.")
void returnResult() {
final List<Battle> battles = battleRepository.findAllByRunnersIdExceptRunnerRecords(박성우.getId());

assertThat(battles).hasSize(2);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package online.partyrun.partyrunbattleservice.domain.single.repository;

import online.partyrun.partyrunbattleservice.domain.runner.entity.record.GpsData;
import online.partyrun.partyrunbattleservice.domain.runner.entity.record.RunnerRecord;
import online.partyrun.partyrunbattleservice.domain.single.entity.RunningTime;
import online.partyrun.partyrunbattleservice.domain.single.entity.Single;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;

import java.time.LocalDateTime;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@DataMongoTest
@DisplayName("SingleRepository")
class SingleRepositoryTest {

@Autowired
SingleRepository singleRepository;

String runnerId = "박성우";
LocalDateTime now = LocalDateTime.now();

@Nested
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class findAllByRunnerId는 {

@Test
@DisplayName("모든 싱글 데이터를 조회한다.")
void returnSingles() {
final List<Single> singleData = List.of(
new Single(runnerId, new RunningTime(0, 0, 1), List.of(new RunnerRecord(GpsData.of(1, 1, 1, now), 0))),
new Single(runnerId, new RunningTime(0, 0, 1), List.of(new RunnerRecord(GpsData.of(1, 1, 1, now), 0)))
);

singleRepository.saveAll(singleData);

final List<Single> singleResult = singleRepository.findAllByRunnerId(runnerId);

assertThat(singleResult).hasSize(2);
}
}
}

0 comments on commit 8e0df12

Please sign in to comment.