From f33cc2f61dc35c7fbb3f2830f667500875f91bda Mon Sep 17 00:00:00 2001 From: parksey Date: Tue, 28 Nov 2023 15:50:12 +0900 Subject: [PATCH 01/11] =?UTF-8?q?feat:=20=ED=9A=8C=EC=9B=90=EC=9D=98=20?= =?UTF-8?q?=EB=9E=AD=ED=82=B9=20redis=EC=97=90=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EB=B0=8F=20=EC=82=AD=EC=A0=9C,=20=EC=97=85=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=8A=B8=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/application/member/MemberMapper.java | 9 +++++ .../api/application/member/MemberService.java | 16 +++++++-- .../application/ranking/RankingService.java | 36 +++++++++++++++++++ .../com/moabam/api/domain/member/Member.java | 1 - .../moabam/api/dto/ranking/RankingInfo.java | 12 +++++++ .../redis/ZSetRedisRepository.java | 25 +++++++++++-- .../api/presentation/RoomController.java | 3 ++ 7 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/moabam/api/application/ranking/RankingService.java create mode 100644 src/main/java/com/moabam/api/dto/ranking/RankingInfo.java diff --git a/src/main/java/com/moabam/api/application/member/MemberMapper.java b/src/main/java/com/moabam/api/application/member/MemberMapper.java index 148a0b8d..600f16f6 100644 --- a/src/main/java/com/moabam/api/application/member/MemberMapper.java +++ b/src/main/java/com/moabam/api/application/member/MemberMapper.java @@ -19,6 +19,7 @@ import com.moabam.api.dto.member.MemberInfo; import com.moabam.api.dto.member.MemberInfoResponse; import com.moabam.api.dto.member.MemberInfoSearchResponse; +import com.moabam.api.dto.ranking.RankingInfo; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -90,4 +91,12 @@ private static Map defaultSkins(String morningImage, String nigh return birdsSkin; } + + public static RankingInfo toRankingInfo(Member member) { + return RankingInfo.builder() + .memberId(member.getId()) + .nickname(member.getNickname()) + .image(member.getProfileImage()) + .build(); + } } diff --git a/src/main/java/com/moabam/api/application/member/MemberService.java b/src/main/java/com/moabam/api/application/member/MemberService.java index b6505ce3..2fd08926 100644 --- a/src/main/java/com/moabam/api/application/member/MemberService.java +++ b/src/main/java/com/moabam/api/application/member/MemberService.java @@ -10,6 +10,7 @@ import org.springframework.transaction.annotation.Transactional; import com.moabam.api.application.auth.mapper.AuthMapper; +import com.moabam.api.application.ranking.RankingService; import com.moabam.api.domain.item.Inventory; import com.moabam.api.domain.item.Item; import com.moabam.api.domain.item.repository.InventoryRepository; @@ -23,6 +24,7 @@ import com.moabam.api.dto.member.MemberInfoResponse; import com.moabam.api.dto.member.MemberInfoSearchResponse; import com.moabam.api.dto.member.ModifyMemberRequest; +import com.moabam.api.dto.ranking.RankingInfo; import com.moabam.global.auth.model.AuthMember; import com.moabam.global.common.util.BaseDataCode; import com.moabam.global.common.util.ClockHolder; @@ -38,6 +40,7 @@ @RequiredArgsConstructor public class MemberService { + private final RankingService rankingService; private final MemberRepository memberRepository; private final InventoryRepository inventoryRepository; private final ItemRepository itemRepository; @@ -72,6 +75,7 @@ public void delete(Member member) { member.delete(clockHolder.times()); memberRepository.flush(); memberRepository.delete(member); + rankingService.removeRanking(MemberMapper.toRankingInfo(member)); } public MemberInfoResponse searchInfo(AuthMember authMember, Long memberId) { @@ -82,21 +86,24 @@ public MemberInfoResponse searchInfo(AuthMember authMember, Long memberId) { searchId = memberId; } MemberInfoSearchResponse memberInfoSearchResponse = findMemberInfo(searchId, isMe); + return MemberMapper.toMemberInfoResponse(memberInfoSearchResponse); } @Transactional public void modifyInfo(AuthMember authMember, ModifyMemberRequest modifyMemberRequest, String newProfileUri) { validateNickname(modifyMemberRequest.nickname()); - Member member = memberSearchRepository.findMember(authMember.id()) .orElseThrow(() -> new NotFoundException(MEMBER_NOT_FOUND)); + RankingInfo beforeInfo = MemberMapper.toRankingInfo(member); member.changeNickName(modifyMemberRequest.nickname()); member.changeIntro(modifyMemberRequest.intro()); member.changeProfileUri(newProfileUri); - memberRepository.save(member); + RankingInfo afterInfo = MemberMapper.toRankingInfo(member); + + rankingService.changeInfos(beforeInfo, afterInfo); } private void validateNickname(String nickname) { @@ -107,7 +114,10 @@ private void validateNickname(String nickname) { private Member signUp(Long socialId) { Member member = MemberMapper.toMember(socialId); - return memberRepository.save(member); + Member savedMember = memberRepository.save(member); + rankingService.addRanking(MemberMapper.toRankingInfo(member), member.getTotalCertifyCount()); + + return savedMember; } private void saveMyEgg(Member member) { diff --git a/src/main/java/com/moabam/api/application/ranking/RankingService.java b/src/main/java/com/moabam/api/application/ranking/RankingService.java new file mode 100644 index 00000000..8460e210 --- /dev/null +++ b/src/main/java/com/moabam/api/application/ranking/RankingService.java @@ -0,0 +1,36 @@ +package com.moabam.api.application.ranking; + +import org.springframework.stereotype.Service; + +import com.moabam.api.application.member.MemberMapper; +import com.moabam.api.domain.member.Member; +import com.moabam.api.dto.ranking.RankingInfo; +import com.moabam.api.dto.room.CertifiedMemberInfo; +import com.moabam.api.infrastructure.redis.ZSetRedisRepository; + +import lombok.RequiredArgsConstructor; + +@Service +@RequiredArgsConstructor +public class RankingService { + + private final ZSetRedisRepository zSetRedisRepository; + + public void addRanking(RankingInfo rankingInfo, Long totalCertifyCount) { + zSetRedisRepository.add("Ranking", rankingInfo, totalCertifyCount); + } + + public void updateCacheScore(CertifiedMemberInfo info) { + Member member = info.member(); + RankingInfo rankingInfo = MemberMapper.toRankingInfo(member); + zSetRedisRepository.add("Ranking", rankingInfo, member.getTotalCertifyCount()); + } + + public void changeInfos(RankingInfo before, RankingInfo after) { + zSetRedisRepository.changeMember("Ranking", before, after); + } + + public void removeRanking(RankingInfo rankingInfo) { + zSetRedisRepository.delete("Ranking", rankingInfo); + } +} diff --git a/src/main/java/com/moabam/api/domain/member/Member.java b/src/main/java/com/moabam/api/domain/member/Member.java index 3cfc9f24..b2ede937 100644 --- a/src/main/java/com/moabam/api/domain/member/Member.java +++ b/src/main/java/com/moabam/api/domain/member/Member.java @@ -146,7 +146,6 @@ public void changeIntro(String intro) { public void changeProfileUri(String newProfileUri) { this.profileImage = requireNonNullElse(newProfileUri, profileImage); - this.profileImage = requireNonNullElse(newProfileUri, profileImage); } public void changeDefaultSkintUrl(Item item) throws NotFoundException { diff --git a/src/main/java/com/moabam/api/dto/ranking/RankingInfo.java b/src/main/java/com/moabam/api/dto/ranking/RankingInfo.java new file mode 100644 index 00000000..46645389 --- /dev/null +++ b/src/main/java/com/moabam/api/dto/ranking/RankingInfo.java @@ -0,0 +1,12 @@ +package com.moabam.api.dto.ranking; + +import lombok.Builder; + +@Builder +public record RankingInfo( + Long memberId, + String nickname, + String image +) { + +} diff --git a/src/main/java/com/moabam/api/infrastructure/redis/ZSetRedisRepository.java b/src/main/java/com/moabam/api/infrastructure/redis/ZSetRedisRepository.java index 9ac7c607..5dce1e42 100644 --- a/src/main/java/com/moabam/api/infrastructure/redis/ZSetRedisRepository.java +++ b/src/main/java/com/moabam/api/infrastructure/redis/ZSetRedisRepository.java @@ -18,9 +18,7 @@ public class ZSetRedisRepository { public void addIfAbsent(String key, Object value, double score) { if (redisTemplate.opsForZSet().score(key, value) == null) { - redisTemplate - .opsForZSet() - .add(requireNonNull(key), requireNonNull(value), score); + add(key, value, score); } } @@ -29,4 +27,25 @@ public Set> popMin(String key, long count) { .opsForZSet() .popMin(key, count); } + + public void add(String key, Object value, double score) { + redisTemplate + .opsForZSet() + .add(requireNonNull(key), requireNonNull(value), score); + } + + public void changeMember(String key, Object before, Object after) { + Double score = redisTemplate.opsForZSet().score(key, before); + + if (score == null) { + return; + } + + delete(key, before); + add(key, after, score); + } + + public void delete(String key, Object value) { + redisTemplate.opsForZSet().remove(key, value); + } } diff --git a/src/main/java/com/moabam/api/presentation/RoomController.java b/src/main/java/com/moabam/api/presentation/RoomController.java index ce4725e3..370d050e 100644 --- a/src/main/java/com/moabam/api/presentation/RoomController.java +++ b/src/main/java/com/moabam/api/presentation/RoomController.java @@ -18,6 +18,7 @@ import org.springframework.web.multipart.MultipartFile; import com.moabam.api.application.image.ImageService; +import com.moabam.api.application.ranking.RankingService; import com.moabam.api.application.room.CertificationService; import com.moabam.api.application.room.RoomService; import com.moabam.api.application.room.SearchService; @@ -48,6 +49,7 @@ public class RoomController { private final SearchService searchService; private final CertificationService certificationService; private final ImageService imageService; + private final RankingService rankingService; @PostMapping @ResponseStatus(HttpStatus.CREATED) @@ -114,6 +116,7 @@ public void certifyRoom(@Auth AuthMember authMember, @PathVariable("roomId") Lon List imageUrls = imageService.uploadImages(multipartFiles, ImageType.CERTIFICATION); CertifiedMemberInfo info = certificationService.getCertifiedMemberInfo(authMember.id(), roomId, imageUrls); certificationService.certifyRoom(info); + rankingService.updateCacheScore(info); } @PutMapping("/{roomId}/members/{memberId}/mandate") From 490b4a997fa27a59f4a892716fb2de6caea6e3de Mon Sep 17 00:00:00 2001 From: parksey Date: Wed, 29 Nov 2023 00:42:28 +0900 Subject: [PATCH 02/11] =?UTF-8?q?test:=20=ED=9A=8C=EC=9B=90=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=EC=82=AD=EC=A0=9C?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80=EC=97=90=20=EB=94=B0=EB=A5=B8=20=EB=9E=AD?= =?UTF-8?q?=ED=82=B9=20=EC=B0=B8=EC=97=AC,=20=EC=A0=9C=EC=99=B8=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/member/MemberServiceTest.java | 4 + .../ranking/RankingServiceTest.java | 163 ++++++++++++++++++ .../presentation/MemberControllerTest.java | 23 +++ .../support/fixture/RankingInfoFixture.java | 5 + 4 files changed, 195 insertions(+) create mode 100644 src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java create mode 100644 src/test/java/com/moabam/support/fixture/RankingInfoFixture.java diff --git a/src/test/java/com/moabam/api/application/member/MemberServiceTest.java b/src/test/java/com/moabam/api/application/member/MemberServiceTest.java index a7959641..20dc1234 100644 --- a/src/test/java/com/moabam/api/application/member/MemberServiceTest.java +++ b/src/test/java/com/moabam/api/application/member/MemberServiceTest.java @@ -16,6 +16,7 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import com.moabam.api.application.ranking.RankingService; import com.moabam.api.domain.item.Inventory; import com.moabam.api.domain.item.Item; import com.moabam.api.domain.item.repository.InventoryRepository; @@ -60,6 +61,9 @@ class MemberServiceTest { @Mock InventoryRepository inventoryRepository; + @Mock + RankingService rankingService; + @Mock ItemRepository itemRepository; diff --git a/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java b/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java new file mode 100644 index 00000000..01b967dc --- /dev/null +++ b/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java @@ -0,0 +1,163 @@ +package com.moabam.api.application.ranking; + +import static org.assertj.core.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.redis.core.RedisTemplate; + +import com.moabam.api.application.member.MemberMapper; +import com.moabam.api.domain.member.Member; +import com.moabam.api.dto.ranking.RankingInfo; +import com.moabam.api.dto.room.CertifiedMemberInfo; +import com.moabam.api.infrastructure.redis.ZSetRedisRepository; +import com.moabam.global.config.EmbeddedRedisConfig; +import com.moabam.support.fixture.BugFixture; + +@SpringBootTest(classes = {EmbeddedRedisConfig.class, RankingService.class, ZSetRedisRepository.class}) +public class RankingServiceTest { + + @Autowired + ZSetRedisRepository zSetRedisRepository; + + @Autowired + RedisTemplate redisTemplate; + + @Autowired + RankingService rankingService; + + @DisplayName("redis에 추가") + @Nested + class Add { + + @DisplayName("성공") + @Test + void add_success() { + // given + Long totalCertifyCount = 0L; + RankingInfo rankingInfo = RankingInfo.builder() + .image("https://image.moabam.com/test") + .memberId(1L) + .nickname("nickname") + .build(); + + // when + rankingService.addRanking(rankingInfo, totalCertifyCount); + + // then + Double resultDouble = + redisTemplate.opsForZSet().score("Ranking", rankingInfo); + + assertAll( + () -> assertThat(resultDouble).isNotNull(), + () -> assertThat(resultDouble).isEqualTo(Double.valueOf(totalCertifyCount)) + ); + } + } + + @DisplayName("스코어 업데이트") + @Nested + class Update { + + @DisplayName("성공") + @Test + void update_success() { + // given + Long totalCertifyCount = 0L; + Member member = Member.builder() + .socialId("1") + .bug(BugFixture.bug()) + .build(); + member.increaseTotalCertifyCount(); + member.increaseTotalCertifyCount(); + Long expect = member.getTotalCertifyCount(); + CertifiedMemberInfo certifiedMemberInfo = CertifiedMemberInfo.builder() + .member(member) + .build(); + RankingInfo result = MemberMapper.toRankingInfo(member); + + // when + rankingService.addRanking(result, totalCertifyCount); + rankingService.updateCacheScore(certifiedMemberInfo); + Double resultDouble = + redisTemplate.opsForZSet().score("Ranking", result); + + // then + assertAll( + () -> assertThat(resultDouble).isNotNull(), + () -> assertThat(resultDouble).isEqualTo(Double.valueOf(expect)) + ); + } + } + + @DisplayName("사용자 정보 변경") + @Nested + class Change { + + @DisplayName("성공") + @Test + void update_success() { + // given + Member member = Member.builder() + .socialId("1") + .bug(BugFixture.bug()) + .build(); + member.increaseTotalCertifyCount(); + member.increaseTotalCertifyCount(); + Long expect = member.getTotalCertifyCount(); + CertifiedMemberInfo certifiedMemberInfo = CertifiedMemberInfo.builder() + .member(member) + .build(); + RankingInfo before = MemberMapper.toRankingInfo(member); + + // when + rankingService.updateCacheScore(certifiedMemberInfo); + member.changeIntro("밥세공기"); + RankingInfo changeInfo = MemberMapper.toRankingInfo(member); + rankingService.changeInfos(before, changeInfo); + + Double resultDouble = + redisTemplate.opsForZSet().score("Ranking", changeInfo); + + // then + assertAll( + () -> assertThat(resultDouble).isNotNull(), + () -> assertThat(resultDouble).isEqualTo(Double.valueOf(expect)) + ); + } + } + + @DisplayName("랭킹 삭제") + @Nested + class Delete { + + @DisplayName("성공") + @Test + void update_success() { + // given + Long totalCertify = 5L; + Member member = Member.builder() + .socialId("1") + .bug(BugFixture.bug()) + .build(); + member.increaseTotalCertifyCount(); + member.increaseTotalCertifyCount(); + RankingInfo rankingInfo = MemberMapper.toRankingInfo(member); + + rankingService.addRanking(rankingInfo, totalCertify); + + // when + rankingService.removeRanking(rankingInfo); + + Double resultDouble = + redisTemplate.opsForZSet().score("Ranking", rankingInfo); + + // then + assertThat(resultDouble).isNull(); + } + } +} diff --git a/src/test/java/com/moabam/api/presentation/MemberControllerTest.java b/src/test/java/com/moabam/api/presentation/MemberControllerTest.java index 4c19555c..8fd6e465 100644 --- a/src/test/java/com/moabam/api/presentation/MemberControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/MemberControllerTest.java @@ -12,6 +12,7 @@ import java.nio.charset.StandardCharsets; import java.util.List; +import java.util.Objects; import java.util.Optional; import org.assertj.core.api.Assertions; @@ -29,6 +30,8 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Import; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; @@ -45,6 +48,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.moabam.api.application.auth.OAuth2AuthorizationServerRequestService; import com.moabam.api.application.image.ImageService; +import com.moabam.api.application.member.MemberMapper; import com.moabam.api.domain.auth.repository.TokenRepository; import com.moabam.api.domain.image.ImageType; import com.moabam.api.domain.item.Inventory; @@ -63,6 +67,8 @@ import com.moabam.api.domain.room.repository.RoomRepository; import com.moabam.api.dto.auth.TokenSaveValue; import com.moabam.api.dto.member.ModifyMemberRequest; +import com.moabam.api.dto.ranking.RankingInfo; +import com.moabam.global.config.EmbeddedRedisConfig; import com.moabam.global.config.OAuthConfig; import com.moabam.global.error.exception.UnauthorizedException; import com.moabam.global.error.handler.RestTemplateResponseHandler; @@ -83,6 +89,7 @@ @AutoConfigureMockMvc @AutoConfigureMockRestServiceServer @TestInstance(TestInstance.Lifecycle.PER_CLASS) +@Import(EmbeddedRedisConfig.class) class MemberControllerTest extends WithoutFilterSupporter { @Autowired @@ -133,6 +140,9 @@ class MemberControllerTest extends WithoutFilterSupporter { @Autowired EntityManager entityManager; + @Autowired + RedisTemplate redisTemplate; + @BeforeAll void allSetUp() { restTemplateBuilder = new RestTemplateBuilder() @@ -481,6 +491,7 @@ void member_modify_request_success(String intro, String nickname) throws Excepti .characterEncoding("UTF-8")) .andExpect(status().is2xxSuccessful()) .andDo(print()); + } @DisplayName("회원 프로필없이 성공 ") @@ -499,6 +510,8 @@ void member_modify_no_image_request_success(String intro, String nickname) throw willThrow(NullPointerException.class) .given(imageService).uploadImages(any(), any()); + RankingInfo rankingInfo = MemberMapper.toRankingInfo(member); + redisTemplate.opsForZSet().add("Ranking", rankingInfo, member.getTotalCertifyCount()); // expected mockMvc.perform(multipart(HttpMethod.POST, "/members/modify") @@ -508,5 +521,15 @@ void member_modify_no_image_request_success(String intro, String nickname) throw .characterEncoding("UTF-8")) .andExpect(status().is2xxSuccessful()) .andDo(print()); + + String updateNick = member.getNickname(); + + if (Objects.nonNull(nickname)) { + updateNick = nickname; + } + + Double result = redisTemplate.opsForZSet() + .score("Ranking", new RankingInfo(member.getId(), updateNick, member.getProfileImage())); + assertThat(result).isEqualTo(member.getTotalCertifyCount()); } } diff --git a/src/test/java/com/moabam/support/fixture/RankingInfoFixture.java b/src/test/java/com/moabam/support/fixture/RankingInfoFixture.java new file mode 100644 index 00000000..9058ebcc --- /dev/null +++ b/src/test/java/com/moabam/support/fixture/RankingInfoFixture.java @@ -0,0 +1,5 @@ +package com.moabam.support.fixture; + +public class RankingInfoFixture { + +} From 51fe35335ae5dea66f049a3c0fd83a18a684c8c5 Mon Sep 17 00:00:00 2001 From: parksey Date: Wed, 29 Nov 2023 13:54:04 +0900 Subject: [PATCH 03/11] =?UTF-8?q?feat:=20=EB=9E=AD=ED=82=B9=EC=8B=9C?= =?UTF-8?q?=EC=8A=A4=ED=85=9C=20API=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20?= =?UTF-8?q?=EB=9E=AD=ED=82=B9=20=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/application/member/MemberMapper.java | 10 ++ .../api/application/member/MemberService.java | 7 ++ .../application/ranking/RankingMapper.java | 43 +++++++++ .../application/ranking/RankingService.java | 51 ++++++++++- .../api/dto/ranking/PersonalRankingInfo.java | 13 +++ .../dto/ranking/TopRankingInfoResponse.java | 14 +++ .../api/dto/ranking/TopRankingResponses.java | 13 +++ .../redis/ZSetRedisRepository.java | 18 ++++ .../api/presentation/RankingController.java | 32 +++++++ .../global/config/EmbeddedRedisConfig.java | 6 ++ .../com/moabam/global/config/RedisConfig.java | 7 ++ .../ranking/RankingServiceTest.java | 91 +++++++++++++++++++ 12 files changed, 301 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/moabam/api/application/ranking/RankingMapper.java create mode 100644 src/main/java/com/moabam/api/dto/ranking/PersonalRankingInfo.java create mode 100644 src/main/java/com/moabam/api/dto/ranking/TopRankingInfoResponse.java create mode 100644 src/main/java/com/moabam/api/dto/ranking/TopRankingResponses.java create mode 100644 src/main/java/com/moabam/api/presentation/RankingController.java diff --git a/src/main/java/com/moabam/api/application/member/MemberMapper.java b/src/main/java/com/moabam/api/application/member/MemberMapper.java index 600f16f6..66d9baa4 100644 --- a/src/main/java/com/moabam/api/application/member/MemberMapper.java +++ b/src/main/java/com/moabam/api/application/member/MemberMapper.java @@ -19,6 +19,7 @@ import com.moabam.api.dto.member.MemberInfo; import com.moabam.api.dto.member.MemberInfoResponse; import com.moabam.api.dto.member.MemberInfoSearchResponse; +import com.moabam.api.dto.ranking.PersonalRankingInfo; import com.moabam.api.dto.ranking.RankingInfo; import lombok.AccessLevel; @@ -99,4 +100,13 @@ public static RankingInfo toRankingInfo(Member member) { .image(member.getProfileImage()) .build(); } + + public static PersonalRankingInfo toRankingInfoWithScore(Member member) { + return PersonalRankingInfo.builder() + .score(member.getTotalCertifyCount()) + .memberId(member.getId()) + .nickname(member.getNickname()) + .image(member.getProfileImage()) + .build(); + } } diff --git a/src/main/java/com/moabam/api/application/member/MemberService.java b/src/main/java/com/moabam/api/application/member/MemberService.java index 8ecdec82..6d1c84bc 100644 --- a/src/main/java/com/moabam/api/application/member/MemberService.java +++ b/src/main/java/com/moabam/api/application/member/MemberService.java @@ -24,6 +24,7 @@ import com.moabam.api.dto.member.MemberInfoResponse; import com.moabam.api.dto.member.MemberInfoSearchResponse; import com.moabam.api.dto.member.ModifyMemberRequest; +import com.moabam.api.dto.ranking.PersonalRankingInfo; import com.moabam.api.dto.ranking.RankingInfo; import com.moabam.global.auth.model.AuthMember; import com.moabam.global.common.util.BaseDataCode; @@ -106,6 +107,12 @@ public void modifyInfo(AuthMember authMember, ModifyMemberRequest modifyMemberRe rankingService.changeInfos(beforeInfo, afterInfo); } + public PersonalRankingInfo getRankingInfo(AuthMember authMember) { + Member member = findMember(authMember.id()); + + return MemberMapper.toRankingInfoWithScore(member); + } + private void validateNickname(String nickname) { if (StringUtils.isEmpty(nickname) && memberRepository.existsByNickname(nickname)) { throw new ConflictException(NICKNAME_CONFLICT); diff --git a/src/main/java/com/moabam/api/application/ranking/RankingMapper.java b/src/main/java/com/moabam/api/application/ranking/RankingMapper.java new file mode 100644 index 00000000..ad9c0924 --- /dev/null +++ b/src/main/java/com/moabam/api/application/ranking/RankingMapper.java @@ -0,0 +1,43 @@ +package com.moabam.api.application.ranking; + +import java.util.List; + +import com.moabam.api.dto.ranking.PersonalRankingInfo; +import com.moabam.api.dto.ranking.RankingInfo; +import com.moabam.api.dto.ranking.TopRankingInfoResponse; +import com.moabam.api.dto.ranking.TopRankingResponses; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class RankingMapper { + + public static TopRankingInfoResponse topRankingResponse(int rank, long score, RankingInfo rankInfo) { + return TopRankingInfoResponse.builder() + .rank(rank) + .score(score) + .nickname(rankInfo.nickname()) + .image(rankInfo.image()) + .memberId(rankInfo.memberId()) + .build(); + } + + public static TopRankingInfoResponse topRankingResponse(int rank, PersonalRankingInfo rankInfo) { + return TopRankingInfoResponse.builder() + .rank(rank) + .score(rankInfo.score()) + .nickname(rankInfo.nickname()) + .image(rankInfo.image()) + .memberId(rankInfo.memberId()) + .build(); + } + + public static TopRankingResponses topRankingResponses(TopRankingInfoResponse myRanking, + List topRankings) { + return TopRankingResponses.builder() + .topRankings(topRankings) + .myRanking(myRanking) + .build(); + } +} diff --git a/src/main/java/com/moabam/api/application/ranking/RankingService.java b/src/main/java/com/moabam/api/application/ranking/RankingService.java index 8460e210..075e44b8 100644 --- a/src/main/java/com/moabam/api/application/ranking/RankingService.java +++ b/src/main/java/com/moabam/api/application/ranking/RankingService.java @@ -1,10 +1,22 @@ package com.moabam.api.application.ranking; +import static java.util.Objects.*; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.springframework.data.redis.core.ZSetOperations; import org.springframework.stereotype.Service; +import com.fasterxml.jackson.databind.ObjectMapper; import com.moabam.api.application.member.MemberMapper; import com.moabam.api.domain.member.Member; +import com.moabam.api.dto.ranking.PersonalRankingInfo; import com.moabam.api.dto.ranking.RankingInfo; +import com.moabam.api.dto.ranking.TopRankingInfoResponse; +import com.moabam.api.dto.ranking.TopRankingResponses; import com.moabam.api.dto.room.CertifiedMemberInfo; import com.moabam.api.infrastructure.redis.ZSetRedisRepository; @@ -14,23 +26,54 @@ @RequiredArgsConstructor public class RankingService { + private static final String RANKING = "Ranking"; + private static final int START_INDEX = 0; + private static final int LIMIT_INDEX = 10; + + private final ObjectMapper objectMapper; private final ZSetRedisRepository zSetRedisRepository; public void addRanking(RankingInfo rankingInfo, Long totalCertifyCount) { - zSetRedisRepository.add("Ranking", rankingInfo, totalCertifyCount); + zSetRedisRepository.add(RANKING, rankingInfo, totalCertifyCount); } public void updateCacheScore(CertifiedMemberInfo info) { Member member = info.member(); RankingInfo rankingInfo = MemberMapper.toRankingInfo(member); - zSetRedisRepository.add("Ranking", rankingInfo, member.getTotalCertifyCount()); + zSetRedisRepository.add(RANKING, rankingInfo, member.getTotalCertifyCount()); } public void changeInfos(RankingInfo before, RankingInfo after) { - zSetRedisRepository.changeMember("Ranking", before, after); + zSetRedisRepository.changeMember(RANKING, before, after); } public void removeRanking(RankingInfo rankingInfo) { - zSetRedisRepository.delete("Ranking", rankingInfo); + zSetRedisRepository.delete(RANKING, rankingInfo); + } + + public TopRankingResponses getMemberRanking(PersonalRankingInfo myRankingInfo) { + List topRankings = getTopRankings(); + Long myRanking = zSetRedisRepository.rank("Ranking", myRankingInfo); + TopRankingInfoResponse myRankingInfoResponse = RankingMapper.topRankingResponse(myRanking.intValue(), + myRankingInfo); + + return RankingMapper.topRankingResponses(myRankingInfoResponse, topRankings); + } + + private List getTopRankings() { + Set> topRankings = + zSetRedisRepository.range(RANKING, START_INDEX, LIMIT_INDEX); + + Set scoreSet = new HashSet<>(); + List topRankingInfoRespons = new ArrayList<>(); + + for (ZSetOperations.TypedTuple topRanking : topRankings) { + long score = requireNonNull(topRanking.getScore()).longValue(); + scoreSet.add(score); + + RankingInfo rankingInfo = objectMapper.convertValue(topRanking.getValue(), RankingInfo.class); + topRankingInfoRespons.add(RankingMapper.topRankingResponse(scoreSet.size(), score, rankingInfo)); + } + return topRankingInfoRespons; } } diff --git a/src/main/java/com/moabam/api/dto/ranking/PersonalRankingInfo.java b/src/main/java/com/moabam/api/dto/ranking/PersonalRankingInfo.java new file mode 100644 index 00000000..8a8ffc8d --- /dev/null +++ b/src/main/java/com/moabam/api/dto/ranking/PersonalRankingInfo.java @@ -0,0 +1,13 @@ +package com.moabam.api.dto.ranking; + +import lombok.Builder; + +@Builder +public record PersonalRankingInfo( + Long memberId, + String nickname, + String image, + Long score +) { + +} diff --git a/src/main/java/com/moabam/api/dto/ranking/TopRankingInfoResponse.java b/src/main/java/com/moabam/api/dto/ranking/TopRankingInfoResponse.java new file mode 100644 index 00000000..ed0d3857 --- /dev/null +++ b/src/main/java/com/moabam/api/dto/ranking/TopRankingInfoResponse.java @@ -0,0 +1,14 @@ +package com.moabam.api.dto.ranking; + +import lombok.Builder; + +@Builder +public record TopRankingInfoResponse( + int rank, + Long memberId, + Long score, + String nickname, + String image +) { + +} diff --git a/src/main/java/com/moabam/api/dto/ranking/TopRankingResponses.java b/src/main/java/com/moabam/api/dto/ranking/TopRankingResponses.java new file mode 100644 index 00000000..da1dd863 --- /dev/null +++ b/src/main/java/com/moabam/api/dto/ranking/TopRankingResponses.java @@ -0,0 +1,13 @@ +package com.moabam.api.dto.ranking; + +import java.util.List; + +import lombok.Builder; + +@Builder +public record TopRankingResponses( + List topRankings, + TopRankingInfoResponse myRanking +) { + +} diff --git a/src/main/java/com/moabam/api/infrastructure/redis/ZSetRedisRepository.java b/src/main/java/com/moabam/api/infrastructure/redis/ZSetRedisRepository.java index 5dce1e42..93934045 100644 --- a/src/main/java/com/moabam/api/infrastructure/redis/ZSetRedisRepository.java +++ b/src/main/java/com/moabam/api/infrastructure/redis/ZSetRedisRepository.java @@ -5,7 +5,9 @@ import java.util.Set; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ZSetOperations; import org.springframework.data.redis.core.ZSetOperations.TypedTuple; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.stereotype.Repository; import lombok.RequiredArgsConstructor; @@ -48,4 +50,20 @@ public void changeMember(String key, Object before, Object after) { public void delete(String key, Object value) { redisTemplate.opsForZSet().remove(key, value); } + + public Set> range(String key, int startIndex, int limitIndex) { + setSerialize(Object.class); + Set> rankings = redisTemplate.opsForZSet() + .reverseRangeWithScores(key, startIndex, limitIndex); + setSerialize(String.class); + return rankings; + } + + private void setSerialize(Class classes) { + redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(classes)); + } + + public Long rank(String key, Object myRankingInfo) { + return redisTemplate.opsForZSet().reverseRank(key, myRankingInfo); + } } diff --git a/src/main/java/com/moabam/api/presentation/RankingController.java b/src/main/java/com/moabam/api/presentation/RankingController.java new file mode 100644 index 00000000..9820454e --- /dev/null +++ b/src/main/java/com/moabam/api/presentation/RankingController.java @@ -0,0 +1,32 @@ +package com.moabam.api.presentation; + +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; + +import com.moabam.api.application.member.MemberService; +import com.moabam.api.application.ranking.RankingService; +import com.moabam.api.dto.ranking.PersonalRankingInfo; +import com.moabam.api.dto.ranking.TopRankingResponses; +import com.moabam.global.auth.annotation.Auth; +import com.moabam.global.auth.model.AuthMember; + +import lombok.RequiredArgsConstructor; + +@RequestMapping("/ranking") +@RestController +@RequiredArgsConstructor +public class RankingController { + + private final RankingService rankingService; + private final MemberService memberService; + + @GetMapping + @ResponseStatus(HttpStatus.OK) + public TopRankingResponses getRanking(@Auth AuthMember authMember) { + PersonalRankingInfo rankingInfo = memberService.getRankingInfo(authMember); + return rankingService.getMemberRanking(rankingInfo); + } +} diff --git a/src/main/java/com/moabam/global/config/EmbeddedRedisConfig.java b/src/main/java/com/moabam/global/config/EmbeddedRedisConfig.java index 02268602..098109bc 100644 --- a/src/main/java/com/moabam/global/config/EmbeddedRedisConfig.java +++ b/src/main/java/com/moabam/global/config/EmbeddedRedisConfig.java @@ -17,6 +17,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.util.StringUtils; +import com.fasterxml.jackson.databind.ObjectMapper; import com.moabam.global.error.exception.MoabamException; import com.moabam.global.error.model.ErrorMessage; @@ -60,6 +61,11 @@ public RedisTemplate redisTemplate(RedisConnectionFactory redisC return redisTemplate; } + @Bean + public ObjectMapper objectMapper() { + return new ObjectMapper(); + } + public void startRedis() { Os os = Os.createOs(); availablePort = findPort(os); diff --git a/src/main/java/com/moabam/global/config/RedisConfig.java b/src/main/java/com/moabam/global/config/RedisConfig.java index 9d017cce..31105b5c 100644 --- a/src/main/java/com/moabam/global/config/RedisConfig.java +++ b/src/main/java/com/moabam/global/config/RedisConfig.java @@ -10,6 +10,8 @@ import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; +import com.fasterxml.jackson.databind.ObjectMapper; + @Configuration @Profile("!test") public class RedisConfig { @@ -35,4 +37,9 @@ public RedisTemplate redisTemplate(RedisConnectionFactory redisC return redisTemplate; } + + @Bean + public ObjectMapper objectMapper() { + return new ObjectMapper(); + } } diff --git a/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java b/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java index 01b967dc..6c15a39f 100644 --- a/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java +++ b/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java @@ -3,15 +3,20 @@ import static org.assertj.core.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*; +import java.util.Set; + import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ZSetOperations; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import com.moabam.api.application.member.MemberMapper; import com.moabam.api.domain.member.Member; +import com.moabam.api.dto.ranking.PersonalRankingInfo; import com.moabam.api.dto.ranking.RankingInfo; import com.moabam.api.dto.room.CertifiedMemberInfo; import com.moabam.api.infrastructure.redis.ZSetRedisRepository; @@ -160,4 +165,90 @@ void update_success() { assertThat(resultDouble).isNull(); } } + + @DisplayName("조회") + @Nested + class Select { + + @DisplayName("성공") + @Test + void test() { + // given + redisTemplate.opsForZSet().add("Ranking", + new RankingInfo(1L, "Hello1", "123"), + 1); + redisTemplate.opsForZSet().add("Ranking", + new RankingInfo(2L, "Hello2", "123"), + 2); + redisTemplate.opsForZSet().add("Ranking", + new RankingInfo(3L, "Hello3", "123"), + 3); + redisTemplate.opsForZSet().add("Ranking", + new RankingInfo(4L, "Hello4", "123"), + 4); + + // when + setSerialize(Object.class); + Set> rankings = redisTemplate.opsForZSet() + .reverseRangeWithScores("Ranking", 0, 2); + setSerialize(String.class); + + // then + assertThat(rankings).hasSize(3); + } + + @DisplayName("일부만 조회 성공") + @Test + void search_part() { + // given + redisTemplate.opsForZSet().add("Ranking", + new RankingInfo(1L, "Hello1", "123"), + 1); + redisTemplate.opsForZSet().add("Ranking", + new RankingInfo(2L, "Hello2", "123"), + 2); + + // when + setSerialize(Object.class); + Set> rankings = redisTemplate.opsForZSet() + .reverseRangeWithScores("Ranking", 0, 10); + setSerialize(String.class); + + // then + assertThat(rankings).hasSize(2); + } + + private void setSerialize(Class classes) { + redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(classes)); + } + + @DisplayName("랭킹 조회 성공") + @Test + void getTopRankings() { + // given + redisTemplate.opsForZSet().add("Ranking", + new RankingInfo(1L, "Hello1", "123"), + 1); + redisTemplate.opsForZSet().add("Ranking", + new RankingInfo(2L, "Hello2", "123"), + 2); + redisTemplate.opsForZSet().add("Ranking", + new RankingInfo(4L, "Hello3", "123"), + 2); + redisTemplate.opsForZSet().add("Ranking", + new RankingInfo(3L, "Hello2", "123"), + 3); + redisTemplate.opsForZSet().add("Ranking", + new RankingInfo(5L, "Hello4", "123"), + 3); + + // When + Then + assertThatNoException().isThrownBy(() -> rankingService.getMemberRanking(PersonalRankingInfo.builder() + .score(1L) + .image("123") + .nickname("Hello1") + .memberId(1L) + .build())); + } + } } From 0ac854c49376ea57b81aba115e0298438f4ff284 Mon Sep 17 00:00:00 2001 From: parksey Date: Thu, 30 Nov 2023 04:17:00 +0900 Subject: [PATCH 04/11] =?UTF-8?q?feat:=20=EB=9E=AD=ED=82=B9=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=EB=9E=AD=ED=82=B9=20=EC=97=85?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=8A=B8=20=EB=A1=9C=EC=A7=81=20=EA=B0=81=20?= =?UTF-8?q?=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8=20->=20=EC=8A=A4=EC=BC=80?= =?UTF-8?q?=EC=A5=B4=EB=9F=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/application/member/MemberMapper.java | 34 ++-- .../api/application/member/MemberService.java | 18 ++- .../notification/NotificationService.java | 4 +- .../application/ranking/RankingMapper.java | 12 +- .../application/ranking/RankingService.java | 23 ++- .../repository/MemberSearchRepository.java | 9 ++ ...nalRankingInfo.java => UpdateRanking.java} | 6 +- .../redis/ZSetRedisRepository.java | 8 +- .../api/presentation/RankingController.java | 7 +- .../api/presentation/RoomController.java | 3 - .../application/member/MemberServiceTest.java | 13 ++ .../ranking/RankingServiceTest.java | 149 ++++++++---------- .../CertificationServiceConcurrencyTest.java | 10 +- .../room/CertificationServiceTest.java | 6 +- .../room/RoomServiceConcurrencyTest.java | 8 +- .../api/application/room/RoomServiceTest.java | 2 +- .../InventorySearchRepositoryTest.java | 10 +- .../domain/member/MemberRepositoryTest.java | 10 +- .../presentation/MemberControllerTest.java | 6 +- .../NotificationControllerTest.java | 2 +- .../presentation/RankingControllerTest.java | 82 ++++++++++ .../presentation/ReportControllerTest.java | 4 +- .../api/presentation/RoomControllerTest.java | 14 +- .../moabam/support/fixture/MemberFixture.java | 2 +- 24 files changed, 261 insertions(+), 181 deletions(-) rename src/main/java/com/moabam/api/dto/ranking/{PersonalRankingInfo.java => UpdateRanking.java} (51%) create mode 100644 src/test/java/com/moabam/api/presentation/RankingControllerTest.java diff --git a/src/main/java/com/moabam/api/application/member/MemberMapper.java b/src/main/java/com/moabam/api/application/member/MemberMapper.java index 66d9baa4..67fd8a92 100644 --- a/src/main/java/com/moabam/api/application/member/MemberMapper.java +++ b/src/main/java/com/moabam/api/application/member/MemberMapper.java @@ -19,8 +19,8 @@ import com.moabam.api.dto.member.MemberInfo; import com.moabam.api.dto.member.MemberInfoResponse; import com.moabam.api.dto.member.MemberInfoSearchResponse; -import com.moabam.api.dto.ranking.PersonalRankingInfo; import com.moabam.api.dto.ranking.RankingInfo; +import com.moabam.api.dto.ranking.UpdateRanking; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -35,6 +35,13 @@ public static Member toMember(Long socialId) { .build(); } + public static UpdateRanking toUpdateRanking(Member member) { + return UpdateRanking.builder() + .rankingInfo(toRankingInfo(member)) + .score(member.getTotalCertifyCount()) + .build(); + } + public static MemberInfoSearchResponse toMemberInfoSearchResponse(List memberInfos) { MemberInfo infos = memberInfos.get(0); List badgeTypes = memberInfos.stream() @@ -81,6 +88,14 @@ public static Inventory toInventory(Long memberId, Item item) { .build(); } + public static RankingInfo toRankingInfo(Member member) { + return RankingInfo.builder() + .memberId(member.getId()) + .nickname(member.getNickname()) + .image(member.getProfileImage()) + .build(); + } + private static List badgedNames(Set badgeTypes) { return BadgeType.memberBadgeMap(badgeTypes); } @@ -92,21 +107,4 @@ private static Map defaultSkins(String morningImage, String nigh return birdsSkin; } - - public static RankingInfo toRankingInfo(Member member) { - return RankingInfo.builder() - .memberId(member.getId()) - .nickname(member.getNickname()) - .image(member.getProfileImage()) - .build(); - } - - public static PersonalRankingInfo toRankingInfoWithScore(Member member) { - return PersonalRankingInfo.builder() - .score(member.getTotalCertifyCount()) - .memberId(member.getId()) - .nickname(member.getNickname()) - .image(member.getProfileImage()) - .build(); - } } diff --git a/src/main/java/com/moabam/api/application/member/MemberService.java b/src/main/java/com/moabam/api/application/member/MemberService.java index 554033ed..7cfec537 100644 --- a/src/main/java/com/moabam/api/application/member/MemberService.java +++ b/src/main/java/com/moabam/api/application/member/MemberService.java @@ -6,6 +6,7 @@ import java.util.Objects; import java.util.Optional; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -27,8 +28,8 @@ import com.moabam.api.dto.member.MemberInfoResponse; import com.moabam.api.dto.member.MemberInfoSearchResponse; import com.moabam.api.dto.member.ModifyMemberRequest; -import com.moabam.api.dto.ranking.PersonalRankingInfo; import com.moabam.api.dto.ranking.RankingInfo; +import com.moabam.api.dto.ranking.UpdateRanking; import com.moabam.global.auth.model.AuthMember; import com.moabam.global.common.util.BaseDataCode; import com.moabam.global.common.util.ClockHolder; @@ -124,10 +125,20 @@ public void modifyInfo(AuthMember authMember, ModifyMemberRequest modifyMemberRe } } - public PersonalRankingInfo getRankingInfo(AuthMember authMember) { + public UpdateRanking getRankingInfo(AuthMember authMember) { Member member = findMember(authMember.id()); - return MemberMapper.toRankingInfoWithScore(member); + return MemberMapper.toUpdateRanking(member); + } + + @Scheduled(cron = "* 15 * * * *") + public void updateAllRanking() { + List members = memberSearchRepository.findAllMembers(); + List updateRankings = members.stream() + .map(MemberMapper::toUpdateRanking) + .toList(); + + rankingService.updateScores(updateRankings); } private void changeNickname(Long memberId, String changedName) { @@ -138,7 +149,6 @@ private void changeNickname(Long memberId, String changedName) { } } - private void validateNickname(String nickname) { if (Objects.isNull(nickname)) { return; diff --git a/src/main/java/com/moabam/api/application/notification/NotificationService.java b/src/main/java/com/moabam/api/application/notification/NotificationService.java index 7eedd953..dccd2a03 100644 --- a/src/main/java/com/moabam/api/application/notification/NotificationService.java +++ b/src/main/java/com/moabam/api/application/notification/NotificationService.java @@ -73,8 +73,8 @@ public List getMyKnockStatusInRoom(Long memberId, Long roomId, List !participant.getMemberId().equals(memberId)) .toList(); - Predicate knockPredicate = targetId -> - notificationRepository.existsKnockByKey(memberId, targetId, roomId); + Predicate knockPredicate = targetId -> notificationRepository.existsKnockByKey(memberId, targetId, + roomId); Map> knockStatus = filteredParticipants.stream() .map(Participant::getMemberId) diff --git a/src/main/java/com/moabam/api/application/ranking/RankingMapper.java b/src/main/java/com/moabam/api/application/ranking/RankingMapper.java index ad9c0924..eae3b65c 100644 --- a/src/main/java/com/moabam/api/application/ranking/RankingMapper.java +++ b/src/main/java/com/moabam/api/application/ranking/RankingMapper.java @@ -2,10 +2,10 @@ import java.util.List; -import com.moabam.api.dto.ranking.PersonalRankingInfo; import com.moabam.api.dto.ranking.RankingInfo; import com.moabam.api.dto.ranking.TopRankingInfoResponse; import com.moabam.api.dto.ranking.TopRankingResponses; +import com.moabam.api.dto.ranking.UpdateRanking; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -23,13 +23,13 @@ public static TopRankingInfoResponse topRankingResponse(int rank, long score, Ra .build(); } - public static TopRankingInfoResponse topRankingResponse(int rank, PersonalRankingInfo rankInfo) { + public static TopRankingInfoResponse topRankingResponse(int rank, UpdateRanking updateRanking) { return TopRankingInfoResponse.builder() .rank(rank) - .score(rankInfo.score()) - .nickname(rankInfo.nickname()) - .image(rankInfo.image()) - .memberId(rankInfo.memberId()) + .score(updateRanking.score()) + .nickname(updateRanking.rankingInfo().nickname()) + .image(updateRanking.rankingInfo().image()) + .memberId(updateRanking.rankingInfo().memberId()) .build(); } diff --git a/src/main/java/com/moabam/api/application/ranking/RankingService.java b/src/main/java/com/moabam/api/application/ranking/RankingService.java index a4c6842c..fdcc8638 100644 --- a/src/main/java/com/moabam/api/application/ranking/RankingService.java +++ b/src/main/java/com/moabam/api/application/ranking/RankingService.java @@ -11,13 +11,10 @@ import org.springframework.stereotype.Service; import com.fasterxml.jackson.databind.ObjectMapper; -import com.moabam.api.application.member.MemberMapper; -import com.moabam.api.domain.member.Member; -import com.moabam.api.dto.ranking.PersonalRankingInfo; import com.moabam.api.dto.ranking.RankingInfo; import com.moabam.api.dto.ranking.TopRankingInfoResponse; import com.moabam.api.dto.ranking.TopRankingResponses; -import com.moabam.api.dto.room.CertifiedMemberInfo; +import com.moabam.api.dto.ranking.UpdateRanking; import com.moabam.api.infrastructure.redis.ZSetRedisRepository; import lombok.RequiredArgsConstructor; @@ -28,7 +25,7 @@ public class RankingService { private static final String RANKING = "Ranking"; private static final int START_INDEX = 0; - private static final int LIMIT_INDEX = 10; + private static final int LIMIT_INDEX = 9; private final ObjectMapper objectMapper; private final ZSetRedisRepository zSetRedisRepository; @@ -37,10 +34,9 @@ public void addRanking(RankingInfo rankingInfo, Long totalCertifyCount) { zSetRedisRepository.add(RANKING, rankingInfo, totalCertifyCount); } - public void updateCacheScore(CertifiedMemberInfo info) { - Member member = info.member(); - RankingInfo rankingInfo = MemberMapper.toRankingInfo(member); - zSetRedisRepository.add(RANKING, rankingInfo, member.getTotalCertifyCount()); + public void updateScores(List updateRankings) { + updateRankings.forEach(updateRanking -> + zSetRedisRepository.add(RANKING, updateRanking.rankingInfo(), updateRanking.score())); } public void changeInfos(RankingInfo before, RankingInfo after) { @@ -51,11 +47,11 @@ public void removeRanking(RankingInfo rankingInfo) { zSetRedisRepository.delete(RANKING, rankingInfo); } - public TopRankingResponses getMemberRanking(PersonalRankingInfo myRankingInfo) { + public TopRankingResponses getMemberRanking(UpdateRanking myRankingInfo) { List topRankings = getTopRankings(); - Long myRanking = zSetRedisRepository.reverseRank("Ranking", myRankingInfo); - TopRankingInfoResponse myRankingInfoResponse = RankingMapper.topRankingResponse(myRanking.intValue(), - myRankingInfo); + Long myRanking = zSetRedisRepository.reverseRank(RANKING, myRankingInfo.rankingInfo()); + TopRankingInfoResponse myRankingInfoResponse = + RankingMapper.topRankingResponse(myRanking.intValue(), myRankingInfo); return RankingMapper.topRankingResponses(myRankingInfoResponse, topRankings); } @@ -74,6 +70,7 @@ private List getTopRankings() { RankingInfo rankingInfo = objectMapper.convertValue(topRanking.getValue(), RankingInfo.class); topRankingInfoRespons.add(RankingMapper.topRankingResponse(scoreSet.size(), score, rankingInfo)); } + return topRankingInfoRespons; } } diff --git a/src/main/java/com/moabam/api/domain/member/repository/MemberSearchRepository.java b/src/main/java/com/moabam/api/domain/member/repository/MemberSearchRepository.java index b853b871..82a43a01 100644 --- a/src/main/java/com/moabam/api/domain/member/repository/MemberSearchRepository.java +++ b/src/main/java/com/moabam/api/domain/member/repository/MemberSearchRepository.java @@ -29,6 +29,15 @@ public Optional findMember(Long memberId) { return findMember(memberId, true); } + public List findAllMembers() { + return jpaQueryFactory + .selectFrom(member) + .where( + member.deletedAt.isNotNull() + ) + .fetch(); + } + public Optional findMember(Long memberId, boolean isNotDeleted) { return Optional.ofNullable(jpaQueryFactory .selectFrom(member) diff --git a/src/main/java/com/moabam/api/dto/ranking/PersonalRankingInfo.java b/src/main/java/com/moabam/api/dto/ranking/UpdateRanking.java similarity index 51% rename from src/main/java/com/moabam/api/dto/ranking/PersonalRankingInfo.java rename to src/main/java/com/moabam/api/dto/ranking/UpdateRanking.java index 8a8ffc8d..6b5218ee 100644 --- a/src/main/java/com/moabam/api/dto/ranking/PersonalRankingInfo.java +++ b/src/main/java/com/moabam/api/dto/ranking/UpdateRanking.java @@ -3,10 +3,8 @@ import lombok.Builder; @Builder -public record PersonalRankingInfo( - Long memberId, - String nickname, - String image, +public record UpdateRanking( + RankingInfo rankingInfo, Long score ) { diff --git a/src/main/java/com/moabam/api/infrastructure/redis/ZSetRedisRepository.java b/src/main/java/com/moabam/api/infrastructure/redis/ZSetRedisRepository.java index 4457a26a..d68e43b7 100644 --- a/src/main/java/com/moabam/api/infrastructure/redis/ZSetRedisRepository.java +++ b/src/main/java/com/moabam/api/infrastructure/redis/ZSetRedisRepository.java @@ -68,11 +68,11 @@ public Set> rangeJson(String key, int startIndex, int limitIn return rankings; } - private void setSerialize(Class classes) { - redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(classes)); - } - public Long reverseRank(String key, Object myRankingInfo) { return redisTemplate.opsForZSet().reverseRank(key, myRankingInfo); } + + private void setSerialize(Class classes) { + redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(classes)); + } } diff --git a/src/main/java/com/moabam/api/presentation/RankingController.java b/src/main/java/com/moabam/api/presentation/RankingController.java index 9820454e..68796632 100644 --- a/src/main/java/com/moabam/api/presentation/RankingController.java +++ b/src/main/java/com/moabam/api/presentation/RankingController.java @@ -8,14 +8,14 @@ import com.moabam.api.application.member.MemberService; import com.moabam.api.application.ranking.RankingService; -import com.moabam.api.dto.ranking.PersonalRankingInfo; import com.moabam.api.dto.ranking.TopRankingResponses; +import com.moabam.api.dto.ranking.UpdateRanking; import com.moabam.global.auth.annotation.Auth; import com.moabam.global.auth.model.AuthMember; import lombok.RequiredArgsConstructor; -@RequestMapping("/ranking") +@RequestMapping("/rankings") @RestController @RequiredArgsConstructor public class RankingController { @@ -26,7 +26,8 @@ public class RankingController { @GetMapping @ResponseStatus(HttpStatus.OK) public TopRankingResponses getRanking(@Auth AuthMember authMember) { - PersonalRankingInfo rankingInfo = memberService.getRankingInfo(authMember); + UpdateRanking rankingInfo = memberService.getRankingInfo(authMember); + return rankingService.getMemberRanking(rankingInfo); } } diff --git a/src/main/java/com/moabam/api/presentation/RoomController.java b/src/main/java/com/moabam/api/presentation/RoomController.java index 142cfa80..2581e5e1 100644 --- a/src/main/java/com/moabam/api/presentation/RoomController.java +++ b/src/main/java/com/moabam/api/presentation/RoomController.java @@ -18,7 +18,6 @@ import org.springframework.web.multipart.MultipartFile; import com.moabam.api.application.image.ImageService; -import com.moabam.api.application.ranking.RankingService; import com.moabam.api.application.room.CertificationService; import com.moabam.api.application.room.RoomService; import com.moabam.api.application.room.SearchService; @@ -49,7 +48,6 @@ public class RoomController { private final SearchService searchService; private final CertificationService certificationService; private final ImageService imageService; - private final RankingService rankingService; @PostMapping @ResponseStatus(HttpStatus.CREATED) @@ -116,7 +114,6 @@ public void certifyRoom(@Auth AuthMember authMember, @PathVariable("roomId") Lon List imageUrls = imageService.uploadImages(multipartFiles, ImageType.CERTIFICATION); CertifiedMemberInfo info = certificationService.getCertifiedMemberInfo(authMember.id(), roomId, imageUrls); certificationService.certifyRoom(info); - rankingService.updateCacheScore(info); } @PutMapping("/{roomId}/members/{memberId}/mandate") diff --git a/src/test/java/com/moabam/api/application/member/MemberServiceTest.java b/src/test/java/com/moabam/api/application/member/MemberServiceTest.java index 4969c93e..4be1fe70 100644 --- a/src/test/java/com/moabam/api/application/member/MemberServiceTest.java +++ b/src/test/java/com/moabam/api/application/member/MemberServiceTest.java @@ -229,4 +229,17 @@ void modify_success_test(@WithMember AuthMember authMember) { () -> assertThat(member.getProfileImage()).isEqualTo("/main") ); } + + @DisplayName("모든 랭킹 업데이트") + @Test + void update_all_ranking() { + // given + Member member1 = MemberFixture.member("1"); + Member member2 = MemberFixture.member("2"); + given(memberSearchRepository.findAllMembers()) + .willReturn(List.of(member1, member2)); + + // when + Then + assertThatNoException().isThrownBy(() -> memberService.updateAllRanking()); + } } diff --git a/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java b/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java index 6c15a39f..b33c2f9d 100644 --- a/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java +++ b/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java @@ -3,6 +3,7 @@ import static org.assertj.core.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*; +import java.util.List; import java.util.Set; import org.junit.jupiter.api.DisplayName; @@ -16,12 +17,14 @@ import com.moabam.api.application.member.MemberMapper; import com.moabam.api.domain.member.Member; -import com.moabam.api.dto.ranking.PersonalRankingInfo; import com.moabam.api.dto.ranking.RankingInfo; -import com.moabam.api.dto.room.CertifiedMemberInfo; +import com.moabam.api.dto.ranking.TopRankingInfoResponse; +import com.moabam.api.dto.ranking.TopRankingResponses; +import com.moabam.api.dto.ranking.UpdateRanking; import com.moabam.api.infrastructure.redis.ZSetRedisRepository; import com.moabam.global.config.EmbeddedRedisConfig; import com.moabam.support.fixture.BugFixture; +import com.moabam.support.fixture.MemberFixture; @SpringBootTest(classes = {EmbeddedRedisConfig.class, RankingService.class, ZSetRedisRepository.class}) public class RankingServiceTest { @@ -54,13 +57,10 @@ void add_success() { rankingService.addRanking(rankingInfo, totalCertifyCount); // then - Double resultDouble = - redisTemplate.opsForZSet().score("Ranking", rankingInfo); + Double resultDouble = redisTemplate.opsForZSet().score("Ranking", rankingInfo); - assertAll( - () -> assertThat(resultDouble).isNotNull(), - () -> assertThat(resultDouble).isEqualTo(Double.valueOf(totalCertifyCount)) - ); + assertAll(() -> assertThat(resultDouble).isNotNull(), + () -> assertThat(resultDouble).isEqualTo(Double.valueOf(totalCertifyCount))); } } @@ -72,30 +72,25 @@ class Update { @Test void update_success() { // given - Long totalCertifyCount = 0L; - Member member = Member.builder() - .socialId("1") - .bug(BugFixture.bug()) - .build(); + Member member = MemberFixture.member("1"); member.increaseTotalCertifyCount(); member.increaseTotalCertifyCount(); - Long expect = member.getTotalCertifyCount(); - CertifiedMemberInfo certifiedMemberInfo = CertifiedMemberInfo.builder() - .member(member) - .build(); - RankingInfo result = MemberMapper.toRankingInfo(member); + + Member member1 = MemberFixture.member("2"); + member1.increaseTotalCertifyCount(); + member1.increaseTotalCertifyCount(); + + List members = List.of(member, member1); + List updateRankings = members.stream().map(MemberMapper::toUpdateRanking).toList(); // when - rankingService.addRanking(result, totalCertifyCount); - rankingService.updateCacheScore(certifiedMemberInfo); - Double resultDouble = - redisTemplate.opsForZSet().score("Ranking", result); + rankingService.updateScores(updateRankings); + + Double resultDouble = redisTemplate.opsForZSet().score("Ranking", updateRankings.get(0).rankingInfo()); // then - assertAll( - () -> assertThat(resultDouble).isNotNull(), - () -> assertThat(resultDouble).isEqualTo(Double.valueOf(expect)) - ); + assertAll(() -> assertThat(resultDouble).isNotNull(), + () -> assertThat(resultDouble).isEqualTo(Double.valueOf(member.getTotalCertifyCount()))); } } @@ -107,32 +102,24 @@ class Change { @Test void update_success() { // given - Member member = Member.builder() - .socialId("1") - .bug(BugFixture.bug()) - .build(); + Member member = Member.builder().socialId("1").bug(BugFixture.bug()).build(); member.increaseTotalCertifyCount(); member.increaseTotalCertifyCount(); - Long expect = member.getTotalCertifyCount(); - CertifiedMemberInfo certifiedMemberInfo = CertifiedMemberInfo.builder() - .member(member) - .build(); + + long expect = member.getTotalCertifyCount(); RankingInfo before = MemberMapper.toRankingInfo(member); + rankingService.addRanking(before, member.getTotalCertifyCount()); // when - rankingService.updateCacheScore(certifiedMemberInfo); member.changeIntro("밥세공기"); RankingInfo changeInfo = MemberMapper.toRankingInfo(member); rankingService.changeInfos(before, changeInfo); - Double resultDouble = - redisTemplate.opsForZSet().score("Ranking", changeInfo); + Double resultDouble = redisTemplate.opsForZSet().score("Ranking", changeInfo); // then - assertAll( - () -> assertThat(resultDouble).isNotNull(), - () -> assertThat(resultDouble).isEqualTo(Double.valueOf(expect)) - ); + assertAll(() -> assertThat(resultDouble).isNotNull(), + () -> assertThat(resultDouble).isEqualTo(Double.valueOf(expect))); } } @@ -145,10 +132,7 @@ class Delete { void update_success() { // given Long totalCertify = 5L; - Member member = Member.builder() - .socialId("1") - .bug(BugFixture.bug()) - .build(); + Member member = Member.builder().socialId("1").bug(BugFixture.bug()).build(); member.increaseTotalCertifyCount(); member.increaseTotalCertifyCount(); RankingInfo rankingInfo = MemberMapper.toRankingInfo(member); @@ -158,8 +142,7 @@ void update_success() { // when rankingService.removeRanking(rankingInfo); - Double resultDouble = - redisTemplate.opsForZSet().score("Ranking", rankingInfo); + Double resultDouble = redisTemplate.opsForZSet().score("Ranking", rankingInfo); // then assertThat(resultDouble).isNull(); @@ -174,18 +157,10 @@ class Select { @Test void test() { // given - redisTemplate.opsForZSet().add("Ranking", - new RankingInfo(1L, "Hello1", "123"), - 1); - redisTemplate.opsForZSet().add("Ranking", - new RankingInfo(2L, "Hello2", "123"), - 2); - redisTemplate.opsForZSet().add("Ranking", - new RankingInfo(3L, "Hello3", "123"), - 3); - redisTemplate.opsForZSet().add("Ranking", - new RankingInfo(4L, "Hello4", "123"), - 4); + redisTemplate.opsForZSet().add("Ranking", new RankingInfo(1L, "Hello1", "123"), 1); + redisTemplate.opsForZSet().add("Ranking", new RankingInfo(2L, "Hello2", "123"), 2); + redisTemplate.opsForZSet().add("Ranking", new RankingInfo(3L, "Hello3", "123"), 3); + redisTemplate.opsForZSet().add("Ranking", new RankingInfo(4L, "Hello4", "123"), 4); // when setSerialize(Object.class); @@ -201,12 +176,8 @@ void test() { @Test void search_part() { // given - redisTemplate.opsForZSet().add("Ranking", - new RankingInfo(1L, "Hello1", "123"), - 1); - redisTemplate.opsForZSet().add("Ranking", - new RankingInfo(2L, "Hello2", "123"), - 2); + redisTemplate.opsForZSet().add("Ranking", new RankingInfo(1L, "Hello1", "123"), 1); + redisTemplate.opsForZSet().add("Ranking", new RankingInfo(2L, "Hello2", "123"), 2); // when setSerialize(Object.class); @@ -226,29 +197,33 @@ private void setSerialize(Class classes) { @Test void getTopRankings() { // given - redisTemplate.opsForZSet().add("Ranking", - new RankingInfo(1L, "Hello1", "123"), - 1); - redisTemplate.opsForZSet().add("Ranking", - new RankingInfo(2L, "Hello2", "123"), - 2); - redisTemplate.opsForZSet().add("Ranking", - new RankingInfo(4L, "Hello3", "123"), - 2); - redisTemplate.opsForZSet().add("Ranking", - new RankingInfo(3L, "Hello2", "123"), - 3); - redisTemplate.opsForZSet().add("Ranking", - new RankingInfo(5L, "Hello4", "123"), - 3); - - // When + Then - assertThatNoException().isThrownBy(() -> rankingService.getMemberRanking(PersonalRankingInfo.builder() + for (int i = 0; i < 20; i++) { + RankingInfo rankingInfo = new RankingInfo((long)(i + 1), "Hello" + (i + 1), "123"); + redisTemplate.opsForZSet().add("Ranking", rankingInfo, i + 1); + } + RankingInfo rankingInfo = new RankingInfo(21L, "Hello22", "123"); + redisTemplate.opsForZSet().add("Ranking", rankingInfo, 20); + RankingInfo rankingInfo2 = new RankingInfo(22L, "Hello23", "123"); + redisTemplate.opsForZSet().add("Ranking", rankingInfo2, 19); + + UpdateRanking myRanking = UpdateRanking.builder() .score(1L) - .image("123") - .nickname("Hello1") - .memberId(1L) - .build())); + .rankingInfo(RankingInfo.builder().nickname("Hello1").memberId(1L).image("123").build()) + .build(); + + // When + TopRankingResponses topRankingResponses = rankingService.getMemberRanking(myRanking); + + // Then + List topRankings = topRankingResponses.topRankings(); + TopRankingInfoResponse myRank = topRankingResponses.myRanking(); + assertAll(() -> assertThat(topRankings).hasSize(10), () -> assertThat(myRank.score()).isEqualTo(1), + () -> assertThat(topRankings.get(0).rank()).isEqualTo(1), + () -> assertThat(topRankings.get(1).rank()).isEqualTo(1), + () -> assertThat(topRankings.get(2).rank()).isEqualTo(2), + () -> assertThat(topRankings.get(3).rank()).isEqualTo(2), + () -> assertThat(topRankings.get(4).rank()).isEqualTo(3)); + } } } diff --git a/src/test/java/com/moabam/api/application/room/CertificationServiceConcurrencyTest.java b/src/test/java/com/moabam/api/application/room/CertificationServiceConcurrencyTest.java index fd1fdc1e..3edc5543 100644 --- a/src/test/java/com/moabam/api/application/room/CertificationServiceConcurrencyTest.java +++ b/src/test/java/com/moabam/api/application/room/CertificationServiceConcurrencyTest.java @@ -61,11 +61,11 @@ void certify_room_success() throws InterruptedException { } Room savedRoom = roomRepository.save(room); - Member member1 = MemberFixture.member("0000", "닉네임1"); - Member member2 = MemberFixture.member("1234", "닉네임2"); - Member member3 = MemberFixture.member("5678", "닉네임3"); - Member member4 = MemberFixture.member("3333", "닉네임4"); - Member member5 = MemberFixture.member("5555", "닉네임5"); + Member member1 = MemberFixture.member("0000"); + Member member2 = MemberFixture.member("1234"); + Member member3 = MemberFixture.member("5678"); + Member member4 = MemberFixture.member("3333"); + Member member5 = MemberFixture.member("5555"); List members = memberRepository.saveAll(List.of(member1, member2, member3, member4, member5)); diff --git a/src/test/java/com/moabam/api/application/room/CertificationServiceTest.java b/src/test/java/com/moabam/api/application/room/CertificationServiceTest.java index d15ecdf1..32b9eb9b 100644 --- a/src/test/java/com/moabam/api/application/room/CertificationServiceTest.java +++ b/src/test/java/com/moabam/api/application/room/CertificationServiceTest.java @@ -102,9 +102,9 @@ class CertificationServiceTest { void init() { room = spy(RoomFixture.room()); participant = spy(RoomFixture.participant(room, 1L)); - member1 = MemberFixture.member("1", "회원1"); - member2 = MemberFixture.member("2", "회원2"); - member3 = MemberFixture.member("3", "회원3"); + member1 = MemberFixture.member("1"); + member2 = MemberFixture.member("2"); + member3 = MemberFixture.member("3"); lenient().when(room.getId()).thenReturn(1L); lenient().when(participant.getRoom()).thenReturn(room); diff --git a/src/test/java/com/moabam/api/application/room/RoomServiceConcurrencyTest.java b/src/test/java/com/moabam/api/application/room/RoomServiceConcurrencyTest.java index 53a190f9..7e1c0f41 100644 --- a/src/test/java/com/moabam/api/application/room/RoomServiceConcurrencyTest.java +++ b/src/test/java/com/moabam/api/application/room/RoomServiceConcurrencyTest.java @@ -60,9 +60,9 @@ void enter_room_concurrency_test() throws InterruptedException { Room savedRoom = roomRepository.save(room); - Member member1 = MemberFixture.member("qwe", "닉네임1"); - Member member2 = MemberFixture.member("qwfe", "닉네임2"); - Member member3 = MemberFixture.member("qff", "닉네임3"); + Member member1 = MemberFixture.member("qwe"); + Member member2 = MemberFixture.member("qwfe"); + Member member3 = MemberFixture.member("qff"); memberRepository.saveAll(List.of(member1, member2, member3)); Participant participant1 = RoomFixture.participant(savedRoom, member1.getId()); @@ -79,7 +79,7 @@ void enter_room_concurrency_test() throws InterruptedException { // when for (int i = 0; i < threadCount; i++) { - Member member = MemberFixture.member(String.valueOf(i + 100), "test"); + Member member = MemberFixture.member(String.valueOf(i + 100)); newMembers.add(member); memberRepository.save(member); final Long memberId = member.getId(); diff --git a/src/test/java/com/moabam/api/application/room/RoomServiceTest.java b/src/test/java/com/moabam/api/application/room/RoomServiceTest.java index 667508f0..2a37acfc 100644 --- a/src/test/java/com/moabam/api/application/room/RoomServiceTest.java +++ b/src/test/java/com/moabam/api/application/room/RoomServiceTest.java @@ -115,7 +115,7 @@ void room_manager_mandate_success() { Long managerId = 1L; Long memberId = 2L; - Member member = MemberFixture.member("1234", "닉네임"); + Member member = MemberFixture.member("1234"); Room room = spy(RoomFixture.room()); given(room.getId()).willReturn(1L); diff --git a/src/test/java/com/moabam/api/domain/item/repository/InventorySearchRepositoryTest.java b/src/test/java/com/moabam/api/domain/item/repository/InventorySearchRepositoryTest.java index 1e6a3299..54063f6b 100644 --- a/src/test/java/com/moabam/api/domain/item/repository/InventorySearchRepositoryTest.java +++ b/src/test/java/com/moabam/api/domain/item/repository/InventorySearchRepositoryTest.java @@ -85,7 +85,7 @@ void empty_success() { @Test void find_one_success() { // given - Member member = memberRepository.save(member("999", "test")); + Member member = memberRepository.save(member("999")); Item item = itemRepository.save(nightMageSkin()); Inventory inventory = inventoryRepository.save(inventory(member.getId(), item)); @@ -100,7 +100,7 @@ void find_one_success() { @Test void find_default_success() { // given - Member member = memberRepository.save(member("11314", "test")); + Member member = memberRepository.save(member("11314")); Item item = itemRepository.save(nightMageSkin()); Inventory inventory = inventoryRepository.save(inventory(member.getId(), item)); inventory.select(); @@ -116,8 +116,8 @@ void find_default_success() { @Test void find_all_default_type_night_success() { // given - Member member1 = memberRepository.save(member("625", "회원1")); - Member member2 = memberRepository.save(member("255", "회원2")); + Member member1 = memberRepository.save(member("625")); + Member member2 = memberRepository.save(member("255")); Item item = itemRepository.save(nightMageSkin()); Inventory inventory1 = inventoryRepository.save(inventory(member1.getId(), item)); Inventory inventory2 = inventoryRepository.save(inventory(member2.getId(), item)); @@ -141,7 +141,7 @@ class FindDefaultBird { @Test void bird_find_success() { // given - Member member = MemberFixture.member("fffdd", "test"); + Member member = MemberFixture.member("fffdd"); member.exitRoom(RoomType.MORNING); memberRepository.save(member); diff --git a/src/test/java/com/moabam/api/domain/member/MemberRepositoryTest.java b/src/test/java/com/moabam/api/domain/member/MemberRepositoryTest.java index 8f600444..90729351 100644 --- a/src/test/java/com/moabam/api/domain/member/MemberRepositoryTest.java +++ b/src/test/java/com/moabam/api/domain/member/MemberRepositoryTest.java @@ -58,7 +58,7 @@ class MemberRepositoryTest { @Test void test() { // given - Member member = MemberFixture.member("313", "test"); + Member member = MemberFixture.member("313"); memberRepository.save(member); // when @@ -76,7 +76,7 @@ class FindMemberTest { @Test void room_exist_and_manager_error() { // given - Member member = MemberFixture.member("1111", "test"); + Member member = MemberFixture.member("1111"); memberRepository.save(member); Room room = RoomFixture.room(); @@ -102,7 +102,7 @@ void room_exist_and_not_manager_success() { room.changeManagerNickname("test"); roomRepository.save(room); - Member member = MemberFixture.member("44", "test"); + Member member = MemberFixture.member("44"); member.changeNickName("not"); memberRepository.save(member); @@ -133,7 +133,7 @@ void member_not_found() { @Test void search_info_success() { // given - Member member = MemberFixture.member("hhhh", "test"); + Member member = MemberFixture.member("hhhh"); member.enterRoom(RoomType.MORNING); memberRepository.save(member); @@ -158,7 +158,7 @@ void search_info_success() { @Test void no_badges_search_success() { // given - Member member = MemberFixture.member("ttttt", "test"); + Member member = MemberFixture.member("ttttt"); member.enterRoom(RoomType.MORNING); memberRepository.save(member); diff --git a/src/test/java/com/moabam/api/presentation/MemberControllerTest.java b/src/test/java/com/moabam/api/presentation/MemberControllerTest.java index 41d42a6b..cb66bdd1 100644 --- a/src/test/java/com/moabam/api/presentation/MemberControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/MemberControllerTest.java @@ -148,7 +148,7 @@ void allSetUp() { restTemplateBuilder = new RestTemplateBuilder() .errorHandler(new RestTemplateResponseHandler()); - member = MemberFixture.member("1234567890987654", "nickname"); + member = MemberFixture.member("1234567890987654"); member.increaseTotalCertifyCount(); memberRepository.save(member); } @@ -371,7 +371,7 @@ void search_my_info_with_no_badge_success() throws Exception { @Test void search_friend_info_success() throws Exception { // given - Member friend = MemberFixture.member("123456789", "nick"); + Member friend = MemberFixture.member("123456789"); memberRepository.save(friend); Badge morningBirth = BadgeFixture.badge(friend.getId(), BadgeType.MORNING_BIRTH); @@ -527,7 +527,7 @@ void member_modify_no_image_request_success(String intro, String nickname) throw if (Objects.nonNull(nickname)) { updateNick = nickname; } - + Double result = redisTemplate.opsForZSet() .score("Ranking", new RankingInfo(member.getId(), updateNick, member.getProfileImage())); assertThat(result).isEqualTo(member.getTotalCertifyCount()); diff --git a/src/test/java/com/moabam/api/presentation/NotificationControllerTest.java b/src/test/java/com/moabam/api/presentation/NotificationControllerTest.java index 95581ddf..b8e3696c 100644 --- a/src/test/java/com/moabam/api/presentation/NotificationControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/NotificationControllerTest.java @@ -73,7 +73,7 @@ class NotificationControllerTest extends WithoutFilterSupporter { @BeforeEach void setUp() { - target = memberRepository.save(MemberFixture.member("123", "targetName")); + target = memberRepository.save(MemberFixture.member("123")); room = roomRepository.save(RoomFixture.room()); knockKey = String.format("room_%s_member_%s_knocks_%s", room.getId(), 1, target.getId()); diff --git a/src/test/java/com/moabam/api/presentation/RankingControllerTest.java b/src/test/java/com/moabam/api/presentation/RankingControllerTest.java new file mode 100644 index 00000000..9e124cb2 --- /dev/null +++ b/src/test/java/com/moabam/api/presentation/RankingControllerTest.java @@ -0,0 +1,82 @@ +package com.moabam.api.presentation; + +import static org.hamcrest.Matchers.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.transaction.annotation.Transactional; + +import com.moabam.api.domain.member.Member; +import com.moabam.api.domain.member.repository.MemberRepository; +import com.moabam.api.dto.ranking.RankingInfo; +import com.moabam.api.dto.ranking.UpdateRanking; +import com.moabam.support.annotation.WithMember; +import com.moabam.support.common.WithoutFilterSupporter; +import com.moabam.support.fixture.MemberFixture; + +@Transactional +@SpringBootTest +@AutoConfigureMockMvc +@AutoConfigureRestDocs +class RankingControllerTest extends WithoutFilterSupporter { + + @Autowired + MockMvc mockMvc; + + @Autowired + MemberRepository memberRepository; + + @Autowired + RedisTemplate redisTemplate; + + @DisplayName("") + @WithMember + @Test + void top_ranking() throws Exception { + // given + List members = new ArrayList<>(); + for (int i = 0; i < 20; i++) { + Member member = MemberFixture.member(String.valueOf(i + 1)); + members.add(member); + + RankingInfo rankingInfo = new RankingInfo((long)(i + 1), member.getNickname(), member.getProfileImage()); + redisTemplate.opsForZSet().add("Ranking", rankingInfo, i + 1); + } + memberRepository.saveAll(members); + + RankingInfo rankingInfo = new RankingInfo(21L, "Hello22", "123"); + redisTemplate.opsForZSet().add("Ranking", rankingInfo, 20); + RankingInfo rankingInfo2 = new RankingInfo(22L, "Hello23", "123"); + redisTemplate.opsForZSet().add("Ranking", rankingInfo2, 19); + + UpdateRanking myRanking = UpdateRanking.builder() + .score(1L) + .rankingInfo(RankingInfo.builder() + .nickname(members.get(0).getNickname()) + .memberId(members.get(0).getId()) + .image(members.get(0).getProfileImage()).build()) + .build(); + + // when + mockMvc.perform(MockMvcRequestBuilders.get("/rankings")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.topRankings", hasSize(10))) + .andExpect(jsonPath("$.myRanking.nickname", is(members.get(0).getNickname()))) + .andExpect(jsonPath("$.myRanking.rank", is(21))); + + // then + + } + +} diff --git a/src/test/java/com/moabam/api/presentation/ReportControllerTest.java b/src/test/java/com/moabam/api/presentation/ReportControllerTest.java index 006eb10a..4f5c688d 100644 --- a/src/test/java/com/moabam/api/presentation/ReportControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/ReportControllerTest.java @@ -110,7 +110,7 @@ void reports_success(boolean roomFilter, boolean certificationFilter) throws Exc @Test void reports_failBy_subject_null() throws Exception { // given - Member member = MemberFixture.member("2", "ji"); + Member member = MemberFixture.member("2"); memberRepository.save(member); ReportRequest reportRequest = ReportFixture.reportRequest(member.getId(), null, null); @@ -128,7 +128,7 @@ void reports_failBy_subject_null() throws Exception { @Test void reports_failBy_member() throws Exception { // given - Member newMember = MemberFixture.member("9999", "n"); + Member newMember = MemberFixture.member("9999"); memberRepository.save(newMember); newMember.delete(LocalDateTime.now()); diff --git a/src/test/java/com/moabam/api/presentation/RoomControllerTest.java b/src/test/java/com/moabam/api/presentation/RoomControllerTest.java index 0577f001..9d6b965f 100644 --- a/src/test/java/com/moabam/api/presentation/RoomControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/RoomControllerTest.java @@ -794,8 +794,8 @@ void get_room_details_test() throws Exception { Participant participant1 = RoomFixture.participant(room, 1L); participant1.enableManager(); - Member member2 = MemberFixture.member("2", "NICK2"); - Member member3 = MemberFixture.member("3", "NICK3"); + Member member2 = MemberFixture.member("2"); + Member member3 = MemberFixture.member("3"); roomRepository.save(room); routineRepository.saveAll(routines); @@ -860,7 +860,7 @@ void get_room_details_test() throws Exception { void deport_member_success() throws Exception { // given Room room = RoomFixture.room(); - Member member = MemberFixture.member("1234", "참여자"); + Member member = MemberFixture.member("1234"); memberRepository.save(member); Participant memberParticipant = RoomFixture.participant(room, member.getId()); @@ -911,7 +911,7 @@ void deport_self_fail() throws Exception { @Test void mandate_manager_success() throws Exception { // given - Member member2 = MemberFixture.member("1234", "방장될 멤버"); + Member member2 = MemberFixture.member("1234"); memberRepository.save(member2); Room room = RoomFixture.room(); @@ -1011,7 +1011,7 @@ void get_un_joined_room_details() throws Exception { Room room = RoomFixture.room("테스트 방", NIGHT, 21); Room savedRoom = roomRepository.save(room); - Member member1 = MemberFixture.member("901010", "testtest"); + Member member1 = MemberFixture.member("901010"); member1 = memberRepository.save(member1); Item item = ItemFixture.nightMageSkin(); @@ -1531,8 +1531,8 @@ void search_first_page_all_rooms_by_keyword_roomType_roomId_success() throws Exc @Test void get_room_details_before_modification_success() throws Exception { // given - Member member2 = MemberFixture.member("123", "참여자1"); - Member member3 = MemberFixture.member("456", "참여자2"); + Member member2 = MemberFixture.member("123"); + Member member3 = MemberFixture.member("456"); member2 = memberRepository.save(member2); member3 = memberRepository.save(member3); diff --git a/src/test/java/com/moabam/support/fixture/MemberFixture.java b/src/test/java/com/moabam/support/fixture/MemberFixture.java index 3bf73d15..c0ed7551 100644 --- a/src/test/java/com/moabam/support/fixture/MemberFixture.java +++ b/src/test/java/com/moabam/support/fixture/MemberFixture.java @@ -30,7 +30,7 @@ public static Member member(Bug bug) { .build(); } - public static Member member(String socialId, String nickname) { + public static Member member(String socialId) { return Member.builder() .socialId(socialId) .bug(BugFixture.bug()) From f585949696323186be62a7a3347f43ea926c4244 Mon Sep 17 00:00:00 2001 From: parksey Date: Thu, 30 Nov 2023 12:48:50 +0900 Subject: [PATCH 05/11] =?UTF-8?q?style:=20checkstyle=20=EC=97=90=EB=9F=AC?= =?UTF-8?q?=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/moabam/global/config/EmbeddedRedisConfig.java | 2 +- .../com/moabam/api/application/ranking/RankingServiceTest.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/moabam/global/config/EmbeddedRedisConfig.java b/src/main/java/com/moabam/global/config/EmbeddedRedisConfig.java index ce82d3c1..a823fd26 100644 --- a/src/main/java/com/moabam/global/config/EmbeddedRedisConfig.java +++ b/src/main/java/com/moabam/global/config/EmbeddedRedisConfig.java @@ -68,7 +68,7 @@ public RedisTemplate redisTemplate(RedisConnectionFactory redisC public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModules(new JavaTimeModule()); - + return objectMapper; } diff --git a/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java b/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java index b33c2f9d..9a3f3204 100644 --- a/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java +++ b/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java @@ -210,7 +210,6 @@ void getTopRankings() { .score(1L) .rankingInfo(RankingInfo.builder().nickname("Hello1").memberId(1L).image("123").build()) .build(); - // When TopRankingResponses topRankingResponses = rankingService.getMemberRanking(myRanking); From f5783bb55cc84395d084e8cab9c306617cfbb4d3 Mon Sep 17 00:00:00 2001 From: parksey Date: Thu, 30 Nov 2023 14:15:46 +0900 Subject: [PATCH 06/11] =?UTF-8?q?refactor:=20=EC=9D=91=EB=8B=B5=20?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=EB=AA=85=20=EB=B3=80=EA=B2=BD=20TopRankingIn?= =?UTF-8?q?foResponse=20->=20TopRankingInfo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/application/ranking/RankingMapper.java | 14 +++++++------- .../api/application/ranking/RankingService.java | 14 +++++++------- ...ankingInfoResponse.java => TopRankingInfo.java} | 2 +- .../api/dto/ranking/TopRankingResponses.java | 4 ++-- .../application/ranking/RankingServiceTest.java | 6 +++--- 5 files changed, 20 insertions(+), 20 deletions(-) rename src/main/java/com/moabam/api/dto/ranking/{TopRankingInfoResponse.java => TopRankingInfo.java} (79%) diff --git a/src/main/java/com/moabam/api/application/ranking/RankingMapper.java b/src/main/java/com/moabam/api/application/ranking/RankingMapper.java index eae3b65c..7dcef586 100644 --- a/src/main/java/com/moabam/api/application/ranking/RankingMapper.java +++ b/src/main/java/com/moabam/api/application/ranking/RankingMapper.java @@ -3,7 +3,7 @@ import java.util.List; import com.moabam.api.dto.ranking.RankingInfo; -import com.moabam.api.dto.ranking.TopRankingInfoResponse; +import com.moabam.api.dto.ranking.TopRankingInfo; import com.moabam.api.dto.ranking.TopRankingResponses; import com.moabam.api.dto.ranking.UpdateRanking; @@ -13,8 +13,8 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) public class RankingMapper { - public static TopRankingInfoResponse topRankingResponse(int rank, long score, RankingInfo rankInfo) { - return TopRankingInfoResponse.builder() + public static TopRankingInfo topRankingResponse(int rank, long score, RankingInfo rankInfo) { + return TopRankingInfo.builder() .rank(rank) .score(score) .nickname(rankInfo.nickname()) @@ -23,8 +23,8 @@ public static TopRankingInfoResponse topRankingResponse(int rank, long score, Ra .build(); } - public static TopRankingInfoResponse topRankingResponse(int rank, UpdateRanking updateRanking) { - return TopRankingInfoResponse.builder() + public static TopRankingInfo topRankingResponse(int rank, UpdateRanking updateRanking) { + return TopRankingInfo.builder() .rank(rank) .score(updateRanking.score()) .nickname(updateRanking.rankingInfo().nickname()) @@ -33,8 +33,8 @@ public static TopRankingInfoResponse topRankingResponse(int rank, UpdateRanking .build(); } - public static TopRankingResponses topRankingResponses(TopRankingInfoResponse myRanking, - List topRankings) { + public static TopRankingResponses topRankingResponses(TopRankingInfo myRanking, + List topRankings) { return TopRankingResponses.builder() .topRankings(topRankings) .myRanking(myRanking) diff --git a/src/main/java/com/moabam/api/application/ranking/RankingService.java b/src/main/java/com/moabam/api/application/ranking/RankingService.java index ebe344f2..f784b84b 100644 --- a/src/main/java/com/moabam/api/application/ranking/RankingService.java +++ b/src/main/java/com/moabam/api/application/ranking/RankingService.java @@ -12,7 +12,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.moabam.api.dto.ranking.RankingInfo; -import com.moabam.api.dto.ranking.TopRankingInfoResponse; +import com.moabam.api.dto.ranking.TopRankingInfo; import com.moabam.api.dto.ranking.TopRankingResponses; import com.moabam.api.dto.ranking.UpdateRanking; import com.moabam.api.infrastructure.redis.ZSetRedisRepository; @@ -48,29 +48,29 @@ public void removeRanking(RankingInfo rankingInfo) { } public TopRankingResponses getMemberRanking(UpdateRanking myRankingInfo) { - List topRankings = getTopRankings(); + List topRankings = getTopRankings(); Long myRanking = zSetRedisRepository.reverseRank(RANKING, myRankingInfo.rankingInfo()); - TopRankingInfoResponse myRankingInfoResponse = + TopRankingInfo myRankingInfoResponse = RankingMapper.topRankingResponse(myRanking.intValue(), myRankingInfo); return RankingMapper.topRankingResponses(myRankingInfoResponse, topRankings); } - private List getTopRankings() { + private List getTopRankings() { Set> topRankings = zSetRedisRepository.rangeJson(RANKING, START_INDEX, LIMIT_INDEX); Set scoreSet = new HashSet<>(); - List topRankingInfoRespons = new ArrayList<>(); + List topRankingInfo = new ArrayList<>(); for (ZSetOperations.TypedTuple topRanking : topRankings) { long score = requireNonNull(topRanking.getScore()).longValue(); scoreSet.add(score); RankingInfo rankingInfo = objectMapper.convertValue(topRanking.getValue(), RankingInfo.class); - topRankingInfoRespons.add(RankingMapper.topRankingResponse(scoreSet.size(), score, rankingInfo)); + topRankingInfo.add(RankingMapper.topRankingResponse(scoreSet.size(), score, rankingInfo)); } - return topRankingInfoRespons; + return topRankingInfo; } } diff --git a/src/main/java/com/moabam/api/dto/ranking/TopRankingInfoResponse.java b/src/main/java/com/moabam/api/dto/ranking/TopRankingInfo.java similarity index 79% rename from src/main/java/com/moabam/api/dto/ranking/TopRankingInfoResponse.java rename to src/main/java/com/moabam/api/dto/ranking/TopRankingInfo.java index ed0d3857..bcd56ff2 100644 --- a/src/main/java/com/moabam/api/dto/ranking/TopRankingInfoResponse.java +++ b/src/main/java/com/moabam/api/dto/ranking/TopRankingInfo.java @@ -3,7 +3,7 @@ import lombok.Builder; @Builder -public record TopRankingInfoResponse( +public record TopRankingInfo( int rank, Long memberId, Long score, diff --git a/src/main/java/com/moabam/api/dto/ranking/TopRankingResponses.java b/src/main/java/com/moabam/api/dto/ranking/TopRankingResponses.java index da1dd863..a5644c7f 100644 --- a/src/main/java/com/moabam/api/dto/ranking/TopRankingResponses.java +++ b/src/main/java/com/moabam/api/dto/ranking/TopRankingResponses.java @@ -6,8 +6,8 @@ @Builder public record TopRankingResponses( - List topRankings, - TopRankingInfoResponse myRanking + List topRankings, + TopRankingInfo myRanking ) { } diff --git a/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java b/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java index 9a3f3204..d433aaa3 100644 --- a/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java +++ b/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java @@ -18,7 +18,7 @@ import com.moabam.api.application.member.MemberMapper; import com.moabam.api.domain.member.Member; import com.moabam.api.dto.ranking.RankingInfo; -import com.moabam.api.dto.ranking.TopRankingInfoResponse; +import com.moabam.api.dto.ranking.TopRankingInfo; import com.moabam.api.dto.ranking.TopRankingResponses; import com.moabam.api.dto.ranking.UpdateRanking; import com.moabam.api.infrastructure.redis.ZSetRedisRepository; @@ -214,8 +214,8 @@ void getTopRankings() { TopRankingResponses topRankingResponses = rankingService.getMemberRanking(myRanking); // Then - List topRankings = topRankingResponses.topRankings(); - TopRankingInfoResponse myRank = topRankingResponses.myRanking(); + List topRankings = topRankingResponses.topRankings(); + TopRankingInfo myRank = topRankingResponses.myRanking(); assertAll(() -> assertThat(topRankings).hasSize(10), () -> assertThat(myRank.score()).isEqualTo(1), () -> assertThat(topRankings.get(0).rank()).isEqualTo(1), () -> assertThat(topRankings.get(1).rank()).isEqualTo(1), From b0c12ca4d3cfe2b1c1e5bfacc0511e2772bd26ac Mon Sep 17 00:00:00 2001 From: parksey Date: Thu, 30 Nov 2023 14:19:24 +0900 Subject: [PATCH 07/11] =?UTF-8?q?fix:=20=EB=9E=AD=ED=82=B9=20=EC=97=85?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=8A=B8=20=EC=8B=9C=EA=B0=84=2015=EB=B6=84?= =?UTF-8?q?=20=EB=A7=A4=EC=B4=88=EB=A7=88=EB=8B=A4=20=EB=8F=99=EC=9E=91?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EB=B0=A9=EC=8B=9D=20->=2015=EB=B6=84?= =?UTF-8?q?=EC=97=90=20=ED=95=9C=20=EB=B2=88=EB=A7=8C=20=EC=8B=A4=ED=96=89?= =?UTF-8?q?=EB=90=98=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/moabam/api/application/member/MemberService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/moabam/api/application/member/MemberService.java b/src/main/java/com/moabam/api/application/member/MemberService.java index f4fc0ec0..358cf2df 100644 --- a/src/main/java/com/moabam/api/application/member/MemberService.java +++ b/src/main/java/com/moabam/api/application/member/MemberService.java @@ -134,7 +134,7 @@ public UpdateRanking getRankingInfo(AuthMember authMember) { return MemberMapper.toUpdateRanking(member); } - @Scheduled(cron = "* 15 * * * *") + @Scheduled(cron = "0 15 * * * *") public void updateAllRanking() { List members = memberSearchRepository.findAllMembers(); List updateRankings = members.stream() From 286820bfed2b05fe75923ec67a309bb661fb1186 Mon Sep 17 00:00:00 2001 From: Park Seyeon Date: Thu, 30 Nov 2023 16:13:56 +0900 Subject: [PATCH 08/11] =?UTF-8?q?refactor:=20=EB=9E=AD=ED=82=B9=20?= =?UTF-8?q?=EC=9D=91=EB=8B=B5=20=EB=B0=98=ED=99=98=20=EA=B0=9D=EC=B2=B4=20?= =?UTF-8?q?=EB=B3=80=EC=88=98=EB=A9=B4=20s=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kim Heebin --- .../java/com/moabam/api/dto/ranking/TopRankingResponses.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/moabam/api/dto/ranking/TopRankingResponses.java b/src/main/java/com/moabam/api/dto/ranking/TopRankingResponses.java index a5644c7f..38663842 100644 --- a/src/main/java/com/moabam/api/dto/ranking/TopRankingResponses.java +++ b/src/main/java/com/moabam/api/dto/ranking/TopRankingResponses.java @@ -5,7 +5,7 @@ import lombok.Builder; @Builder -public record TopRankingResponses( +public record TopRankingResponse( List topRankings, TopRankingInfo myRanking ) { From 431c95b6f2b973597d8ba86c13b08a01a31c7729 Mon Sep 17 00:00:00 2001 From: parksey Date: Thu, 30 Nov 2023 16:17:01 +0900 Subject: [PATCH 09/11] =?UTF-8?q?refactor:=20ToprankingResponses=20?= =?UTF-8?q?=EC=9D=91=EB=8B=B5=20=EA=B0=9D=EC=B2=B4=20=EB=B0=98=ED=99=98?= =?UTF-8?q?=EB=AA=85=20TopRankingResponse=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/moabam/api/application/ranking/RankingMapper.java | 6 +++--- .../moabam/api/application/ranking/RankingService.java | 4 ++-- .../{TopRankingResponses.java => TopRankingResponse.java} | 0 .../com/moabam/api/presentation/RankingController.java | 4 ++-- .../api/application/ranking/RankingServiceTest.java | 8 ++++---- 5 files changed, 11 insertions(+), 11 deletions(-) rename src/main/java/com/moabam/api/dto/ranking/{TopRankingResponses.java => TopRankingResponse.java} (100%) diff --git a/src/main/java/com/moabam/api/application/ranking/RankingMapper.java b/src/main/java/com/moabam/api/application/ranking/RankingMapper.java index 7dcef586..06ad3389 100644 --- a/src/main/java/com/moabam/api/application/ranking/RankingMapper.java +++ b/src/main/java/com/moabam/api/application/ranking/RankingMapper.java @@ -4,7 +4,7 @@ import com.moabam.api.dto.ranking.RankingInfo; import com.moabam.api.dto.ranking.TopRankingInfo; -import com.moabam.api.dto.ranking.TopRankingResponses; +import com.moabam.api.dto.ranking.TopRankingResponse; import com.moabam.api.dto.ranking.UpdateRanking; import lombok.AccessLevel; @@ -33,9 +33,9 @@ public static TopRankingInfo topRankingResponse(int rank, UpdateRanking updateRa .build(); } - public static TopRankingResponses topRankingResponses(TopRankingInfo myRanking, + public static TopRankingResponse topRankingResponses(TopRankingInfo myRanking, List topRankings) { - return TopRankingResponses.builder() + return TopRankingResponse.builder() .topRankings(topRankings) .myRanking(myRanking) .build(); diff --git a/src/main/java/com/moabam/api/application/ranking/RankingService.java b/src/main/java/com/moabam/api/application/ranking/RankingService.java index f784b84b..aa48370d 100644 --- a/src/main/java/com/moabam/api/application/ranking/RankingService.java +++ b/src/main/java/com/moabam/api/application/ranking/RankingService.java @@ -13,7 +13,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.moabam.api.dto.ranking.RankingInfo; import com.moabam.api.dto.ranking.TopRankingInfo; -import com.moabam.api.dto.ranking.TopRankingResponses; +import com.moabam.api.dto.ranking.TopRankingResponse; import com.moabam.api.dto.ranking.UpdateRanking; import com.moabam.api.infrastructure.redis.ZSetRedisRepository; @@ -47,7 +47,7 @@ public void removeRanking(RankingInfo rankingInfo) { zSetRedisRepository.delete(RANKING, rankingInfo); } - public TopRankingResponses getMemberRanking(UpdateRanking myRankingInfo) { + public TopRankingResponse getMemberRanking(UpdateRanking myRankingInfo) { List topRankings = getTopRankings(); Long myRanking = zSetRedisRepository.reverseRank(RANKING, myRankingInfo.rankingInfo()); TopRankingInfo myRankingInfoResponse = diff --git a/src/main/java/com/moabam/api/dto/ranking/TopRankingResponses.java b/src/main/java/com/moabam/api/dto/ranking/TopRankingResponse.java similarity index 100% rename from src/main/java/com/moabam/api/dto/ranking/TopRankingResponses.java rename to src/main/java/com/moabam/api/dto/ranking/TopRankingResponse.java diff --git a/src/main/java/com/moabam/api/presentation/RankingController.java b/src/main/java/com/moabam/api/presentation/RankingController.java index 68796632..d218ca5d 100644 --- a/src/main/java/com/moabam/api/presentation/RankingController.java +++ b/src/main/java/com/moabam/api/presentation/RankingController.java @@ -8,7 +8,7 @@ import com.moabam.api.application.member.MemberService; import com.moabam.api.application.ranking.RankingService; -import com.moabam.api.dto.ranking.TopRankingResponses; +import com.moabam.api.dto.ranking.TopRankingResponse; import com.moabam.api.dto.ranking.UpdateRanking; import com.moabam.global.auth.annotation.Auth; import com.moabam.global.auth.model.AuthMember; @@ -25,7 +25,7 @@ public class RankingController { @GetMapping @ResponseStatus(HttpStatus.OK) - public TopRankingResponses getRanking(@Auth AuthMember authMember) { + public TopRankingResponse getRanking(@Auth AuthMember authMember) { UpdateRanking rankingInfo = memberService.getRankingInfo(authMember); return rankingService.getMemberRanking(rankingInfo); diff --git a/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java b/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java index d433aaa3..d246c5df 100644 --- a/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java +++ b/src/test/java/com/moabam/api/application/ranking/RankingServiceTest.java @@ -19,7 +19,7 @@ import com.moabam.api.domain.member.Member; import com.moabam.api.dto.ranking.RankingInfo; import com.moabam.api.dto.ranking.TopRankingInfo; -import com.moabam.api.dto.ranking.TopRankingResponses; +import com.moabam.api.dto.ranking.TopRankingResponse; import com.moabam.api.dto.ranking.UpdateRanking; import com.moabam.api.infrastructure.redis.ZSetRedisRepository; import com.moabam.global.config.EmbeddedRedisConfig; @@ -211,11 +211,11 @@ void getTopRankings() { .rankingInfo(RankingInfo.builder().nickname("Hello1").memberId(1L).image("123").build()) .build(); // When - TopRankingResponses topRankingResponses = rankingService.getMemberRanking(myRanking); + TopRankingResponse topRankingResponse = rankingService.getMemberRanking(myRanking); // Then - List topRankings = topRankingResponses.topRankings(); - TopRankingInfo myRank = topRankingResponses.myRanking(); + List topRankings = topRankingResponse.topRankings(); + TopRankingInfo myRank = topRankingResponse.myRanking(); assertAll(() -> assertThat(topRankings).hasSize(10), () -> assertThat(myRank.score()).isEqualTo(1), () -> assertThat(topRankings.get(0).rank()).isEqualTo(1), () -> assertThat(topRankings.get(1).rank()).isEqualTo(1), From 2b8f7217656cdd03e77966190b9f8b9408c71e2b Mon Sep 17 00:00:00 2001 From: parksey Date: Thu, 30 Nov 2023 19:01:10 +0900 Subject: [PATCH 10/11] =?UTF-8?q?fix:=20ObjectMapper=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/moabam/global/config/EmbeddedRedisConfig.java | 4 +++- src/main/java/com/moabam/global/config/RedisConfig.java | 8 -------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/moabam/global/config/EmbeddedRedisConfig.java b/src/main/java/com/moabam/global/config/EmbeddedRedisConfig.java index a823fd26..85e7f64a 100644 --- a/src/main/java/com/moabam/global/config/EmbeddedRedisConfig.java +++ b/src/main/java/com/moabam/global/config/EmbeddedRedisConfig.java @@ -9,6 +9,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; +import org.springframework.core.annotation.Order; import org.springframework.core.io.ClassPathResource; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; @@ -64,8 +65,9 @@ public RedisTemplate redisTemplate(RedisConnectionFactory redisC return redisTemplate; } + @Order(2) @Bean - public ObjectMapper objectMapper() { + public ObjectMapper objectRedisMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModules(new JavaTimeModule()); diff --git a/src/main/java/com/moabam/global/config/RedisConfig.java b/src/main/java/com/moabam/global/config/RedisConfig.java index cc412271..0e9cf0fb 100644 --- a/src/main/java/com/moabam/global/config/RedisConfig.java +++ b/src/main/java/com/moabam/global/config/RedisConfig.java @@ -38,12 +38,4 @@ public RedisTemplate redisTemplate(RedisConnectionFactory redisC return redisTemplate; } - - @Bean - public ObjectMapper objectMapper() { - ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.registerModules(new JavaTimeModule()); - - return objectMapper; - } } From 3c995d4d328838caa2ba3c0842e93c73456074a7 Mon Sep 17 00:00:00 2001 From: parksey Date: Thu, 30 Nov 2023 19:19:33 +0900 Subject: [PATCH 11/11] =?UTF-8?q?fix:=20objectMapper=20=EC=82=AD=EC=A0=9C?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/moabam/global/config/RedisConfig.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/main/java/com/moabam/global/config/RedisConfig.java b/src/main/java/com/moabam/global/config/RedisConfig.java index cc412271..9d017cce 100644 --- a/src/main/java/com/moabam/global/config/RedisConfig.java +++ b/src/main/java/com/moabam/global/config/RedisConfig.java @@ -10,9 +10,6 @@ import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; - @Configuration @Profile("!test") public class RedisConfig { @@ -38,12 +35,4 @@ public RedisTemplate redisTemplate(RedisConnectionFactory redisC return redisTemplate; } - - @Bean - public ObjectMapper objectMapper() { - ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.registerModules(new JavaTimeModule()); - - return objectMapper; - } }