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

멤버 추방 / 모임 탈퇴 및 마감하기 #71

Merged
merged 4 commits into from
Jul 30, 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
19 changes: 19 additions & 0 deletions src/main/java/likelion/MZConnent/api/club/MyClubController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@RestController
Expand Down Expand Up @@ -46,4 +48,21 @@ public ResponseEntity<MemberRateResponse> getMemberRateAndCount(@AuthenticationP
return ResponseEntity.ok(response);
}

@PostMapping("/api/clubs/{clubId}/close")
public ResponseEntity closeClub(@AuthenticationPrincipal UserPrinciple userPrinciple, @PathVariable Long clubId) {
myClubService.closeClub(userPrinciple.getEmail(), clubId);
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","멤버 추방 성공"));
}

@PostMapping("/api/clubs/{clubId}/leave")
public ResponseEntity leaveClub(@AuthenticationPrincipal UserPrinciple userPrinciple, @PathVariable Long clubId) {
myClubService.leaveClub(userPrinciple.getEmail(), clubId);
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);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package likelion.MZConnent.service.club;

import jakarta.transaction.Transactional;
import likelion.MZConnent.domain.club.ClubRole;
import likelion.MZConnent.domain.member.Member;
import likelion.MZConnent.dto.club.LeaderDto;
Expand All @@ -21,6 +22,7 @@

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
public class ClubInfoService {
private final ClubRepository clubRepository;
Expand Down
118 changes: 100 additions & 18 deletions src/main/java/likelion/MZConnent/service/club/MyClubService.java
Original file line number Diff line number Diff line change
@@ -1,10 +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 @@ -15,9 +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 All @@ -32,23 +43,6 @@ public MyClubSimpleResponse getMyClubs(String email) {
.build();
}

private List<Club> getClubsByMember(Member member) {
return member.getClubMembers().stream()
.map(cm -> cm.getClub())
.collect(Collectors.toList());
}

private MyClubSimpleResponse.MyClubSimpleDto convertToSimpleDto(Club club) {
return MyClubSimpleResponse.MyClubSimpleDto.builder()
.clubId(club.getClubId())
.title(club.getTitle())
.cultureName(club.getCulture().getName())
.meetingDate(club.getMeetingDate())
.currentParticipant(club.getClubMembers().size())
.maxParticipant(club.getMaxParticipant())
.build();
}

public MyClubDetailResponse getMyClubDetail(String email, Long clubId) {
Member member = getMemberByEmail(email);
Club club = getClubByMemberAndId(member, clubId);
Expand All @@ -67,14 +61,90 @@ public MyClubDetailResponse getMyClubDetail(String email, Long clubId) {
.build();
}

public void closeClub(String email, Long clubId) {
Member member = getMemberByEmail(email);
ClubMember clubMember = getClubMemberByMemberAndId(member, clubId);
Club club = clubRepository.findById(clubId)
.orElseThrow(() -> new IllegalArgumentException("해당 클럽을 찾을 수 없습니다."));

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

club.setStatus("CLOSE");
clubRepository.save(club);
}

public void deleteClubMember(String email, Long clubId, Long targetMemberId) {
Member member = getMemberByEmail(email);
Club club = getClubById(clubId);
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);
}

public void leaveClub(String email, Long clubId) {
Member member = getMemberByEmail(email);
Club club = getClubById(clubId);
ClubMember clubMember = getClubMemberByMemberAndId(member, clubId);

// 클럽장인 경우 예외 처리
if (clubMember.getClubRole().equals(ClubRole.LEADER)){
throw new IllegalArgumentException("클럽장은 클럽을 탈퇴할 수 없습니다.");
}

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

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

private List<Club> getClubsByMember(Member member) {
return member.getClubMembers().stream()
.map(ClubMember::getClub)
.collect(Collectors.toList());
}

private MyClubSimpleResponse.MyClubSimpleDto convertToSimpleDto(Club club) {
return MyClubSimpleResponse.MyClubSimpleDto.builder()
.clubId(club.getClubId())
.title(club.getTitle())
.cultureName(club.getCulture().getName())
.meetingDate(club.getMeetingDate())
.currentParticipant(club.getClubMembers().size())
.maxParticipant(club.getMaxParticipant())
.build();
}

private Member getMemberByEmail(String email) {
return memberRepository.findByEmail(email)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 사용자입니다."));
}

private Club getClubByMemberAndId(Member member, Long clubId) {
return member.getClubMembers().stream()
.map(cm -> cm.getClub())
.map(ClubMember::getClub)
.filter(c -> c.getClubId().equals(clubId))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("해당 클럽을 찾을 수 없습니다."));
Expand Down Expand Up @@ -105,4 +175,16 @@ private List<MyClubDetailResponse.MyClubMemberDto> convertToMemberDtos(Club club
.build())
.collect(Collectors.toList());
}

private ClubMember getClubMemberByMemberAndId(Member member, Long clubId) {
return member.getClubMembers().stream()
.filter(cm -> cm.getClub().getClubId().equals(clubId))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("해당 멤버가 가입된 모임이 아닙니다."));
}

private Club getClubById(Long clubId) {
return clubRepository.findById(clubId)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 모임입니다."));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package likelion.MZConnent.service.club;

import jakarta.transaction.Transactional;
import likelion.MZConnent.domain.club.RegionCategory;
import likelion.MZConnent.dto.club.RegionCategoryDto;
import likelion.MZConnent.dto.club.response.RegionCategoryResponse;
Expand All @@ -12,6 +13,7 @@

@Service
@RequiredArgsConstructor
@Transactional
public class RegionCategoryService {
private final RegionCategoryRepository regionCategoryRepository;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package likelion.MZConnent.service.review;

import jakarta.transaction.Transactional;
import likelion.MZConnent.domain.culture.Culture;
import likelion.MZConnent.domain.member.Member;
import likelion.MZConnent.domain.review.Review;
Expand Down Expand Up @@ -29,6 +30,7 @@
@Service
@Slf4j
@RequiredArgsConstructor
@Transactional
public class ReviewService {
private final ReviewRepository reviewRepository;
private final S3ImageService s3ImageService;
Expand Down
Loading