Skip to content

Commit

Permalink
#13 feat: club, memberclub, member에서의 예외처리
Browse files Browse the repository at this point in the history
  • Loading branch information
wjdwlghks committed Apr 18, 2024
1 parent ade1bc7 commit 0e5ec17
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.project.capstone.club.domain;

import com.project.capstone.book.domain.Book;
import com.project.capstone.club.controller.dto.ClubCreateRequest;
import com.project.capstone.common.domain.MemberClub;
import com.project.capstone.memberclub.domain.MemberClub;
import com.project.capstone.content.domain.Content;
import com.project.capstone.member.domain.Member;
import com.project.capstone.post.domain.Post;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.project.capstone.club.exception;

import com.project.capstone.common.exception.BaseException;
import com.project.capstone.common.exception.ExceptionType;

public class ClubException extends BaseException {
public ClubException(ExceptionType exceptionType) {
super(exceptionType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.project.capstone.club.exception;

import com.project.capstone.common.exception.ExceptionType;

import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;


import static org.springframework.http.HttpStatus.*;

@AllArgsConstructor
public enum ClubExceptionType implements ExceptionType {

CLUB_NOT_FOUND(NOT_FOUND, 101, "해당 모임을 찾을 수 없습니다."),
EXIT_WITHOUT_DELEGATION(BAD_REQUEST, 102, "모임장은 위임 후 모임을 나갈 수 있습니다."),
UNAUTHORIZED_ACTION(UNAUTHORIZED, 103, "모임장만 할 수 있는 기능입니다.")
;

private final HttpStatus status;
private final int exceptionCode;
private final String message;
@Override
public HttpStatus httpStatus() {
return status;
}

@Override
public int exceptionCode() {
return exceptionCode;
}

@Override
public String message() {
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import com.project.capstone.club.controller.dto.ClubResponse;
import com.project.capstone.club.domain.Club;
import com.project.capstone.club.domain.ClubRepository;
import com.project.capstone.common.domain.MemberClub;
import com.project.capstone.common.domain.MemberClubRepository;
import com.project.capstone.club.exception.ClubException;
import com.project.capstone.memberclub.domain.MemberClub;
import com.project.capstone.memberclub.domain.MemberClubRepository;
import com.project.capstone.member.domain.Member;
import com.project.capstone.member.domain.MemberRepository;
import com.project.capstone.member.exception.MemberException;
import com.project.capstone.memberclub.exception.MemberClubException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -18,6 +21,10 @@
import java.util.List;
import java.util.UUID;

import static com.project.capstone.club.exception.ClubExceptionType.*;
import static com.project.capstone.member.exception.MemberExceptionType.*;
import static com.project.capstone.memberclub.exception.MemberClubExceptionType.*;

@Service
@Slf4j
@RequiredArgsConstructor
Expand Down Expand Up @@ -60,60 +67,58 @@ public void createClub(ClubCreateRequest request, String memberId) {

public void join(String memberId, Long clubId) {
Member member = memberRepository.findMemberById(UUID.fromString(memberId)).orElseThrow(
() -> new RuntimeException("존재하지 않는 멤버입니다.")
() -> new MemberException(MEMBER_NOT_FOUND)
);
Club club = clubRepository.findClubById(clubId).orElseThrow(
() -> new RuntimeException("존재하지 않는 모임입니다.")
() -> new ClubException(CLUB_NOT_FOUND)
);
if (memberClubRepository.findMemberClubByMember_IdAndClub_Id(UUID.fromString(memberId), clubId).isPresent()) {
throw new RuntimeException("이미 가입된 모임입니다.");
throw new MemberClubException(ALREADY_JOIN);
}
memberClubRepository.save(new MemberClub(null, member, club));
}

@Transactional
public void out(String userId, Long clubId) {
Club club = clubRepository.findClubById(clubId).orElseThrow(
() -> new RuntimeException("존재하지 않는 모임입니다.")
() -> new ClubException(CLUB_NOT_FOUND)
);
if (club.getManagerId().toString().equals(userId)) {
throw new RuntimeException("모임장은 모임을 나갈 수 없습니다. 모임장을 위임해야합니다.");
throw new ClubException(EXIT_WITHOUT_DELEGATION);
}
memberClubRepository.deleteMemberClubByClub_IdAndMember_Id(clubId, UUID.fromString(userId));
}

@Transactional
public void delegateManager(PrincipalDetails details, UUID memberId, Long clubId) {
Club club = clubRepository.findClubById(clubId).orElseThrow(
() -> new RuntimeException("존재하지 않는 모임입니다.")
);
if (!club.getManagerId().toString().equals(details.getUserId())) {
throw new RuntimeException("해당 클럽의 모임장이 아님");
}
if (memberClubRepository.findMemberClubByMember_IdAndClub_Id(memberId, clubId).isEmpty()) {
throw new RuntimeException("위임하려는 멤버가 모임 구성원이 아닙니다.");
}
checkIsManagerAndTargetIsClubMember(details, memberId, clubId);
clubRepository.updateManager(memberId);
}

@Transactional
public void expelMember(PrincipalDetails details, UUID memberId, Long clubId) {
Club club = clubRepository.findClubById(clubId).orElseThrow(
() -> new RuntimeException("존재하지 않는 모임입니다.")
);
checkIsManagerAndTargetIsClubMember(details, memberId, clubId);
memberClubRepository.deleteMemberClubByClub_IdAndMember_Id(clubId, memberId);
}

private void checkIsManagerAndTargetIsClubMember(PrincipalDetails details, UUID memberId, Long clubId) {
Club club = findClubById(clubId);
if (!club.getManagerId().toString().equals(details.getUserId())) {
throw new RuntimeException("해당 클럽의 모임장이 아님");
throw new ClubException(UNAUTHORIZED_ACTION);
}
if (memberClubRepository.findMemberClubByMember_IdAndClub_Id(memberId, clubId).isEmpty()) {
throw new RuntimeException("추방하려는 멤버가 모임 구성원이 아닙니다.");
throw new MemberClubException(MEMBERCLUB_NOT_FOUND);
}
memberClubRepository.deleteMemberClubByClub_IdAndMember_Id(clubId, memberId);
}

public ClubResponse getClub(Long clubId) {
Club club = clubRepository.findClubById(clubId).orElseThrow(
() -> new RuntimeException("해당 모임이 존재하지 않습니다.")
private Club findClubById(Long clubId) {
return clubRepository.findClubById(clubId).orElseThrow(
() -> new ClubException(CLUB_NOT_FOUND)
);
}

public ClubResponse getClub(Long clubId) {
Club club = findClubById(clubId);
return new ClubResponse(club);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.project.capstone.common.exception;

import lombok.Getter;

@Getter
public class BaseException extends RuntimeException {
private final ExceptionType exceptionType;

public BaseException(ExceptionType exceptionType) {
super(exceptionType.message());
this.exceptionType = exceptionType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.project.capstone.common.exception;

public record ExceptionResponse (
int exceptionCode,
String message
) {

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.project.capstone.common.exception;

import org.springframework.http.HttpStatus;

public interface ExceptionType {
HttpStatus httpStatus();
int exceptionCode();
String message();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.project.capstone.exception;

import com.project.capstone.common.exception.BaseException;
import com.project.capstone.common.exception.ExceptionResponse;
import com.project.capstone.common.exception.ExceptionType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler
public ResponseEntity<ExceptionResponse> handleBaseException(BaseException e) {
ExceptionType type = e.getExceptionType();
return ResponseEntity.status(type.httpStatus()).body(new ExceptionResponse(type.exceptionCode(), type.message()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.project.capstone.auth.controller.dto.SignupRequest;
import com.project.capstone.comment.domain.Comment;
import com.project.capstone.common.domain.MemberClub;
import com.project.capstone.memberclub.domain.MemberClub;
import com.project.capstone.content.domain.Content;
import com.project.capstone.post.domain.Post;
import jakarta.persistence.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.project.capstone.member.exception;

import com.project.capstone.common.exception.BaseException;
import com.project.capstone.common.exception.ExceptionType;

public class MemberException extends BaseException {
public MemberException(ExceptionType exceptionType) {
super(exceptionType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.project.capstone.member.exception;

import com.project.capstone.common.exception.ExceptionType;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;

import static org.springframework.http.HttpStatus.*;


@AllArgsConstructor
public enum MemberExceptionType implements ExceptionType {
MEMBER_NOT_FOUND(NOT_FOUND, 201, "해당 멤버를 찾을 수 없습니다.")
;

private final HttpStatus status;
private final int exceptionCode;
private final String message;

@Override
public HttpStatus httpStatus() {
return status;
}

@Override
public int exceptionCode() {
return exceptionCode;
}

@Override
public String message() {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.project.capstone.common.domain;
package com.project.capstone.memberclub.domain;

import com.project.capstone.club.domain.Club;
import com.project.capstone.member.domain.Member;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.project.capstone.common.domain;
package com.project.capstone.memberclub.domain;

import org.springframework.data.jpa.repository.JpaRepository;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.project.capstone.memberclub.exception;

import com.project.capstone.common.exception.BaseException;
import com.project.capstone.common.exception.ExceptionType;

public class MemberClubException extends BaseException {
public MemberClubException(ExceptionType exceptionType) {
super(exceptionType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.project.capstone.memberclub.exception;

import com.project.capstone.common.exception.ExceptionType;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;

import static org.springframework.http.HttpStatus.*;


@AllArgsConstructor
public enum MemberClubExceptionType implements ExceptionType {
ALREADY_JOIN(BAD_REQUEST, 301, "이미 가입한 모임입니다."),
MEMBERCLUB_NOT_FOUND(NOT_FOUND, 302, "위임 또는 추방하려는 멤버가 모임 구성원이 아닙니다.");
;

private final HttpStatus status;
private final int exceptionCode;
private final String message;

@Override
public HttpStatus httpStatus() {
return status;
}

@Override
public int exceptionCode() {
return exceptionCode;
}

@Override
public String message() {
return message;
}
}

0 comments on commit 0e5ec17

Please sign in to comment.