Skip to content

Commit

Permalink
Merge pull request #11 from KNU-HAEDAL/issue/#10
Browse files Browse the repository at this point in the history
Issue/#10
  • Loading branch information
bayy1216 authored May 31, 2024
2 parents 8b0a373 + 84f6c95 commit 948b77f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,7 +15,6 @@

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Tag(name = "user", description = "유저 API")
@RequiredArgsConstructor
Expand All @@ -27,11 +27,8 @@ public class UserController {
public ApiResponse<UserRes.UserInfoDto> 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 = "내 정보를 수정한다.")
Expand Down Expand Up @@ -61,13 +58,13 @@ public ApiResponse<List<UserRes.StrickDto>> getStrick(

@Operation(summary = "유저 랭킹 페이징", description = "전체 유저 랭킹을 조회 페이징")
@GetMapping("/api/users/ranking")
public ApiResponse<PagingResponse<Void>> getUsersRanking(
@Valid @RequestParam Long page
public ApiResponse<PagingResponse<UserRes.UserInfoDto>> 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);
}



}
Original file line number Diff line number Diff line change
@@ -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<User> findByAuthToken(String authToken);

Page<User> getUserPagingByRanking(Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}

/**
* 수정해야할 정보를 받고 해당 값으로 모두 업데이트
*/
Expand All @@ -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<UserModel> getUserPagingByRanking(Pageable pageable) {
Page<User> users = userReader.getUserPagingByRanking(pageable);
return users.map(UserModel::from);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
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;

@Component
@RequiredArgsConstructor
public class UserReaderImpl implements UserReader {
private final UserRepository userRepository;
private final JPAQueryFactory queryFactory;

@Override
public User getById(Long id) {
Expand All @@ -23,4 +30,18 @@ public User getById(Long id) {
public Optional<User> findByAuthToken(String authToken) {
return userRepository.findByAuthToken(authToken);
}

@Override
public Page<User> getUserPagingByRanking(Pageable pageable) {
Long totalCount = queryFactory
.select(QUser.user.count())
.from(QUser.user)
.fetchOne();
List<User> 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);
}
}

0 comments on commit 948b77f

Please sign in to comment.