From 6eb5796fc195d8a64be620f8e168d8a7cce6800d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Mon, 11 Nov 2024 16:40:30 +0900 Subject: [PATCH 01/31] add :: Rank.java --- .../domaserver/domain/rank/entity/Rank.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/main/java/com/example/domaserver/domain/rank/entity/Rank.java diff --git a/src/main/java/com/example/domaserver/domain/rank/entity/Rank.java b/src/main/java/com/example/domaserver/domain/rank/entity/Rank.java new file mode 100644 index 0000000..e1d40e7 --- /dev/null +++ b/src/main/java/com/example/domaserver/domain/rank/entity/Rank.java @@ -0,0 +1,31 @@ +package com.example.domaserver.domain.rank.entity; + +import com.example.domaserver.domain.user.entity.User; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class Rank { + + @Id + @GeneratedValue(strategy =GenerationType.IDENTITY) + private Long RankId; + private int PenaltyPoints; + private double RankScore; + + @ManyToOne + @JoinColumn(name = "user_id") + private User user; + + public Rank(Long rankId, Double rankScore) { + RankId = rankId; + RankScore = rankScore; + } +} From 1e8da19f803f2d76971d670acb6704c4872b79c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Mon, 11 Nov 2024 16:41:20 +0900 Subject: [PATCH 02/31] add :: RankController.java --- .../rank/presentation/RankController.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/main/java/com/example/domaserver/domain/rank/presentation/RankController.java diff --git a/src/main/java/com/example/domaserver/domain/rank/presentation/RankController.java b/src/main/java/com/example/domaserver/domain/rank/presentation/RankController.java new file mode 100644 index 0000000..8d858a9 --- /dev/null +++ b/src/main/java/com/example/domaserver/domain/rank/presentation/RankController.java @@ -0,0 +1,46 @@ +package com.example.domaserver.domain.rank.presentation; + + +import com.example.domaserver.domain.rank.entity.Rank; +import com.example.domaserver.domain.rank.service.RankService; +import com.example.domaserver.domain.user.entity.User; +import com.example.domaserver.domain.user.service.UserService; +import com.example.domaserver.global.security.jwt.JwtService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/home") +public class RankController { + private final RankService rankService; + private final UserService userService; + private final JwtService jwtService; + + @Autowired + public RankController(RankService rankService, UserService userService, JwtService jwtService) { + this.rankService = rankService; + this.userService = userService; + this.jwtService = jwtService; + } + + @GetMapping("/rank") + public ResponseEntity> getRanks() { + List topRanks = rankService.getTopRanking(25); + return ResponseEntity.ok(topRanks); + } + + @GetMapping("/my-rank") + public ResponseEntity getMyRank(@RequestHeader("Authorization") String token) { + User user = jwtService.getUserFromToken(token.substring(7)); + Long rank = rankService.getRanking(user); + return ResponseEntity.ok(rank); + } + + +} From 7dce3ef63a6820b00e7d46745e062c39c149c726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Mon, 11 Nov 2024 16:42:02 +0900 Subject: [PATCH 03/31] add :: RankResponse.java --- .../presentation/dto/response/RankResponse.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/main/java/com/example/domaserver/domain/rank/presentation/dto/response/RankResponse.java diff --git a/src/main/java/com/example/domaserver/domain/rank/presentation/dto/response/RankResponse.java b/src/main/java/com/example/domaserver/domain/rank/presentation/dto/response/RankResponse.java new file mode 100644 index 0000000..6296953 --- /dev/null +++ b/src/main/java/com/example/domaserver/domain/rank/presentation/dto/response/RankResponse.java @@ -0,0 +1,16 @@ +package com.example.domaserver.domain.rank.presentation.dto.response; + +import lombok.Builder; +import lombok.Getter; + +import java.util.UUID; + +@Builder +@Getter +public class RankResponse { + private UUID Id; + private String name; + private String profileImageUrl; + private int penaltyPoints; + private double RankScore; +} From 0ef14a46613b6ce0b982fbdda7fc1c1720d1562d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Mon, 11 Nov 2024 16:42:25 +0900 Subject: [PATCH 04/31] add :: RankServiceImpl.java --- .../rank/service/impl/RankServiceImpl.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/main/java/com/example/domaserver/domain/rank/service/impl/RankServiceImpl.java diff --git a/src/main/java/com/example/domaserver/domain/rank/service/impl/RankServiceImpl.java b/src/main/java/com/example/domaserver/domain/rank/service/impl/RankServiceImpl.java new file mode 100644 index 0000000..d0e38f1 --- /dev/null +++ b/src/main/java/com/example/domaserver/domain/rank/service/impl/RankServiceImpl.java @@ -0,0 +1,52 @@ +package com.example.domaserver.domain.rank.service.impl; + +import com.example.domaserver.domain.rank.entity.Rank; +import com.example.domaserver.domain.rank.service.RankService; +import com.example.domaserver.domain.user.entity.User; +import com.example.domaserver.global.annotation.ServiceWithTransaction; +import jakarta.transaction.Transactional; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ZSetOperations; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + + +@ServiceWithTransaction +public class RankServiceImpl implements RankService { + + private static final String RANK_KEY = "USER_RANKING"; + + private final RedisTemplate redisTemplate; + + public RankServiceImpl(@Qualifier("redisTemplate") RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + @Transactional + public Long getRanking(User user) { + Long rank = redisTemplate.opsForZSet().rank(RANK_KEY, user.getId()); + return (rank != null) ? rank + 1 : null; + } + + @Transactional + public void updateRank(Rank rank) { + redisTemplate.opsForZSet().add(RANK_KEY, rank.getUser().getId(), rank.getPenaltyPoints()); + } + + @Transactional + public List getTopRanking(int topN) { + Set> rankedUsers = + redisTemplate.opsForZSet().reverseRangeWithScores(RANK_KEY, 0, topN - 1); + + List userRanks = new ArrayList<>(); + for (ZSetOperations.TypedTuple entry : rankedUsers) { + Long RankId = entry.getValue(); + Double RankScore = entry.getScore(); + userRanks.add(new Rank(RankId, RankScore)); + } + return userRanks; + } + +} From 91ac7c2eb7e704d62ffc6f88650ead61126ffebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Mon, 11 Nov 2024 16:42:43 +0900 Subject: [PATCH 05/31] add :: RankService.java --- .../domaserver/domain/rank/service/RankService.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/main/java/com/example/domaserver/domain/rank/service/RankService.java diff --git a/src/main/java/com/example/domaserver/domain/rank/service/RankService.java b/src/main/java/com/example/domaserver/domain/rank/service/RankService.java new file mode 100644 index 0000000..6e3f501 --- /dev/null +++ b/src/main/java/com/example/domaserver/domain/rank/service/RankService.java @@ -0,0 +1,11 @@ +package com.example.domaserver.domain.rank.service; + +import com.example.domaserver.domain.rank.entity.Rank; +import com.example.domaserver.domain.user.entity.User; +import java.util.List; + +public interface RankService { + Long getRanking(User user); + void updateRank(Rank rank); + List getTopRanking(int topN); +} From 210b0d07efb7c436b144b7b72b3e4d28e8ffd38b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Mon, 11 Nov 2024 16:43:56 +0900 Subject: [PATCH 06/31] add :: RedisConfig.java --- .../domain/rank/config/RedisConfig.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/com/example/domaserver/domain/rank/config/RedisConfig.java diff --git a/src/main/java/com/example/domaserver/domain/rank/config/RedisConfig.java b/src/main/java/com/example/domaserver/domain/rank/config/RedisConfig.java new file mode 100644 index 0000000..7dd6e41 --- /dev/null +++ b/src/main/java/com/example/domaserver/domain/rank/config/RedisConfig.java @@ -0,0 +1,23 @@ +package com.example.domaserver.domain.rank.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; + +@EnableRedisRepositories +@Configuration +public class RedisConfig { + @Value("${spring.redis.host}") + private String redisHost; + + @Value("${spring.redis.port}") + private int redisPort; + + @Bean + public RedisConnectionFactory redisConnectionFactory() { + return new LettuceConnectionFactory(redisHost, redisPort); + } +} From 7855b496880ffe811a1b4af4e08850822ba89392 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Mon, 11 Nov 2024 16:44:19 +0900 Subject: [PATCH 07/31] add :: JwtService.java --- .../global/security/jwt/JwtService.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/main/java/com/example/domaserver/global/security/jwt/JwtService.java diff --git a/src/main/java/com/example/domaserver/global/security/jwt/JwtService.java b/src/main/java/com/example/domaserver/global/security/jwt/JwtService.java new file mode 100644 index 0000000..d05e71c --- /dev/null +++ b/src/main/java/com/example/domaserver/global/security/jwt/JwtService.java @@ -0,0 +1,62 @@ +package com.example.domaserver.global.security.jwt; + + +import com.example.domaserver.domain.user.entity.User; +import com.example.domaserver.domain.user.service.UserService; +import io.jsonwebtoken.Claims; +import io.jsonwebtoken.Jwts; +import lombok.extern.slf4j.Slf4j; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.stereotype.Service; +import org.springframework.beans.factory.annotation.Value; + +import java.util.Date; +import java.util.function.Function; + +@Service +@Slf4j +public class JwtService { + + private final UserService userService; + + @Value("${jwt.secret}") + private String secretKey; + + public JwtService(UserService userService) { + this.userService = userService; + } + + public String extractUsername(String token) { + return extractClaim(token, Claims::getSubject); + } + + public T extractClaim(String token, Function claimsResolver) { + final Claims claims = extractAllClaims(token); + return claimsResolver.apply(claims); + } + + private Claims extractAllClaims(String token) { + return Jwts.parser() + .setSigningKey(secretKey) + .parseClaimsJws(token) + .getBody(); + } + + public boolean isTokenValid(String token, UserDetails userDetails) { + final String username = extractUsername(token); + return (username.equals(userDetails.getUsername()) && !isTokenExpired(token)); + } + + private boolean isTokenExpired(String token) { + return extractExpiration(token).before(new Date()); + } + + public Date extractExpiration(String token) { + return extractClaim(token, Claims::getExpiration); + } + + public User getUserFromToken(String token) { + String username = extractUsername(token); + return userService.findByUsername(username); + } +} From bc65f7af2900182c3519bd5103de7426e2e2fef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Mon, 11 Nov 2024 16:47:28 +0900 Subject: [PATCH 08/31] add :: UserServiceImpl.java --- .../user/service/impl/UserServiceImpl.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/main/java/com/example/domaserver/domain/user/service/impl/UserServiceImpl.java diff --git a/src/main/java/com/example/domaserver/domain/user/service/impl/UserServiceImpl.java b/src/main/java/com/example/domaserver/domain/user/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..dd76b5e --- /dev/null +++ b/src/main/java/com/example/domaserver/domain/user/service/impl/UserServiceImpl.java @@ -0,0 +1,34 @@ +package com.example.domaserver.domain.user.service.impl; + +import com.example.domaserver.domain.user.entity.User; +import com.example.domaserver.domain.user.repository.UserRepository; +import com.example.domaserver.domain.user.service.UserService; +import com.example.domaserver.global.annotation.ServiceWithTransaction; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.userdetails.UsernameNotFoundException; + +import java.util.UUID; + +@ServiceWithTransaction +public class UserServiceImpl implements UserService { + + private final UserRepository userRepository; + + @Autowired + public UserServiceImpl(UserRepository userRepository) { + this.userRepository = userRepository; + } + + @Override + public User findByUsername(String username) { + return userRepository.findByUsername(username) + .orElseThrow(() -> new UsernameNotFoundException("User not found with username: " + username)); + } + + @Override + public User findById(UUID id) { + return userRepository.findById(id) + .orElseThrow(() -> new UsernameNotFoundException("User not found with id: " + id)); + } + +} From a2f9ec8a4f8c359d1eac8217433a8c573e2682b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Mon, 11 Nov 2024 20:21:15 +0900 Subject: [PATCH 09/31] add :: UserService.java --- .../domaserver/domain/user/service/UserService.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/com/example/domaserver/domain/user/service/UserService.java diff --git a/src/main/java/com/example/domaserver/domain/user/service/UserService.java b/src/main/java/com/example/domaserver/domain/user/service/UserService.java new file mode 100644 index 0000000..ceaabac --- /dev/null +++ b/src/main/java/com/example/domaserver/domain/user/service/UserService.java @@ -0,0 +1,10 @@ +package com.example.domaserver.domain.user.service; + +import com.example.domaserver.domain.user.entity.User; + +import java.util.UUID; + +public interface UserService { + User findByUsername(String name); + User findById(UUID id); +} From f2740262468c47969415adba1089f92e765009ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Mon, 11 Nov 2024 20:21:40 +0900 Subject: [PATCH 10/31] modify :: User.java --- .../java/com/example/domaserver/domain/user/entity/User.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/example/domaserver/domain/user/entity/User.java b/src/main/java/com/example/domaserver/domain/user/entity/User.java index 2ebb17e..08a7082 100644 --- a/src/main/java/com/example/domaserver/domain/user/entity/User.java +++ b/src/main/java/com/example/domaserver/domain/user/entity/User.java @@ -17,10 +17,9 @@ public class User { @Id @GeneratedValue(generator = "UUID4") private UUID id; - private String name; - private String email; + private int penaltyPoint; @Embedded private StudentNum studentNum; From 4cce7749e9b6c8fd6bffaa2eca158469bb61be26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Mon, 11 Nov 2024 20:21:53 +0900 Subject: [PATCH 11/31] modify :: UserRepository.java --- .../domaserver/domain/user/repository/UserRepository.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/example/domaserver/domain/user/repository/UserRepository.java b/src/main/java/com/example/domaserver/domain/user/repository/UserRepository.java index 01f917c..954d069 100644 --- a/src/main/java/com/example/domaserver/domain/user/repository/UserRepository.java +++ b/src/main/java/com/example/domaserver/domain/user/repository/UserRepository.java @@ -8,4 +8,5 @@ public interface UserRepository extends JpaRepository { Optional findByEmail(String email); + Optional findByUsername(String Username); } From 07e3606a08746deb1308ea025119b658675363c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Mon, 11 Nov 2024 20:22:08 +0900 Subject: [PATCH 12/31] modify :: build.gradle --- build.gradle | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.gradle b/build.gradle index ba5dfc5..7dfae56 100644 --- a/build.gradle +++ b/build.gradle @@ -43,6 +43,8 @@ dependencies { //redis implementation ("org.springframework.boot:spring-boot-starter-data-redis") + implementation ("org.springframework.boot:spring-boot-starter-data-redis:2.3.1.RELEASE") + // aws implementation("org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE") From a42a331767d2fc8fa287e78157818c700dd001ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Mon, 11 Nov 2024 20:22:25 +0900 Subject: [PATCH 13/31] modify :: application.yml --- src/main/resources/application.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 89f9ffc..717b008 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,4 +1,8 @@ server: + redis: + port: 6739 + host: localhost + port: 8090 servlet: context-path: / @@ -33,9 +37,9 @@ spring: port: ${REDIS_PORT} jwt: - secret: ${JWT_SECRET} + secret: "your_secret_key_here" gauth: clientId: ${GAUTH_CLIENT} clientSecret: ${GAUTH_SECRET} - redirectUri: ${GAUTH_REDIRECT_URI} \ No newline at end of file + redirectUri: ${GAUTH_REDIRECT_URI} From 14e7af45c8e90b82802366f31bcb22299b55b023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Thu, 14 Nov 2024 19:50:14 +0900 Subject: [PATCH 14/31] add :: GetTopRankingServiceImpl.java --- .../impl/GetTopRankingServiceImpl.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/main/java/com/example/domaserver/domain/rank/service/impl/GetTopRankingServiceImpl.java diff --git a/src/main/java/com/example/domaserver/domain/rank/service/impl/GetTopRankingServiceImpl.java b/src/main/java/com/example/domaserver/domain/rank/service/impl/GetTopRankingServiceImpl.java new file mode 100644 index 0000000..3aaebe8 --- /dev/null +++ b/src/main/java/com/example/domaserver/domain/rank/service/impl/GetTopRankingServiceImpl.java @@ -0,0 +1,41 @@ +package com.example.domaserver.domain.rank.service.impl; + +import com.example.domaserver.domain.rank.entity.Rank; +import com.example.domaserver.domain.rank.service.GetTopRankingService; +import com.example.domaserver.global.annotation.ServiceWithReadOnlyTransactional; +import com.example.domaserver.global.annotation.ServiceWithTransaction; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ZSetOperations; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + + +@ServiceWithTransaction +@ServiceWithReadOnlyTransactional +public class GetTopRankingServiceImpl implements GetTopRankingService { + + private static final String GET_TOP_RANKING = "getTopRanking"; + + private final RedisTemplate redisTemplate; + + public GetTopRankingServiceImpl(RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + public List getTopRanking(int topN) { + Set> rankedUsers = + redisTemplate.opsForZSet().reverseRange(GET_TOP_RANKING, 0, topN - 1); + + List userRanks = new ArrayList<>(); + for (ZSetOperations.TypedTuple entry : rankedUsers) { + Long RankId = entry.getValue(); + Double RankScore = entry.getScore(); + userRanks.add(new Rank(RankId, RankScore)); + } + + return userRanks; + } + +} From 823fdf7092433c356c3622949bb3060c51048a66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Thu, 14 Nov 2024 19:50:42 +0900 Subject: [PATCH 15/31] add :: GetTopRankingService.java --- .../domain/rank/service/GetTopRankingService.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/java/com/example/domaserver/domain/rank/service/GetTopRankingService.java diff --git a/src/main/java/com/example/domaserver/domain/rank/service/GetTopRankingService.java b/src/main/java/com/example/domaserver/domain/rank/service/GetTopRankingService.java new file mode 100644 index 0000000..f6fdd84 --- /dev/null +++ b/src/main/java/com/example/domaserver/domain/rank/service/GetTopRankingService.java @@ -0,0 +1,9 @@ +package com.example.domaserver.domain.rank.service; + +import com.example.domaserver.domain.rank.entity.Rank; + +import java.util.List; + +public interface GetTopRankingService { + List getTopRanking(int topN); +} From 2b1dd70d98908b5cd3d6bffed253650320a17c6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Thu, 14 Nov 2024 19:51:12 +0900 Subject: [PATCH 16/31] add :: GetRankingServiceImpl.java --- .../service/impl/GetRankingServiceImpl.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/main/java/com/example/domaserver/domain/rank/service/impl/GetRankingServiceImpl.java diff --git a/src/main/java/com/example/domaserver/domain/rank/service/impl/GetRankingServiceImpl.java b/src/main/java/com/example/domaserver/domain/rank/service/impl/GetRankingServiceImpl.java new file mode 100644 index 0000000..dd68b09 --- /dev/null +++ b/src/main/java/com/example/domaserver/domain/rank/service/impl/GetRankingServiceImpl.java @@ -0,0 +1,27 @@ +package com.example.domaserver.domain.rank.service.impl; + +import com.example.domaserver.domain.rank.service.GetRankingService; +import com.example.domaserver.domain.user.entity.User; +import com.example.domaserver.global.annotation.ServiceWithReadOnlyTransactional; +import com.example.domaserver.global.annotation.ServiceWithTransaction; +import org.springframework.data.redis.core.RedisTemplate; + + +@ServiceWithTransaction +@ServiceWithReadOnlyTransactional +public class GetRankingServiceImpl implements GetRankingService { + + private static final String GET_RANKING = "getRank"; + + private final RedisTemplate redisTemplate; + + public GetRankingServiceImpl(RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + public Long getRanking(User user) { + Long rank = redisTemplate.opsForZSet().rank(GET_RANKING, user.getId()); + return (rank != null) ? rank + 1 : null; + } + +} From 1f93210b5fe6938a2f3e70d6ce2f61317901123a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Thu, 14 Nov 2024 19:51:32 +0900 Subject: [PATCH 17/31] add :: GetRankingService.java --- .../domaserver/domain/rank/service/GetRankingService.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/java/com/example/domaserver/domain/rank/service/GetRankingService.java diff --git a/src/main/java/com/example/domaserver/domain/rank/service/GetRankingService.java b/src/main/java/com/example/domaserver/domain/rank/service/GetRankingService.java new file mode 100644 index 0000000..f5fa15b --- /dev/null +++ b/src/main/java/com/example/domaserver/domain/rank/service/GetRankingService.java @@ -0,0 +1,7 @@ +package com.example.domaserver.domain.rank.service; + +import com.example.domaserver.domain.user.entity.User; + +public interface GetRankingService { + Long getRanking(User user); +} From c2caf7c8fbe7e4b52eeae9fc58eaa1b0e77a8c07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Thu, 14 Nov 2024 19:51:55 +0900 Subject: [PATCH 18/31] add :: UpdateRankingServiceImpl.java --- .../impl/UpdateRankingServiceImpl.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/java/com/example/domaserver/domain/rank/service/impl/UpdateRankingServiceImpl.java diff --git a/src/main/java/com/example/domaserver/domain/rank/service/impl/UpdateRankingServiceImpl.java b/src/main/java/com/example/domaserver/domain/rank/service/impl/UpdateRankingServiceImpl.java new file mode 100644 index 0000000..4f468d4 --- /dev/null +++ b/src/main/java/com/example/domaserver/domain/rank/service/impl/UpdateRankingServiceImpl.java @@ -0,0 +1,25 @@ +package com.example.domaserver.domain.rank.service.impl; + +import com.example.domaserver.domain.rank.entity.Rank; +import com.example.domaserver.domain.rank.service.UpdateRankingService; +import com.example.domaserver.global.annotation.ServiceWithReadOnlyTransactional; +import com.example.domaserver.global.annotation.ServiceWithTransaction; +import org.springframework.data.redis.core.RedisTemplate; + + +@ServiceWithTransaction +public class UpdateRankingServiceImpl implements UpdateRankingService { + + private static final String UPDATE_RANKING = "update ranking set rank=rank+1 where rank=?"; + + private final RedisTemplate redisTemplate; + + public UpdateRankingServiceImpl(RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + public void updateRanking(Rank rank) { + redisTemplate.opsForSet().add(UPDATE_RANKING, rank.getUser().getId(), rank.getPenaltyPoints()); + } + +} From 2fc1e70e69f48872a519b341b8b6b50f651200b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Thu, 14 Nov 2024 19:52:10 +0900 Subject: [PATCH 19/31] add :: UpdateRankingService.java --- .../domain/rank/service/UpdateRankingService.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/java/com/example/domaserver/domain/rank/service/UpdateRankingService.java diff --git a/src/main/java/com/example/domaserver/domain/rank/service/UpdateRankingService.java b/src/main/java/com/example/domaserver/domain/rank/service/UpdateRankingService.java new file mode 100644 index 0000000..0f29221 --- /dev/null +++ b/src/main/java/com/example/domaserver/domain/rank/service/UpdateRankingService.java @@ -0,0 +1,7 @@ +package com.example.domaserver.domain.rank.service; + +import com.example.domaserver.domain.rank.entity.Rank; + +public interface UpdateRankingService { + void updateRanking(Rank rank); +} From 6e642a9d6c7e111f16dbe40cbbf552a40468c602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Thu, 14 Nov 2024 19:52:43 +0900 Subject: [PATCH 20/31] modify :: RankController.java --- .../rank/presentation/RankController.java | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/example/domaserver/domain/rank/presentation/RankController.java b/src/main/java/com/example/domaserver/domain/rank/presentation/RankController.java index 8d858a9..3e3fb98 100644 --- a/src/main/java/com/example/domaserver/domain/rank/presentation/RankController.java +++ b/src/main/java/com/example/domaserver/domain/rank/presentation/RankController.java @@ -2,11 +2,13 @@ import com.example.domaserver.domain.rank.entity.Rank; -import com.example.domaserver.domain.rank.service.RankService; +import com.example.domaserver.domain.rank.service.GetRankingService; +import com.example.domaserver.domain.rank.service.GetTopRankingService; +import com.example.domaserver.domain.rank.service.UpdateRankingService; import com.example.domaserver.domain.user.entity.User; import com.example.domaserver.domain.user.service.UserService; import com.example.domaserver.global.security.jwt.JwtService; -import org.springframework.beans.factory.annotation.Autowired; +import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestHeader; @@ -15,32 +17,28 @@ import java.util.List; +@RequiredArgsConstructor @RestController @RequestMapping("/home") public class RankController { - private final RankService rankService; + + private final GetRankingService getRankingService; + private final GetTopRankingService getTopRankingService; + private final UpdateRankingService updateRankingService; private final UserService userService; private final JwtService jwtService; - @Autowired - public RankController(RankService rankService, UserService userService, JwtService jwtService) { - this.rankService = rankService; - this.userService = userService; - this.jwtService = jwtService; - } - @GetMapping("/rank") public ResponseEntity> getRanks() { - List topRanks = rankService.getTopRanking(25); + List topRanks = getTopRankingService.getTopRanking(25); return ResponseEntity.ok(topRanks); } @GetMapping("/my-rank") public ResponseEntity getMyRank(@RequestHeader("Authorization") String token) { User user = jwtService.getUserFromToken(token.substring(7)); - Long rank = rankService.getRanking(user); + Long rank = getRankingService.getRanking(user); return ResponseEntity.ok(rank); } - } From 58546449e1e7e0f1151a02bc8ff0831ba439238f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Thu, 14 Nov 2024 19:54:09 +0900 Subject: [PATCH 21/31] modify :: UserServiceImpl.java --- .../domain/user/service/impl/UserServiceImpl.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/example/domaserver/domain/user/service/impl/UserServiceImpl.java b/src/main/java/com/example/domaserver/domain/user/service/impl/UserServiceImpl.java index dd76b5e..14505df 100644 --- a/src/main/java/com/example/domaserver/domain/user/service/impl/UserServiceImpl.java +++ b/src/main/java/com/example/domaserver/domain/user/service/impl/UserServiceImpl.java @@ -4,21 +4,17 @@ import com.example.domaserver.domain.user.repository.UserRepository; import com.example.domaserver.domain.user.service.UserService; import com.example.domaserver.global.annotation.ServiceWithTransaction; -import org.springframework.beans.factory.annotation.Autowired; +import lombok.RequiredArgsConstructor; import org.springframework.security.core.userdetails.UsernameNotFoundException; import java.util.UUID; +@RequiredArgsConstructor @ServiceWithTransaction public class UserServiceImpl implements UserService { private final UserRepository userRepository; - @Autowired - public UserServiceImpl(UserRepository userRepository) { - this.userRepository = userRepository; - } - @Override public User findByUsername(String username) { return userRepository.findByUsername(username) From 08478b207f08caf5b2c206e5af3a58e1874674e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Thu, 14 Nov 2024 19:54:42 +0900 Subject: [PATCH 22/31] delete :: RankServiceImpl.java --- .../rank/service/impl/RankServiceImpl.java | 52 ------------------- 1 file changed, 52 deletions(-) delete mode 100644 src/main/java/com/example/domaserver/domain/rank/service/impl/RankServiceImpl.java diff --git a/src/main/java/com/example/domaserver/domain/rank/service/impl/RankServiceImpl.java b/src/main/java/com/example/domaserver/domain/rank/service/impl/RankServiceImpl.java deleted file mode 100644 index d0e38f1..0000000 --- a/src/main/java/com/example/domaserver/domain/rank/service/impl/RankServiceImpl.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.example.domaserver.domain.rank.service.impl; - -import com.example.domaserver.domain.rank.entity.Rank; -import com.example.domaserver.domain.rank.service.RankService; -import com.example.domaserver.domain.user.entity.User; -import com.example.domaserver.global.annotation.ServiceWithTransaction; -import jakarta.transaction.Transactional; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.core.ZSetOperations; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - - -@ServiceWithTransaction -public class RankServiceImpl implements RankService { - - private static final String RANK_KEY = "USER_RANKING"; - - private final RedisTemplate redisTemplate; - - public RankServiceImpl(@Qualifier("redisTemplate") RedisTemplate redisTemplate) { - this.redisTemplate = redisTemplate; - } - - @Transactional - public Long getRanking(User user) { - Long rank = redisTemplate.opsForZSet().rank(RANK_KEY, user.getId()); - return (rank != null) ? rank + 1 : null; - } - - @Transactional - public void updateRank(Rank rank) { - redisTemplate.opsForZSet().add(RANK_KEY, rank.getUser().getId(), rank.getPenaltyPoints()); - } - - @Transactional - public List getTopRanking(int topN) { - Set> rankedUsers = - redisTemplate.opsForZSet().reverseRangeWithScores(RANK_KEY, 0, topN - 1); - - List userRanks = new ArrayList<>(); - for (ZSetOperations.TypedTuple entry : rankedUsers) { - Long RankId = entry.getValue(); - Double RankScore = entry.getScore(); - userRanks.add(new Rank(RankId, RankScore)); - } - return userRanks; - } - -} From 6481c65a890cf1366c439e9c1c7a473cca5548cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Thu, 14 Nov 2024 19:54:56 +0900 Subject: [PATCH 23/31] delete :: RankService.java --- .../domaserver/domain/rank/service/RankService.java | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 src/main/java/com/example/domaserver/domain/rank/service/RankService.java diff --git a/src/main/java/com/example/domaserver/domain/rank/service/RankService.java b/src/main/java/com/example/domaserver/domain/rank/service/RankService.java deleted file mode 100644 index 6e3f501..0000000 --- a/src/main/java/com/example/domaserver/domain/rank/service/RankService.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.example.domaserver.domain.rank.service; - -import com.example.domaserver.domain.rank.entity.Rank; -import com.example.domaserver.domain.user.entity.User; -import java.util.List; - -public interface RankService { - Long getRanking(User user); - void updateRank(Rank rank); - List getTopRanking(int topN); -} From 35e0844fb34906f0c752c9b230e1f11fec789458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Thu, 14 Nov 2024 19:55:14 +0900 Subject: [PATCH 24/31] delete :: RedisConfig.java --- .../domain/rank/config/RedisConfig.java | 23 ------------------- 1 file changed, 23 deletions(-) delete mode 100644 src/main/java/com/example/domaserver/domain/rank/config/RedisConfig.java diff --git a/src/main/java/com/example/domaserver/domain/rank/config/RedisConfig.java b/src/main/java/com/example/domaserver/domain/rank/config/RedisConfig.java deleted file mode 100644 index 7dd6e41..0000000 --- a/src/main/java/com/example/domaserver/domain/rank/config/RedisConfig.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.example.domaserver.domain.rank.config; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.redis.connection.RedisConnectionFactory; -import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; -import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; - -@EnableRedisRepositories -@Configuration -public class RedisConfig { - @Value("${spring.redis.host}") - private String redisHost; - - @Value("${spring.redis.port}") - private int redisPort; - - @Bean - public RedisConnectionFactory redisConnectionFactory() { - return new LettuceConnectionFactory(redisHost, redisPort); - } -} From 7e202beaf5680fe04e2fe8a7909d3253a4144cd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Tue, 19 Nov 2024 20:00:53 +0900 Subject: [PATCH 25/31] modify :: GetRankingServiceImpl.java --- .../domain/rank/service/impl/GetRankingServiceImpl.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/example/domaserver/domain/rank/service/impl/GetRankingServiceImpl.java b/src/main/java/com/example/domaserver/domain/rank/service/impl/GetRankingServiceImpl.java index dd68b09..97d9724 100644 --- a/src/main/java/com/example/domaserver/domain/rank/service/impl/GetRankingServiceImpl.java +++ b/src/main/java/com/example/domaserver/domain/rank/service/impl/GetRankingServiceImpl.java @@ -3,22 +3,17 @@ import com.example.domaserver.domain.rank.service.GetRankingService; import com.example.domaserver.domain.user.entity.User; import com.example.domaserver.global.annotation.ServiceWithReadOnlyTransactional; -import com.example.domaserver.global.annotation.ServiceWithTransaction; +import lombok.RequiredArgsConstructor; import org.springframework.data.redis.core.RedisTemplate; - -@ServiceWithTransaction @ServiceWithReadOnlyTransactional +@RequiredArgsConstructor public class GetRankingServiceImpl implements GetRankingService { private static final String GET_RANKING = "getRank"; private final RedisTemplate redisTemplate; - public GetRankingServiceImpl(RedisTemplate redisTemplate) { - this.redisTemplate = redisTemplate; - } - public Long getRanking(User user) { Long rank = redisTemplate.opsForZSet().rank(GET_RANKING, user.getId()); return (rank != null) ? rank + 1 : null; From 593a11ef8acaf4ee8fe333e7cf3abf9ecd9cce8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Tue, 19 Nov 2024 20:01:12 +0900 Subject: [PATCH 26/31] modify :: GetTopRankingServiceImpl.java --- .../rank/service/impl/GetTopRankingServiceImpl.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/example/domaserver/domain/rank/service/impl/GetTopRankingServiceImpl.java b/src/main/java/com/example/domaserver/domain/rank/service/impl/GetTopRankingServiceImpl.java index 3aaebe8..e8ecf9a 100644 --- a/src/main/java/com/example/domaserver/domain/rank/service/impl/GetTopRankingServiceImpl.java +++ b/src/main/java/com/example/domaserver/domain/rank/service/impl/GetTopRankingServiceImpl.java @@ -3,7 +3,7 @@ import com.example.domaserver.domain.rank.entity.Rank; import com.example.domaserver.domain.rank.service.GetTopRankingService; import com.example.domaserver.global.annotation.ServiceWithReadOnlyTransactional; -import com.example.domaserver.global.annotation.ServiceWithTransaction; +import lombok.RequiredArgsConstructor; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ZSetOperations; @@ -11,19 +11,14 @@ import java.util.List; import java.util.Set; - -@ServiceWithTransaction @ServiceWithReadOnlyTransactional +@RequiredArgsConstructor public class GetTopRankingServiceImpl implements GetTopRankingService { private static final String GET_TOP_RANKING = "getTopRanking"; private final RedisTemplate redisTemplate; - public GetTopRankingServiceImpl(RedisTemplate redisTemplate) { - this.redisTemplate = redisTemplate; - } - public List getTopRanking(int topN) { Set> rankedUsers = redisTemplate.opsForZSet().reverseRange(GET_TOP_RANKING, 0, topN - 1); From 75819788f58be02726a594146473e6899646dec0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Tue, 19 Nov 2024 20:01:31 +0900 Subject: [PATCH 27/31] modify :: UpdateRankingServiceImpl.java --- .../domain/rank/service/impl/UpdateRankingServiceImpl.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/example/domaserver/domain/rank/service/impl/UpdateRankingServiceImpl.java b/src/main/java/com/example/domaserver/domain/rank/service/impl/UpdateRankingServiceImpl.java index 4f468d4..25aee58 100644 --- a/src/main/java/com/example/domaserver/domain/rank/service/impl/UpdateRankingServiceImpl.java +++ b/src/main/java/com/example/domaserver/domain/rank/service/impl/UpdateRankingServiceImpl.java @@ -2,22 +2,19 @@ import com.example.domaserver.domain.rank.entity.Rank; import com.example.domaserver.domain.rank.service.UpdateRankingService; -import com.example.domaserver.global.annotation.ServiceWithReadOnlyTransactional; import com.example.domaserver.global.annotation.ServiceWithTransaction; +import lombok.RequiredArgsConstructor; import org.springframework.data.redis.core.RedisTemplate; @ServiceWithTransaction +@RequiredArgsConstructor public class UpdateRankingServiceImpl implements UpdateRankingService { private static final String UPDATE_RANKING = "update ranking set rank=rank+1 where rank=?"; private final RedisTemplate redisTemplate; - public UpdateRankingServiceImpl(RedisTemplate redisTemplate) { - this.redisTemplate = redisTemplate; - } - public void updateRanking(Rank rank) { redisTemplate.opsForSet().add(UPDATE_RANKING, rank.getUser().getId(), rank.getPenaltyPoints()); } From ea0e18e32113c72aa4392c51406019afe6a5048b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Thu, 21 Nov 2024 20:34:27 +0900 Subject: [PATCH 28/31] modify :: User.java --- .../java/com/example/domaserver/domain/user/entity/User.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/example/domaserver/domain/user/entity/User.java b/src/main/java/com/example/domaserver/domain/user/entity/User.java index 127e947..53abf0b 100644 --- a/src/main/java/com/example/domaserver/domain/user/entity/User.java +++ b/src/main/java/com/example/domaserver/domain/user/entity/User.java @@ -16,6 +16,7 @@ public class User { private UUID id; private String name; private String email; + private String profileImageUrl; private int penaltyPoint; @Embedded @@ -23,6 +24,4 @@ public class User { @Enumerated(EnumType.STRING) private Authority authority; - - } From 994ac4611493ec7d5486263f2709553144afd5d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Thu, 21 Nov 2024 20:34:44 +0900 Subject: [PATCH 29/31] modify :: RankResponse.java --- .../domain/rank/presentation/dto/response/RankResponse.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/example/domaserver/domain/rank/presentation/dto/response/RankResponse.java b/src/main/java/com/example/domaserver/domain/rank/presentation/dto/response/RankResponse.java index 6296953..6d9a1b0 100644 --- a/src/main/java/com/example/domaserver/domain/rank/presentation/dto/response/RankResponse.java +++ b/src/main/java/com/example/domaserver/domain/rank/presentation/dto/response/RankResponse.java @@ -10,7 +10,6 @@ public class RankResponse { private UUID Id; private String name; - private String profileImageUrl; private int penaltyPoints; private double RankScore; } From 039cbd7bebbeaf4572986a3c65a2a448cbcfa56f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Thu, 21 Nov 2024 23:10:40 +0900 Subject: [PATCH 30/31] Revert "modify :: User.java" This reverts commit ea0e18e32113c72aa4392c51406019afe6a5048b. --- .../java/com/example/domaserver/domain/user/entity/User.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/domaserver/domain/user/entity/User.java b/src/main/java/com/example/domaserver/domain/user/entity/User.java index 53abf0b..127e947 100644 --- a/src/main/java/com/example/domaserver/domain/user/entity/User.java +++ b/src/main/java/com/example/domaserver/domain/user/entity/User.java @@ -16,7 +16,6 @@ public class User { private UUID id; private String name; private String email; - private String profileImageUrl; private int penaltyPoint; @Embedded @@ -24,4 +23,6 @@ public class User { @Enumerated(EnumType.STRING) private Authority authority; + + } From e8c46cd79a718f91a707fb6ea2d773e2bc3d588d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=95=9C=EC=A3=BC?= Date: Thu, 21 Nov 2024 23:14:15 +0900 Subject: [PATCH 31/31] Revert "modify :: RankResponse.java" This reverts commit 994ac4611493ec7d5486263f2709553144afd5d2. --- .../domain/rank/presentation/dto/response/RankResponse.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/example/domaserver/domain/rank/presentation/dto/response/RankResponse.java b/src/main/java/com/example/domaserver/domain/rank/presentation/dto/response/RankResponse.java index 6d9a1b0..6296953 100644 --- a/src/main/java/com/example/domaserver/domain/rank/presentation/dto/response/RankResponse.java +++ b/src/main/java/com/example/domaserver/domain/rank/presentation/dto/response/RankResponse.java @@ -10,6 +10,7 @@ public class RankResponse { private UUID Id; private String name; + private String profileImageUrl; private int penaltyPoints; private double RankScore; }