Skip to content

Commit

Permalink
Merge pull request #634 from woowacourse-teams/feat/#629
Browse files Browse the repository at this point in the history
Bet 예외 추가 및 예외 처리 통일
  • Loading branch information
ay-eonii authored Oct 16, 2024
2 parents 6576d6e + a2a607f commit 72f43c0
Show file tree
Hide file tree
Showing 19 changed files with 230 additions and 205 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mouda.backend.bet.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum BetErrorMessage {

BET_DARAKBANG_MEMBER_NOT_FOUND("참여하지 않은 안내면진다입니다."),
BET_NOT_FOUND("안내면진다가 존재하지 않습니다."),
LOSER_NOT_FOUND("당첨자가 존재하지 않습니다.");

private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mouda.backend.bet.exception;

import org.springframework.http.HttpStatus;

import mouda.backend.common.exception.MoudaException;

public class BetException extends MoudaException {

public BetException(HttpStatus httpStatus, BetErrorMessage betErrorMessage) {
super(httpStatus, betErrorMessage.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import lombok.RequiredArgsConstructor;
import mouda.backend.bet.entity.BetDarakbangMemberEntity;
import mouda.backend.bet.exception.BetErrorMessage;
import mouda.backend.bet.exception.BetException;
import mouda.backend.bet.infrastructure.BetDarakbangMemberRepository;
import mouda.backend.chat.exception.ChatErrorMessage;
import mouda.backend.chat.exception.ChatException;
import mouda.backend.darakbangmember.domain.DarakbangMember;

@Component
Expand All @@ -20,7 +20,7 @@ public void updateLastReadChat(long betId, DarakbangMember darakbangMember, long
BetDarakbangMemberEntity betDarakbangMemberEntity = betDarakbangMemberRepository
.findByBetIdAndDarakbangMemberId(betId, darakbangMember.getId())
.orElseThrow(
() -> new ChatException(HttpStatus.NOT_FOUND, ChatErrorMessage.BET_DARAKBANG_MEMBER_NOT_FOUND));
() -> new BetException(HttpStatus.NOT_FOUND, BetErrorMessage.BET_DARAKBANG_MEMBER_NOT_FOUND));
betDarakbangMemberEntity.updateLastChat(lastReadChatId);
}
}
20 changes: 12 additions & 8 deletions backend/src/main/java/mouda/backend/bet/implement/BetFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -13,6 +14,8 @@
import mouda.backend.bet.domain.Participant;
import mouda.backend.bet.entity.BetDarakbangMemberEntity;
import mouda.backend.bet.entity.BetEntity;
import mouda.backend.bet.exception.BetErrorMessage;
import mouda.backend.bet.exception.BetException;
import mouda.backend.bet.infrastructure.BetDarakbangMemberRepository;
import mouda.backend.bet.infrastructure.BetRepository;
import mouda.backend.darakbangmember.domain.DarakbangMember;
Expand All @@ -28,7 +31,7 @@ public class BetFinder {

public Bet find(long darakbangId, long betEntityId) {
BetEntity betEntity = betRepository.findByIdAndDarakbangId(betEntityId, darakbangId)
.orElseThrow(IllegalArgumentException::new);
.orElseThrow(() -> new BetException(HttpStatus.NOT_FOUND, BetErrorMessage.BET_NOT_FOUND));
List<Participant> participants = participantFinder.findAllByBetEntity(betEntity);

return Bet.builder()
Expand All @@ -54,7 +57,8 @@ public List<Bet> findAllDrawableBet() {

private List<Bet> createBets(List<BetEntity> betEntities) {
return betEntities.stream()
.map(this::createBet).toList();
.map(this::createBet)
.toList();
}

private Bet createBet(BetEntity betEntity) {
Expand All @@ -70,18 +74,18 @@ private Bet createBet(BetEntity betEntity) {

@Transactional(readOnly = true)
public Loser findResult(long darakbangId, long betId) {
// TODO : 예외처리 공통화
BetEntity betEntity = betRepository.findByIdAndDarakbangId(betId, darakbangId)
.orElseThrow(IllegalArgumentException::new);
.orElseThrow(() -> new BetException(HttpStatus.NOT_FOUND, BetErrorMessage.BET_NOT_FOUND));

// TODO : 리팩토링
Long loserDarakbangMemberId = betEntity.getLoserDarakbangMemberId();
if (loserDarakbangMemberId == null) {
throw new IllegalArgumentException("아직 추첨이 진행되지 않았습니다.");
throw new BetException(HttpStatus.NOT_FOUND, BetErrorMessage.LOSER_NOT_FOUND);
}

BetDarakbangMemberEntity betDarakbangMemberEntity = betDarakbangMemberRepository.findByBetIdAndDarakbangMemberId(
betId, loserDarakbangMemberId).orElseThrow(IllegalArgumentException::new);
BetDarakbangMemberEntity betDarakbangMemberEntity = betDarakbangMemberRepository
.findByBetIdAndDarakbangMemberId(betId, loserDarakbangMemberId)
.orElseThrow(() -> new BetException(HttpStatus.NOT_FOUND, BetErrorMessage.BET_DARAKBANG_MEMBER_NOT_FOUND));

return new Loser(betDarakbangMemberEntity.getDarakbangMember().getId(),
betDarakbangMemberEntity.getDarakbangMember().getNickname());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
import mouda.backend.bet.domain.Bet;
import mouda.backend.bet.entity.BetDarakbangMemberEntity;
import mouda.backend.bet.entity.BetEntity;
import mouda.backend.bet.exception.BetErrorMessage;
import mouda.backend.bet.exception.BetException;
import mouda.backend.bet.infrastructure.BetDarakbangMemberRepository;
import mouda.backend.bet.infrastructure.BetRepository;
import mouda.backend.darakbangmember.domain.DarakbangMember;
Expand All @@ -34,7 +37,7 @@ public long save(long darakbangId, Bet bet) {

public void participate(long darakbangId, long betId, DarakbangMember darakbangMember) {
BetEntity betEntity = betRepository.findByIdAndDarakbangId(betId, darakbangId)
.orElseThrow(() -> new IllegalArgumentException("no bet"));
.orElseThrow(() -> new BetException(HttpStatus.NOT_FOUND, BetErrorMessage.BET_NOT_FOUND));

BetDarakbangMemberEntity betDarakbangMemberEntity = new BetDarakbangMemberEntity(darakbangMember, betEntity);
betDarakbangMemberRepository.save(betDarakbangMemberEntity);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mouda.backend.chat.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
Expand Down Expand Up @@ -33,6 +35,7 @@ public class ChatRoomEntity {

private long darakbangId;

@Enumerated(EnumType.STRING)
private ChatRoomType type;

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@
public enum ChatErrorMessage {

CHATROOM_NOT_FOUND("존재하지 않는 채팅방입니다."),
MOIM_NOT_FOUND("존재하지 않는 모임입니다."),
BET_NOT_FOUND("존재하지 않는 안내면진다입니다."),
CHAMYO_NOT_FOUND("참여하지 않은 모임입니다."),
BET_DARAKBANG_MEMBER_NOT_FOUND("참여하지 않은 안내면진다입니다."),
INVALID_RECENT_CHAT_ID("잘못된 채팅 아이디입니다."),
INVALID_CHATROOM_TYPE("잘못된 채팅 방 타입입니다."),
UNAUTHORIZED_MOIMER("모이머 권한이 없습니다."),
UNAUTHORIZED("권한이 없습니다.");

private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mouda.backend.chat.implement;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
Expand All @@ -12,36 +13,40 @@
import mouda.backend.chat.domain.ChatRoom;
import mouda.backend.chat.domain.ChatRoomType;
import mouda.backend.chat.domain.Participant;
import mouda.backend.chat.exception.ChatErrorMessage;
import mouda.backend.chat.exception.ChatException;
import mouda.backend.darakbangmember.domain.DarakbangMember;

@Component
@RequiredArgsConstructor
public class BetAttributeManager implements AttributeManager {

private final BetFinder betFinder;
private final BetDarakbangMemberRepository betDarakbangMemberRepository;

@Override
public boolean support(ChatRoomType chatRoomType) {
return chatRoomType == ChatRoomType.BET;
}

@Override
public Attributes create(ChatRoom chatRoom, DarakbangMember darakbangMember) {
Bet bet = betFinder.find(darakbangMember.getDarakbang().getId(), chatRoom.getTargetId());
boolean isLoser = bet.isLoser(darakbangMember.getId());
Participant loser = getLoser(bet, darakbangMember.getId());
return new BetAttributes(bet.getBetDetails().getTitle(), isLoser, bet.getId(), loser);
}

private Participant getLoser(Bet bet, long requestDarakbangMemberId) {
DarakbangMember darakbangMember = betDarakbangMemberRepository.findByBetIdAndDarakbangMemberId(bet.getId(), bet.getLoserId())
.orElseThrow(IllegalArgumentException::new).getDarakbangMember(); // TODO: 예외 처리 리팩토링
BetRole betRole = getBetRole(requestDarakbangMemberId, bet.getMoimerId());
return new Participant(darakbangMember.getNickname(), darakbangMember.getProfile(), betRole.toString());
}

private BetRole getBetRole(long requestDarakbangMemberId, long moimerId) {
return moimerId == requestDarakbangMemberId ? BetRole.MOIMER : BetRole.MOIMEE;
}
private final BetFinder betFinder;
private final BetDarakbangMemberRepository betDarakbangMemberRepository;

@Override
public boolean support(ChatRoomType chatRoomType) {
return chatRoomType == ChatRoomType.BET;
}

@Override
public Attributes create(ChatRoom chatRoom, DarakbangMember darakbangMember) {
Bet bet = betFinder.find(darakbangMember.getDarakbang().getId(), chatRoom.getTargetId());
boolean isLoser = bet.isLoser(darakbangMember.getId());
Participant loser = getLoser(bet, darakbangMember.getId());
return new BetAttributes(bet.getBetDetails().getTitle(), isLoser, bet.getId(), loser);
}

private Participant getLoser(Bet bet, long requestDarakbangMemberId) {
DarakbangMember darakbangMember = betDarakbangMemberRepository
.findByBetIdAndDarakbangMemberId(bet.getId(), bet.getLoserId())
.orElseThrow(() -> new ChatException(HttpStatus.NOT_FOUND, ChatErrorMessage.BET_DARAKBANG_MEMBER_NOT_FOUND))
.getDarakbangMember();
BetRole betRole = getBetRole(requestDarakbangMemberId, bet.getMoimerId());
return new Participant(darakbangMember.getNickname(), darakbangMember.getProfile(), betRole.toString());
}

private BetRole getBetRole(long requestDarakbangMemberId, long moimerId) {
return moimerId == requestDarakbangMemberId ? BetRole.MOIMER : BetRole.MOIMEE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
import mouda.backend.chat.domain.ChatRoomType;
import mouda.backend.chat.exception.ChatErrorMessage;
import mouda.backend.chat.exception.ChatException;

@Component
@RequiredArgsConstructor
Expand All @@ -17,6 +20,6 @@ public ParticipantsResolver getResolver(ChatRoomType chatRoomType) {
return participantsResolvers.stream()
.filter(participantsResolver -> participantsResolver.support(chatRoomType))
.findFirst()
.orElseThrow(IllegalArgumentException::new); // TODO: 예외 처리 리팩토링
.orElseThrow(() -> new ChatException(HttpStatus.BAD_REQUEST, ChatErrorMessage.INVALID_CHATROOM_TYPE));
}
}
6 changes: 3 additions & 3 deletions backend/src/main/java/mouda/backend/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import mouda.backend.moim.exception.MoimErrorMessage;
import mouda.backend.moim.exception.MoimException;
import mouda.backend.member.exception.MemberErrorMessage;
import mouda.backend.member.exception.MemberException;

@Entity
@Getter
Expand All @@ -38,7 +38,7 @@ public Member(String name, LoginDetail loginDetail) {

private void validateName(String name) {
if (name.isBlank()) {
throw new MoimException(HttpStatus.BAD_REQUEST, MoimErrorMessage.MEMBER_NAME_NOT_EXISTS);
throw new MemberException(HttpStatus.BAD_REQUEST, MemberErrorMessage.MEMBER_NAME_NOT_EXISTS);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mouda.backend.member.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum MemberErrorMessage {

MEMBER_NAME_NOT_EXISTS("멤버 이름이 존재하지 않습니다."),
;

private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mouda.backend.member.exception;

import org.springframework.http.HttpStatus;

import mouda.backend.common.exception.MoudaException;

public class MemberException extends MoudaException {

public MemberException(HttpStatus httpStatus, MemberErrorMessage chatErrorMessage) {
super(httpStatus, chatErrorMessage.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
public enum ChamyoErrorMessage {

NOT_FOUND("참여 정보가 존재하지 않습니다."),
MOIM_NOT_FOUND("모임이 존재하지 않습니다."),
ALREADY_PARTICIPATED("이미 참여했어요!"),
MOIM_FULL("모임이 꽉 찼어요!"),
MOIMING_CANCLED("모임이 취소됐어요!"),
MOIMING_COMPLETE("모집이 완료됐어요!"),
MOIM_NOT_JOINED("아직 참여하지 않았어요!"),
CANNOT_CANCEL_CHAMYO("취소할 수 없어요!"),
NOT_MOIMER("모이머가 아닙니다.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,14 @@
public enum MoimErrorMessage {

NOT_FOUND("모임이 존재하지 않습니다."),
MAX_PEOPLE("모임 최대 인원 수를 초과합니다."),
PAST_DATE_TIME("모임 날짜를 현재 시점 이후로 입력해주세요."),
TITLE_NOT_EXIST("모임 제목을 입력해주세요."),
TITLE_TOO_LONG("모임 제목을 조금 더 짧게 입력해주세요."),
DATE_NOT_EXIST("모임 날짜를 입력해주세요."),
TIME_NOT_EXIST("모임 시간을 입력해주세요."),
PLACE_NOT_EXIST("모임 장소를 입력해주세요."),
PLACE_TOO_LONG("모임 장소를 조금 더 짧게 입력해주세요."),
MAX_PEOPLE_IS_POSITIVE("모임 최대 인원은 양수여야 합니다."),
MAX_PEOPLE_TOO_MANY("모임 최대 인원을 조금 더 적게 입력해주세요."),
AUTHOR_NAME_NOT_EXIST("모임 생성자 이름을 입력해주세요."),
AUTHOR_NAME_TOO_LONG("모임 생성자 이름을 조금 더 짧게 입력해주세요."),
DESCRIPTION_TOO_LONG("모임 설명을 조금 더 짧게 입력해주세요."),
MEMBER_NAME_NOT_EXISTS("모임 참여자 이름을 입력해주세요."),
MEMBER_NAME_TOO_LONG("모임 참여자 이름을 조금 더 짧게 입력해주세요."),
NOT_ALLOWED_TO_DELETE("방장만 삭제할 수 있어요."),
NOT_ALLOWED_TO_COMPLETE("방장만 완료할 수 있어요."),
MOIM_CANCELED("이미 취소된 모임이에요."),
ALREADY_COMPLETED("이미 모집 완료된 모임이에요."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
import mouda.backend.chat.exception.ChatErrorMessage;
import mouda.backend.chat.exception.ChatException;
import mouda.backend.darakbangmember.domain.DarakbangMember;
import mouda.backend.moim.domain.Chamyo;
import mouda.backend.moim.domain.Moim;
Expand Down Expand Up @@ -54,7 +52,7 @@ public void delete(Chamyo chamyo) {

public void updateLastReadChat(long targetId, DarakbangMember darakbangMember, long lastReadChatId) {
Chamyo chamyo = chamyoRepository.findByMoimIdAndDarakbangMemberId(targetId, darakbangMember.getId())
.orElseThrow(() -> new ChatException(HttpStatus.NOT_FOUND, ChatErrorMessage.CHAMYO_NOT_FOUND));
.orElseThrow(() -> new ChamyoException(HttpStatus.NOT_FOUND, ChamyoErrorMessage.NOT_FOUND));
chamyo.updateLastChat(lastReadChatId);
}
}
Loading

0 comments on commit 72f43c0

Please sign in to comment.