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

YEL-193 [feat] 친구 쪽지 전체 조회하기 #421

Merged
merged 11 commits into from
Jan 30, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.yello.server.domain.vote.dto.response.VoteFriendResponse;
import com.yello.server.domain.vote.dto.response.VoteListResponse;
import com.yello.server.domain.vote.dto.response.VoteUnreadCountResponse;
import com.yello.server.domain.vote.entity.VoteType;
import com.yello.server.domain.vote.service.VoteService;
import com.yello.server.global.common.SuccessCode;
import com.yello.server.global.common.annotation.AccessTokenUser;
Expand Down Expand Up @@ -74,7 +75,7 @@ public BaseResponse<VoteDetailResponse> findVote(@PathVariable Long voteId,

@GetMapping("/v2/vote/friend")
public BaseResponse<VoteFriendAndUserResponse> findAllFriendVotesWithType(
@RequestParam("page") Integer page, @RequestParam("type") String type,
@RequestParam("page") Integer page, @RequestParam(value = "type", required = false) String type,
@AccessTokenUser User user) {
val data = voteService.findAllFriendVotesWithType(user.getId(), createPageableLimitTen(page), type);
return BaseResponse.success(READ_VOTE_SUCCESS, data);
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/yello/server/domain/vote/entity/VoteType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.yello.server.domain.vote.entity;

import java.text.MessageFormat;
import java.util.Arrays;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum VoteType {
send("send");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum 이름은 대문자 SEND로 바꿔주세요


private final String intial;

public static VoteType fromCode(String dbData) {
return Arrays.stream(VoteType.values())
.filter(v -> v.getIntial().equals(dbData))
.findAny()
.orElseThrow(() -> new IllegalArgumentException(
MessageFormat.format("존재하지 않는 투표 타입입니다. {0}", dbData)));
}

public String intial() {
return intial;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.yello.server.domain.vote.entity;

import com.yello.server.domain.user.entity.Social;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import lombok.extern.log4j.Log4j2;

@Converter
@Log4j2
public class VoteTypeConverter implements AttributeConverter<VoteType, String> {

@Override
public String convertToDatabaseColumn(VoteType voteType) {
if (voteType == null) {
return null;
}
return voteType.getIntial();
}

@Override
public VoteType convertToEntityAttribute(String dbData) {
if (dbData == null) {
return null;
}
try {
return VoteType.fromCode(dbData);
} catch (IllegalArgumentException exception) {
log.error("failure to convert cause unexpected code" + dbData + exception);
throw exception;
}
}
}
28 changes: 16 additions & 12 deletions src/main/java/com/yello/server/domain/vote/service/VoteService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.yello.server.domain.vote.dto.response.VoteResponse;
import com.yello.server.domain.vote.dto.response.VoteUnreadCountResponse;
import com.yello.server.domain.vote.entity.Vote;
import com.yello.server.domain.vote.entity.VoteType;
import com.yello.server.domain.vote.exception.VoteForbiddenException;
import com.yello.server.domain.vote.exception.VoteNotFoundException;
import com.yello.server.domain.vote.repository.VoteRepository;
Expand All @@ -57,6 +58,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
@Builder
Expand Down Expand Up @@ -121,18 +123,7 @@ public VoteFriendResponse findAllFriendVotes(Long userId, Pageable pageable) {

public VoteFriendAndUserResponse findAllFriendVotesWithType(Long userId, Pageable pageable, String type) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ENUM을 통해 type을 narrowing해주고,
StringUtils.hasText(); 통해 null처리를 해주세요

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String type -> VoteType type으로 바꿔주세요


if(type.equals(USER_VOTE_TYPE)) {
final Long totalCount = voteRepository.countUserSendReceivedByFriends(userId);

List<VoteFriendAndUserVO> list =
voteRepository.findUserSendReceivedByFriends(userId, pageable)
.stream()
.filter(vote -> vote.getNameHint()!=-3)
.map(vote -> VoteFriendAndUserVO.of(vote, vote.getSender().getId().equals(userId)))
.toList();
return VoteFriendAndUserResponse.of(totalCount,list);
}
if(type.equals(ALL_VOTE_TYPE)) {
if(!StringUtils.hasText(type)) {
final Long totalCount = Long.valueOf(voteRepository.countAllReceivedByFriends(userId));
final List<VoteFriendAndUserVO> list = voteRepository.findAllReceivedByFriends(userId, pageable)
.stream()
Expand All @@ -142,6 +133,19 @@ public VoteFriendAndUserResponse findAllFriendVotesWithType(Long userId, Pageabl
return VoteFriendAndUserResponse.of(totalCount, list);
}

switch(VoteType.valueOf(type)) {
case send -> {
final Long totalCount = voteRepository.countUserSendReceivedByFriends(userId);
List<VoteFriendAndUserVO> list =
voteRepository.findUserSendReceivedByFriends(userId, pageable)
.stream()
.filter(vote -> vote.getNameHint()!=-3)
.map(vote -> VoteFriendAndUserVO.of(vote, vote.getSender().getId().equals(userId)))
.toList();
return VoteFriendAndUserResponse.of(totalCount,list);
}
}

throw new VoteForbiddenException(WRONG_VOTE_TYPE_FORBIDDEN);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.yello.server.domain.vote.dto.response.VoteResponse;
import com.yello.server.domain.vote.dto.response.VoteUnreadCountResponse;
import com.yello.server.domain.vote.entity.Vote;
import com.yello.server.domain.vote.entity.VoteType;
import com.yello.server.domain.vote.service.VoteService;
import com.yello.server.global.exception.ControllerExceptionAdvice;
import com.yello.server.infrastructure.firebase.service.NotificationService;
Expand Down Expand Up @@ -201,7 +202,8 @@ void init() {
final VoteFriendAndUserResponse voteFriendResponse =
VoteFriendAndUserResponse.of(1L, Arrays.asList(voteFriendAndUserVO));

given(voteService.findAllFriendVotesWithType(anyLong(), any(Pageable.class), anyString()))
given(voteService.findAllFriendVotesWithType(anyLong(), any(Pageable.class),
anyString()))
.willReturn(voteFriendResponse);

// when
Expand Down