Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: 나의 기록 조회 V2 작업(퍼센테이지 값 추가) #299

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.dnd.runus.application.member;

import com.dnd.runus.domain.level.Level;
import com.dnd.runus.domain.member.Member;
import com.dnd.runus.domain.member.MemberLevel;
import com.dnd.runus.domain.member.MemberLevelRepository;
Expand Down Expand Up @@ -28,10 +27,11 @@ public MyProfileResponse getMyProfile(long memberId) {

return new MyProfileResponse(
memberCurrentLevel.level().imageUrl(),
Level.formatLevelName(currentLevel),
Level.formatExp(memberCurrentLevel.currentExp()),
Level.formatLevelName(nextLevel),
Level.formatExp(memberCurrentLevel.level().expRangeEnd() - memberCurrentLevel.currentExp()));
currentLevel,
memberCurrentLevel.currentExp(),
nextLevel,
memberCurrentLevel.level().expRangeStart(),
memberCurrentLevel.level().expRangeEnd());
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.dnd.runus.application.member.MemberService;
import com.dnd.runus.presentation.annotation.MemberId;
import com.dnd.runus.presentation.v1.member.dto.response.MyProfileResponse;
import com.dnd.runus.presentation.v1.member.dto.response.MyProfileResponseV1;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -18,7 +18,7 @@ public class MemberProfileController {

@GetMapping("me")
@ResponseStatus(HttpStatus.OK)
public MyProfileResponse getMyProfile(@MemberId long memberId) {
return memberService.getMyProfile(memberId);
public MyProfileResponseV1 getMyProfile(@MemberId long memberId) {
return MyProfileResponseV1.from(memberService.getMyProfile(memberId));
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package com.dnd.runus.presentation.v1.member.dto.response;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

나중에 service 계층에서는 presentation에 의존하지 않도록 따로 수정하면 좋을 것 같아요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#301 이슈에서 해당 작업을 진행합니다.


import io.swagger.v3.oas.annotations.media.Schema;

public record MyProfileResponse(
@Schema(description = "현재 프로필 이미지")
String profileImageUrl,
@Schema(description = "현재 레벨 이름")
String currentLevelName,
@Schema(description = "지금까지 달린 거리")
String currentKm,
@Schema(description = "다음 레벨 이름")
String nextLevelName,
@Schema(description = "다음 레벨까지 남은 거리")
String nextLevelKm
long currentLevel,
int currentExpMeter,
long nextLevel,
int nextLevelStartExpMeter,
int nextLevelEndExpMeter
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.dnd.runus.presentation.v1.member.dto.response;

import com.dnd.runus.domain.level.Level;
import io.swagger.v3.oas.annotations.media.Schema;

public record MyProfileResponseV1(
@Schema(description = "현재 프로필 이미지")
String profileImageUrl,
@Schema(description = "현재 레벨 이름")
String currentLevelName,
@Schema(description = "지금까지 달린 거리")
String currentKm,
@Schema(description = "다음 레벨 이름")
String nextLevelName,
@Schema(description = "다음 레벨까지 남은 거리")
String nextLevelKm
) {

public static MyProfileResponseV1 from(MyProfileResponse myProfileResponse) {
return new MyProfileResponseV1(
myProfileResponse.profileImageUrl(),
Level.formatLevelName(myProfileResponse.currentLevel()),
Level.formatExp(myProfileResponse.currentExpMeter()),
Level.formatLevelName(myProfileResponse.nextLevel()),
Level.formatExp(myProfileResponse.nextLevelEndExpMeter()
- myProfileResponse.currentExpMeter())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.dnd.runus.presentation.v2.member;

import com.dnd.runus.application.member.MemberService;
import com.dnd.runus.presentation.annotation.MemberId;
import com.dnd.runus.presentation.v1.member.dto.response.MyProfileResponse;
import com.dnd.runus.presentation.v2.member.dto.response.MyProfileResponseV2;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("api/v2/members/profiles")
public class MemberProfileControllerV2 {
private final MemberService memberService;

@GetMapping("me")
@ResponseStatus(HttpStatus.OK)
public MyProfileResponseV2 getMyProfile(@MemberId long memberId) {
MyProfileResponse myProfile = memberService.getMyProfile(memberId);

// 퍼센테이지
double leftNextLevelKm = myProfile.nextLevelEndExpMeter() - myProfile.currentExpMeter();
double divisor = myProfile.nextLevelEndExpMeter() - myProfile.nextLevelStartExpMeter();
double percentage = 1 - (leftNextLevelKm / divisor);

return MyProfileResponseV2.of(myProfile, percentage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.dnd.runus.presentation.v2.member.dto.response;


import com.dnd.runus.domain.level.Level;
import com.dnd.runus.presentation.v1.member.dto.response.MyProfileResponse;
import io.swagger.v3.oas.annotations.media.Schema;

public record MyProfileResponseV2 (
@Schema(description = "현재 프로필 이미지")
String profileImageUrl,
@Schema(description = "현재 레벨 이름")
String currentLevelName,
@Schema(description = "지금까지 달린 거리")
String currentKm,
@Schema(description = "다음 레벨 이름")
String nextLevelName,
@Schema(description = "다음 레벨까지 남은 거리")
String nextLevelKm,
@Schema(description = "퍼센테이지값", example = "0.728")
double percentage

) {

public static MyProfileResponseV2 of(MyProfileResponse myProfileResponse, double percentage) {
return new MyProfileResponseV2(
myProfileResponse.profileImageUrl(),
Level.formatLevelName(myProfileResponse.currentLevel()),
Level.formatExp(myProfileResponse.currentExpMeter()),
Level.formatLevelName(myProfileResponse.nextLevel()),
Level.formatExp(myProfileResponse.nextLevelEndExpMeter()
- myProfileResponse.currentExpMeter()),
percentage
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MemberServiceTest {
@Mock
private MemberLevelRepository memberLevelRepository;

@DisplayName("내 프로필 조회 성공 - 현재 레벨 1, 경험치 500일때, 0km로 표현 (소수점 버림)")
@DisplayName("내 프로필 조회 성공 - 현재 레벨 1, 경험치 500일때, 다음 레벨이 2여야함")
@Test
void getMyProfile_givenCurrentLevel1AndExp500_thenSuccess() {
// given
Expand All @@ -35,26 +35,8 @@ void getMyProfile_givenCurrentLevel1AndExp500_thenSuccess() {

// then
assertEquals("imageUrl", myProfileResponse.profileImageUrl());
assertEquals("Level 1", myProfileResponse.currentLevelName());
assertEquals("0.5km", myProfileResponse.currentKm());
assertEquals("Level 2", myProfileResponse.nextLevelName());
}

@DisplayName("내 프로필 조회 성공 - 현재 레벨 2, 경험치 1500일때, 1km로 표현 (소수점 버림)")
@Test
void getMyProfile_givenCurrentLevel1AndExp1500_thenSuccess() {
// given
long memberId = 1L;
Level level = new Level(2, 1000, 2000, "imageUrl");
given(memberLevelRepository.findByMemberIdWithLevel(memberId)).willReturn(new MemberLevel.Current(level, 1500));

// when
MyProfileResponse myProfileResponse = memberService.getMyProfile(memberId);

// then
assertEquals("imageUrl", myProfileResponse.profileImageUrl());
assertEquals("Level 2", myProfileResponse.currentLevelName());
assertEquals("1.5km", myProfileResponse.currentKm());
assertEquals("Level 3", myProfileResponse.nextLevelName());
assertEquals(1, myProfileResponse.currentLevel());
assertEquals(500, myProfileResponse.currentExpMeter());
assertEquals(2, myProfileResponse.nextLevel());
}
}