Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[Feat]: User Internal Preview 조회 API 구현 #153

Merged
merged 4 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public AuthResponse.Token reissueToken(String accessToken, String refreshToken)

@Transactional
public void signOut(Long userId) {
User user = userQueryHandler.findByIdAndStatus(userId, Status.ACTIVE);
User user = userQueryHandler.findByIdFetch(userId);
userSignOutPolicy.validate(user);
user.withDraw();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public class GroupMemberService {
private final GroupMemberJpaRepository userGroupJpaRepository;

public GroupResponse.GetGroups getExpectedMyGroup(Long userId) {
User findUser = userQueryHandler.findByIdAndStatus(userId, Status.ACTIVE);
User findUser = userQueryHandler.findByIdFetch(userId);
List<Group> expectedMyGroups = userGroupJpaRepository.findExpectedGroupsByUser(findUser);
return expectedMyGroups.stream()
.map(GroupResponse.GetGroup::from)
.collect(Collectors.collectingAndThen(Collectors.toList(), GroupResponse.GetGroups::new));
}

public PageResponse<GroupResponse.GetParticipatedGroup> getParticipatedMyGroup(Long userId, int page, int size) {
User findUser = userQueryHandler.findByIdAndStatus(userId, Status.ACTIVE);
User findUser = userQueryHandler.findByIdFetch(userId);
return PageResponse.from(
userGroupJpaRepository.findParticipatedGroupsByUser(findUser, PageRequest.of(page, size))
.map(GroupResponse.GetParticipatedGroup::from)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.gloddy.server.user.api.intenal;


import com.gloddy.server.core.response.ApiResponse;
import com.gloddy.server.user.application.internal.UserInternalQueryService;
import com.gloddy.server.user.application.internal.dto.UserPreviewResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/internal/payload")
@RequiredArgsConstructor
public class UserInternalQueryController {

private final UserInternalQueryService userInternalQueryService;

@GetMapping("/users/{userId}")
public ResponseEntity<UserPreviewResponse> getUserPreview(@PathVariable("userId") Long userId) {
UserPreviewResponse response = userInternalQueryService.getUserPreview(userId);
return ApiResponse.ok(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class UserUpdateService {

@Transactional
public UserUpdateResponse update(Long userId, Info request) {
User user = userQueryHandler.findByIdAndStatus(userId, Status.ACTIVE);
User user = userQueryHandler.findByIdFetch(userId);
user.updateProfile(
request.getImageUrl(),
request.getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class UserGetFacade {

@Transactional(readOnly = true)
public UserResponse.FacadeGet getUserFacade(Long userId) {
User user = userQueryHandler.findByIdAndStatus(userId, Status.ACTIVE);
User user = userQueryHandler.findByIdFetch(userId);
Long countParticipatedGroup = groupMemberQueryHandler.countParticipatedGroup(userId);
Long reviewCount = mateQueryHandler.countByUserId(userId);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.gloddy.server.user.application.internal;

import com.gloddy.server.user.application.internal.dto.UserPreviewResponse;
import com.gloddy.server.user.domain.handler.UserQueryHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class UserInternalQueryService {

private final UserQueryHandler userQueryHandler;

public UserPreviewResponse getUserPreview(Long userId) {
return userQueryHandler.findUserPreviewById(userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.gloddy.server.user.application.internal.dto;

import com.gloddy.server.user.domain.vo.ReliabilityLevel;
import com.querydsl.core.annotations.QueryProjection;

public record UserPreviewResponse(Long id, Boolean isCertifiedStudent, String profileImage, String nickName,
String countryName, String countryImage, ReliabilityLevel reliabilityLevel) {
@QueryProjection
public UserPreviewResponse {
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/gloddy/server/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.gloddy.server.group.domain.service.GroupFactory;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.Where;

import java.time.LocalDate;
import java.util.List;
Expand All @@ -26,6 +27,7 @@
@EqualsAndHashCode(of = {"id"}, callSuper = false)
@Getter
@Table(name = "users")
@Where(clause = "status = 'ACTIVE'")
public class User extends BaseTimeEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package com.gloddy.server.user.domain.handler;

import com.gloddy.server.user.application.internal.dto.UserPreviewResponse;
import com.gloddy.server.user.domain.User;
import com.gloddy.server.user.domain.vo.Phone;
import com.gloddy.server.user.domain.vo.kind.Status;
import com.gloddy.server.user.domain.dto.PraiseResponse;

import java.util.Optional;

public interface UserQueryHandler {
User findById(Long id);

User findByIdAndStatus(Long id, Status status);
User findByIdFetch(Long id);
Optional<User> findByEmail(String email);

boolean existsByNickname(String nickname);

Optional<User> findByPhone(Phone phone);

PraiseResponse.GetPraiseForUser findPraiseDtoByUserId(Long userId);

UserPreviewResponse findUserPreviewById(Long userId);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.gloddy.server.user.domain.handler.impl;

import com.gloddy.server.user.application.internal.dto.UserPreviewResponse;
import com.gloddy.server.user.domain.User;
import com.gloddy.server.user.domain.vo.Phone;
import com.gloddy.server.user.domain.vo.kind.Status;
import com.gloddy.server.user.domain.dto.PraiseResponse;
import com.gloddy.server.user.domain.handler.UserQueryHandler;
import com.gloddy.server.user.infra.repository.UserJpaRepository;
Expand All @@ -26,8 +26,8 @@ public User findById(Long id) {
}

@Override
public User findByIdAndStatus(Long id, Status status) {
return userJpaRepository.findByIdAndStatusFetch(id, status)
public User findByIdFetch(Long id) {
return userJpaRepository.findByIdFetch(id)
.orElseThrow(() -> new UserBusinessException(ErrorCode.USER_NOT_FOUND));
}

Expand All @@ -50,4 +50,10 @@ public Optional<User> findByPhone(Phone phone) {
public PraiseResponse.GetPraiseForUser findPraiseDtoByUserId(Long userId) {
return userJpaRepository.findPraiseByUserId(userId);
}

@Override
public UserPreviewResponse findUserPreviewById(Long userId) {
return userJpaRepository.findUserPreviewById(userId)
.orElseThrow(() -> new UserBusinessException(ErrorCode.USER_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gloddy.server.user.infra.repository.custom;

import com.gloddy.server.user.application.internal.dto.UserPreviewResponse;
import com.gloddy.server.user.domain.User;
import com.gloddy.server.user.domain.vo.Phone;
import com.gloddy.server.user.domain.vo.kind.Status;
Expand All @@ -13,7 +14,9 @@ public interface UserJpaRepositoryCustom {

Optional<User> findByEmail(String email);

Optional<User> findByIdAndStatusFetch(Long id, Status status);
Optional<User> findByIdFetch(Long id);

PraiseResponse.GetPraiseForUser findPraiseByUserId(Long userId);

Optional<UserPreviewResponse> findUserPreviewById(Long id);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.gloddy.server.user.infra.repository.impl;

import com.gloddy.server.user.application.internal.dto.QUserPreviewResponse;
import com.gloddy.server.user.application.internal.dto.UserPreviewResponse;
import com.gloddy.server.user.domain.User;
import com.gloddy.server.user.domain.vo.Phone;
import com.gloddy.server.user.domain.vo.kind.Status;
import com.gloddy.server.user.domain.dto.PraiseResponse;
import com.gloddy.server.user.domain.dto.QPraiseResponse_GetPraiseForUser;
import com.gloddy.server.user.infra.repository.custom.UserJpaRepositoryCustom;
Expand All @@ -25,21 +26,21 @@ public class UserJpaRepositoryImpl implements UserJpaRepositoryCustom {
@Override
public Optional<User> findByPhone(Phone phone) {
return Optional.ofNullable(query.selectFrom(user)
.where(eqPhone(phone), isActive())
.where(eqPhone(phone))
.fetchOne());
}

@Override
public Optional<User> findByEmail(String email) {
return Optional.ofNullable(query.selectFrom(user)
.where(eqEmail(email), isActive())
.where(eqEmail(email))
.fetchOne());
}

@Override
public Optional<User> findByIdAndStatusFetch(Long id, Status status) {
public Optional<User> findByIdFetch(Long id) {
return Optional.ofNullable(query.selectFrom(user)
.where(eqId(id), eqStatus(status))
.where(eqId(id))
.join(user.praise, praise).fetchJoin()
.join(user.reliability, reliability).fetchJoin()
.fetchOne());
Expand All @@ -59,6 +60,22 @@ public PraiseResponse.GetPraiseForUser findPraiseByUserId(Long userId) {
.fetchOne();
}

@Override
public Optional<UserPreviewResponse> findUserPreviewById(Long id) {
return Optional.ofNullable(query.select(new QUserPreviewResponse(
user.id,
user.school.isCertifiedStudent,
user.profile.imageUrl,
user.profile.nickname,
user.profile.country.name,
user.profile.country.image,
reliability.level
)).from(user)
.innerJoin(user.reliability, reliability)
.where(eqId(id))
.fetchOne());
}

private BooleanExpression eqPhone(Phone phone) {
return user.phone.eq(phone);
}
Expand All @@ -67,14 +84,6 @@ private BooleanExpression eqEmail(String email) {
return user.school.email.eq(email);
}

private BooleanExpression isActive() {
return user.status.eq(Status.ACTIVE);
}

private BooleanExpression eqStatus(Status status) {
return user.status.eq(status);
}

private BooleanExpression eqId(Long id) {
return user.id.eq(id);
}
Expand Down
Loading