Skip to content

Commit

Permalink
Merge branch 'main' into feature/club-close-leave
Browse files Browse the repository at this point in the history
해결된 충돌:
- src/main/java/likelion/MZConnent/api/culture/CultureController.java
- src/main/java/likelion/MZConnent/repository/culture/CultureInterestRepository.java
- src/main/java/likelion/MZConnent/service/culture/CultureService.java

로컬 변경 사항:
- src/main/java/likelion/MZConnent/api/club/MyClubController.java
- src/main/java/likelion/MZConnent/service/club/ClubInfoService.java
- src/main/java/likelion/MZConnent/service/club/MyClubService.java
- src/main/java/likelion/MZConnent/service/club/RegionCategoryService.java
  • Loading branch information
oosedus committed Jul 30, 2024
2 parents 872dfea + d10af06 commit 2acc155
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@ public ResponseEntity deleteMember(@AuthenticationPrincipal UserPrinciple userPr
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","모임 탈퇴 성공"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Slf4j
Expand Down Expand Up @@ -58,4 +57,16 @@ public ResponseEntity<CultureDetailResponse> getCultureDetailInfo(@PathVariable(

return ResponseEntity.ok(response);
}

// 문화 관심 토글
@PostMapping("/api/cultures/{cultureId}/interests")
public ResponseEntity<Map<String, String>> addCultureInterest(@PathVariable("cultureId") Long cultureId, @AuthenticationPrincipal UserPrinciple userPrinciple) {
boolean status = cultureService.toggleCultureInterest(userPrinciple.getEmail(), cultureId);

if (status) {
return ResponseEntity.ok(Map.of("message", "관심 문화 추가 성공"));
} else {
return ResponseEntity.ok(Map.of("message", "관심 문화 삭제 성공"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package likelion.MZConnent.repository.culture;

import likelion.MZConnent.domain.culture.Culture;
import likelion.MZConnent.domain.culture.CultureInterest;
import likelion.MZConnent.domain.member.Member;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface CultureInterestRepository extends JpaRepository<CultureInterest, Long> {
}

boolean existsByMemberAndCulture(Member member, Culture culture);

Optional<CultureInterest> findByMemberAndCulture(Member member, Culture culture);
}
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
136 changes: 80 additions & 56 deletions src/main/java/likelion/MZConnent/service/club/MyClubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,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 @@ -78,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 @@ -117,50 +176,15 @@ private List<MyClubDetailResponse.MyClubMemberDto> convertToMemberDtos(Club club
.collect(Collectors.toList());
}

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);
}

private ClubMember getClubMemberByMemberAndId(Member member, Long clubId) {
return member.getClubMembers().stream()
.filter(cm -> cm.getClub().getClubId().equals(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);
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,12 +1,16 @@
package likelion.MZConnent.service.culture;

import likelion.MZConnent.domain.culture.Culture;
import likelion.MZConnent.domain.culture.CultureInterest;
import likelion.MZConnent.domain.member.Member;
import likelion.MZConnent.domain.review.Review;
import likelion.MZConnent.dto.culture.response.CultureDetailResponse;
import likelion.MZConnent.dto.culture.response.CulturesSimpleResponse;
import likelion.MZConnent.dto.paging.response.PageContentResponse;
import likelion.MZConnent.dto.review.response.ReviewsSimpleResponse;
import likelion.MZConnent.repository.culture.CultureInterestRepository;
import likelion.MZConnent.repository.culture.CultureRepository;
import likelion.MZConnent.repository.member.MemberRepository;
import likelion.MZConnent.repository.review.ReviewRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -26,6 +30,9 @@
@RequiredArgsConstructor
public class CultureService {
private final CultureRepository cultureRepository;
private final CultureInterestRepository cultureInterestRepository;
private final MemberRepository memberRepository;

private final int PAGE_SIZE = 6;

public PageContentResponse<CulturesSimpleResponse> getCulturesSimpleList(Long cultureCategoryId, int page) {
Expand Down Expand Up @@ -65,11 +72,55 @@ public PageContentResponse<CulturesSimpleResponse> getMyIntersetCulturesSimpleLi
}

public CultureDetailResponse getCultureDetailInfo(Long cultureId) {
Culture culture = getCultureById(cultureId);
return CultureDetailResponse.builder()
.culture(culture).build();
}


// 문화 관심 토글
public boolean toggleCultureInterest(String email, Long cultureId) {

Member member = memberRepository.findByEmail(email).orElseThrow(() -> {
log.info("해당 회원이 존재하지 않음.");
return new IllegalArgumentException("해당 회원이 존재하지 않습니다.");
});
Culture culture = getCultureById(cultureId);

boolean exists = cultureInterestRepository.existsByMemberAndCulture(member, culture);


if (exists) { // 이미 문화를 관심 등록 해놓은 경우 (삭제)
CultureInterest cultureInterest = cultureInterestRepository.findByMemberAndCulture(member, culture)
.get();
cultureInterestRepository.delete(cultureInterest);

member.getCultureInterests().remove(cultureInterest);
culture.getCultureInterests().remove(cultureInterest);
culture.setInterestCount(culture.getInterestCount() - 1);
return false;
}
else { // 문화를 관심 등록 해놓지 않은 경우 (추가)
CultureInterest cultureInterest = cultureInterestRepository.save(CultureInterest.builder()
.member(member)
.culture(culture)
.build());

member.getCultureInterests().add(cultureInterest);
culture.getCultureInterests().add(cultureInterest);
culture.setInterestCount(culture.getInterestCount()+1);

return true;
}


}

private Culture getCultureById(Long cultureId) {
Culture culture = cultureRepository.findById(cultureId).orElseThrow(() -> {
log.info("해당 문화가 존재하지 않습니다.");
log.info("해당 문화가 존재하지 않음.");
return new IllegalArgumentException("해당 문화가 존재하지 않습니다.");
});
return CultureDetailResponse.builder()
.culture(culture).build();
return culture;
}
}

0 comments on commit 2acc155

Please sign in to comment.