From 768cbba9a2ab9d307ba74c6094bb2f60d1961a34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=90=ED=99=8D=EC=84=9D?= <78216059+bayy1216@users.noreply.github.com> Date: Fri, 31 May 2024 15:08:56 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[Feat]:=20=EC=9C=A0=EC=A0=80=20=EB=82=B4?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=EC=A1=B0=ED=9A=8C,=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=A7=95=20=EB=8F=84=EB=A9=94=EC=9D=B8=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20(#10)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zzansuni/domain/user/UserReader.java | 5 +++++ .../zzansuni/domain/user/UserService.java | 16 ++++++++++++++ .../infrastructure/user/UserReaderImpl.java | 21 +++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/user/UserReader.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/user/UserReader.java index f2cd935..3d45802 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/user/UserReader.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/user/UserReader.java @@ -1,9 +1,14 @@ package org.haedal.zzansuni.domain.user; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + import java.util.Optional; public interface UserReader { User getById(Long id); Optional findByAuthToken(String authToken); + + Page getUserPagingByRanking(Pageable pageable); } diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/user/UserService.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/user/UserService.java index ec33aad..cdf00b0 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/user/UserService.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/user/UserService.java @@ -1,6 +1,9 @@ package org.haedal.zzansuni.domain.user; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -10,6 +13,12 @@ public class UserService { private final UserReader userReader; private final UserStore userStore; + @Transactional(readOnly = true) + public UserModel getUserModel(Long id) { + User user = userReader.getById(id); + return UserModel.from(user); + } + /** * 수정해야할 정보를 받고 해당 값으로 모두 업데이트 */ @@ -18,4 +27,11 @@ public void updateUser(Long id, UserCommand.Update userUpdate) { User user = userReader.getById(id); user.update(userUpdate); } + + + @Transactional(readOnly = true) + public Page getUserPagingByRanking(Pageable pageable) { + Page users = userReader.getUserPagingByRanking(pageable); + return users.map(UserModel::from); + } } diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/user/UserReaderImpl.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/user/UserReaderImpl.java index d3e7933..830dd45 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/user/UserReaderImpl.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/user/UserReaderImpl.java @@ -1,11 +1,17 @@ package org.haedal.zzansuni.infrastructure.user; +import com.querydsl.jpa.impl.JPAQueryFactory; import jakarta.persistence.EntityNotFoundException; import lombok.RequiredArgsConstructor; +import org.haedal.zzansuni.domain.user.QUser; import org.haedal.zzansuni.domain.user.User; import org.haedal.zzansuni.domain.user.UserReader; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Component; +import java.util.List; import java.util.NoSuchElementException; import java.util.Optional; @@ -13,6 +19,7 @@ @RequiredArgsConstructor public class UserReaderImpl implements UserReader { private final UserRepository userRepository; + private final JPAQueryFactory queryFactory; @Override public User getById(Long id) { @@ -23,4 +30,18 @@ public User getById(Long id) { public Optional findByAuthToken(String authToken) { return userRepository.findByAuthToken(authToken); } + + @Override + public Page getUserPagingByRanking(Pageable pageable) { + Long totalCount = queryFactory + .select(QUser.user.count()) + .from(QUser.user) + .fetchOne(); + List users = queryFactory.selectFrom(QUser.user) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) + .orderBy(QUser.user.exp.desc()) + .fetch(); + return new PageImpl<>(users, pageable, totalCount == null ? 0 : totalCount); + } } From 84f6c95f00ada0aef8df9decf8818c1bdd966d5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=90=ED=99=8D=EC=84=9D?= <78216059+bayy1216@users.noreply.github.com> Date: Fri, 31 May 2024 15:12:30 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[Feat]:=20=EC=9C=A0=EC=A0=80=20=EB=82=B4?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=EC=A1=B0=ED=9A=8C,=EB=9E=AD=ED=82=B9=20?= =?UTF-8?q?=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=20=EC=B6=94=EA=B0=80=20(#1?= =?UTF-8?q?0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/user/UserController.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/user/UserController.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/user/UserController.java index 912245a..4e0557c 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/user/UserController.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/user/UserController.java @@ -5,6 +5,7 @@ import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; +import org.haedal.zzansuni.controller.PagingRequest; import org.haedal.zzansuni.controller.PagingResponse; import org.haedal.zzansuni.core.api.ApiResponse; import org.haedal.zzansuni.domain.user.UserService; @@ -14,7 +15,6 @@ import java.time.LocalDate; import java.util.List; -import java.util.Map; @Tag(name = "user", description = "유저 API") @RequiredArgsConstructor @@ -27,11 +27,8 @@ public class UserController { public ApiResponse getUserInfo( @AuthenticationPrincipal JwtUser jwtUser ) { - return ApiResponse.success( - new UserRes.UserInfoDto(1L, "nickname", "https://picsum.photos/200/300", "email", - new UserRes.TierInfoDto("tier", 100, 50) - ) - ); + var userModel = userService.getUserModel(jwtUser.getId()); + return ApiResponse.success(UserRes.UserInfoDto.from(userModel)); } @Operation(summary = "내 정보 수정", description = "내 정보를 수정한다.") @@ -61,13 +58,13 @@ public ApiResponse> getStrick( @Operation(summary = "유저 랭킹 페이징", description = "전체 유저 랭킹을 조회 페이징") @GetMapping("/api/users/ranking") - public ApiResponse> getUsersRanking( - @Valid @RequestParam Long page + public ApiResponse> getUsersRanking( + @Valid PagingRequest request ) { - // TODO - return ApiResponse.success(null); + var userModelPage = userService.getUserPagingByRanking(request.toPageable()); + var response = PagingResponse.from(userModelPage, UserRes.UserInfoDto::from); + return ApiResponse.success(response); } - }