Skip to content

Commit

Permalink
[FIX] #87 소모임 전체조회에서 좋아요 여부 띄우기
Browse files Browse the repository at this point in the history
- 비로그인도 접근 가능하게 허용해두어서, 로그인한 상태여도 인증정보가 SpringContext에 저장되지 않음
- 로그인한 상태에서 가는 api가 따로 있어야 할 것으로 예상
  • Loading branch information
c0smosaur committed Jun 8, 2024
1 parent bd0450d commit 6b0a9bc
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
import com.core.linkup.club.club.service.ClubService;
import com.core.linkup.common.response.BaseResponse;
import com.core.linkup.common.response.BaseResponseStatus;
import com.core.linkup.member.entity.Member;
import com.core.linkup.security.MemberDetails;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -39,11 +44,21 @@ public BaseResponse<ClubSearchResponse> findClub(
//TODO : OfficeBuilding으로 조회 가능 하도록 할 예정
@GetMapping("/search")
public BaseResponse<Page<ClubSearchResponse>> findClubs(
@AuthenticationPrincipal MemberDetails member,
@PageableDefault(sort = "id", direction = Sort.Direction.ASC) Pageable pageable,
@ModelAttribute ClubSearchRequest request
) {
Page<ClubSearchResponse> searchResponse = clubService.findClubs(pageable, request);
return BaseResponse.response(searchResponse);
@ModelAttribute ClubSearchRequest request) {

if (member!=null) {
// 로그인
Page<ClubSearchResponse> searchResponse =
clubService.findClubs(member.getMember(), pageable, request);
return BaseResponse.response(searchResponse);
} else {
// 비로그인
Page<ClubSearchResponse> searchResponse =
clubService.findClubs(pageable, request);
return BaseResponse.response(searchResponse);
}
}

//소모임 등록
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ public ClubSearchResponse toClubResponses(Club club, Member member) {
.build();
}

public ClubSearchResponse toClubResponses(Club club, Member member, boolean liked) {
return ClubSearchResponse.builder()
.id(club.getId())
.title(club.getTitle())
.introduction(club.getIntroduction())
.clubType(club.getCategory())
.recruitCount(club.getRecruitCount())
.memberId(member.getId()) //소모임을 생성함 멤버의 아이디
.memberName(member.getName())
.profileImage(member.getProfileImage())
.clubThumbnail(club.getClubThumbnail())
.liked(liked)
.build();
}

public ClubSearchResponse toClubResponse(Club club, Member member, List<ClubMember> clubMembers, List<ClubMeeting> clubMeetings, Map<Long, Member> memberMap) {
List<ClubMemberResponse> clubMemberResponses = clubMembers.stream()
.map(clubMember -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ClubLikeRepository extends JpaRepository<ClubLike, Long> {

void deleteByMemberIdAndClubId(Long memberId, Long clubId);

List<Long> findClubIdsByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public record ClubSearchResponse(
String clubType,
Integer recruitCount,
List<ClubMemberResponse> clubMembers,
List<ClubMeetingResponse> clubMeetings
List<ClubMeetingResponse> clubMeetings,
// Long officeBuildingId
Boolean liked

) {
}
22 changes: 20 additions & 2 deletions src/main/java/com/core/linkup/club/club/service/ClubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,32 @@ public ClubSearchResponse findClub(Long clubId) {
return clubConverter.toClubResponse(club, member, clubMembers, clubMeetings, memberMap);
}

public Page<ClubSearchResponse> findClubs(Pageable pageable, ClubSearchRequest request) {
public Page<ClubSearchResponse> findClubs(Member member, Pageable pageable, ClubSearchRequest request) {
Page<Club> clubs = clubRepository.findSearchClubs(request, pageable);
List<Member> members = memberRepository.findAllById(clubs.stream()
.map(Club::getMemberId)
.collect(Collectors.toList()));
Map<Long, Member> memberMap = members.stream()
.collect(Collectors.toMap(Member::getId, Function.identity()));
return clubs.map(club -> clubConverter.toClubResponses(club, memberMap.get(club.getMemberId())));

List<Long> clubLikes = clubLikeRepository.findClubIdsByMemberId(member.getId());

return clubs.map(club ->
clubConverter.toClubResponses(
club, memberMap.get(club.getMemberId()), clubLikes.contains(club.getId())));
}

public Page<ClubSearchResponse> findClubs(Pageable pageable, ClubSearchRequest request){
Page<Club> clubs = clubRepository.findSearchClubs(request, pageable);
List<Member> members = memberRepository.findAllById(clubs.stream()
.map(Club::getMemberId)
.collect(Collectors.toList()));
Map<Long, Member> memberMap = members.stream()
.collect(Collectors.toMap(Member::getId, Function.identity()));

return clubs.map(club ->
clubConverter.toClubResponses(
club, memberMap.get(club.getMemberId())));
}

// 소모임 등록
Expand Down

0 comments on commit 6b0a9bc

Please sign in to comment.