Skip to content

Commit

Permalink
feat: 멤버 추방하기 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
oosedus committed Jul 30, 2024
1 parent 566e408 commit 872dfea
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public ResponseEntity closeClub(@AuthenticationPrincipal UserPrinciple userPrinc
return ResponseEntity.ok(Map.of("messege","모임 마감 성공"));
}

@DeleteMapping("/api/clubs/{clubId}/members/{memberId}")
public ResponseEntity deleteMember(@AuthenticationPrincipal UserPrinciple userPrinciple, @PathVariable Long clubId, @PathVariable Long memberId) {
myClubService.deleteClubMember(userPrinciple.getEmail(), clubId, memberId);
return ResponseEntity.ok(Map.of("messege","멤버 추방 성공"));
}
}
4 changes: 3 additions & 1 deletion src/main/java/likelion/MZConnent/domain/manner/Manner.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Getter
@Setter
@NoArgsConstructor
public class Manner {
@Id
Expand All @@ -26,7 +28,7 @@ public class Manner {
private Member member;

@ManyToOne
@JoinColumn(name = "clubMemberId", nullable = false)
@JoinColumn(name = "clubMemberId", nullable = true)
private ClubMember clubMember;

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface MannerRepository extends JpaRepository<Manner, Long> {
long countByMemberAndClubMember(Member member, ClubMember clubMember);
List<Manner> findByMember(Member member);
List<Manner> findByClubMember(ClubMember targetClubMember);
}
32 changes: 32 additions & 0 deletions src/main/java/likelion/MZConnent/service/club/MyClubService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package likelion.MZConnent.service.club;

import jakarta.transaction.Transactional;
import likelion.MZConnent.domain.club.Club;
import likelion.MZConnent.domain.club.ClubMember;
import likelion.MZConnent.domain.club.ClubRole;
import likelion.MZConnent.domain.manner.Manner;
import likelion.MZConnent.domain.member.Member;
import likelion.MZConnent.dto.club.SelfIntroductionDto;
import likelion.MZConnent.dto.club.response.MyClubDetailResponse;
import likelion.MZConnent.dto.club.response.MyClubSimpleResponse;
import likelion.MZConnent.repository.club.ClubMemberRepository;
import likelion.MZConnent.repository.club.ClubRepository;
import likelion.MZConnent.repository.manner.MannerRepository;
import likelion.MZConnent.repository.member.MemberRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -18,10 +22,13 @@

@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class MyClubService {
private final MemberRepository memberRepository;
private final ClubRepository clubRepository;
private final ClubMemberRepository clubMemberRepository;
private final MannerRepository mannerRepository;

public MyClubSimpleResponse getMyClubs(String email) {
Member member = getMemberByEmail(email);
Expand Down Expand Up @@ -131,4 +138,29 @@ private ClubMember getClubMemberByMemberAndId(Member member, Long clubId) {
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("해당 멤버가 가입된 모임이 아닙니다."));
}

public void deleteClubMember(String email, Long clubId, Long targetMemberId) {
Member member = getMemberByEmail(email);
Club club = clubRepository.findById(clubId)
.orElseThrow(() -> new IllegalArgumentException("해당 클럽을 찾을 수 없습니다."));
Member targetMember = memberRepository.findById(targetMemberId)
.orElseThrow(() -> new IllegalArgumentException("해당 멤버를 찾을 수 없습니다."));

ClubMember clubMember = getClubMemberByMemberAndId(member, clubId);
ClubMember targetClubMember = getClubMemberByMemberAndId(targetMember, clubId);

// 클럽장이 아닌 경우 예외 처리
if (!clubMember.getClubRole().equals(ClubRole.LEADER)){
throw new IllegalArgumentException("클럽장만 멤버를 삭제할 수 있습니다.");
}

// 해당 클럽 멤버와 관련된 manner 엔티티의 clubMember 필드를 null로 설정
List<Manner> manners = mannerRepository.findByClubMember(targetClubMember);
manners.forEach(manner -> manner.setClubMember(null));
mannerRepository.saveAll(manners);

clubMemberRepository.delete(targetClubMember);
club.setCurrentParticipant(club.getCurrentParticipant() - 1);
clubRepository.save(club);
}
}

0 comments on commit 872dfea

Please sign in to comment.