Skip to content

Commit

Permalink
Merge pull request #88 from c0smosaur/refactor/#87-club-like
Browse files Browse the repository at this point in the history
[REFACTOR] Refactor/#87 club like
  • Loading branch information
c0smosaur authored Jun 8, 2024
2 parents bb58008 + 0f7c30a commit 0771734
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 62 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 Expand Up @@ -110,24 +125,25 @@ public BaseResponse<List<ClubApplicationResponse>> findMyApplicationList(

//소모임 좋아요
@PostMapping("/{club_id}/like")
public BaseResponse<ClubLikeResponse> likeClub(
public BaseResponse<Void> likeClub(
@AuthenticationPrincipal MemberDetails member,
@PathVariable("club_id") Long clubId
) {
Long memberId = member.getId();

ClubLikeResponse response = clubService.likeClub(memberId, clubId);
return BaseResponse.response(response);
if (clubService.likeClub(member.getId(), clubId).equals("deleted")) {
return BaseResponse.response(BaseResponseStatus.LIKE_DELETED);
} else {
return BaseResponse.response(BaseResponseStatus.LIKE_SUCCESS);
}
}

// 좋아요 조회
@GetMapping("/like")
public BaseResponse<Page<ClubLikeResponse>> findClub(
@AuthenticationPrincipal MemberDetails member,
@PageableDefault(sort = "id", direction = Sort.Direction.ASC) Pageable pageable,
@ModelAttribute ClubLikeRequest request
@PageableDefault(sort = "id", direction = Sort.Direction.ASC) Pageable pageable
) {
Page<ClubLikeResponse> response = clubService.findLikeClub(member, pageable, request);
Page<ClubLikeResponse> response = clubService.findLikeClub(member, pageable);
return BaseResponse.response(response);
}
}
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 Expand Up @@ -163,29 +178,6 @@ public ClubLike toLikeEntity(Long memberId, Long clubId) {
.build();
}

public ClubLikeResponse toUnLikeResponse(boolean liked, String message, Long memberId, Long clubId) {
return ClubLikeResponse.builder()
.clubId(clubId)
.memberId(memberId)
.liked(liked)
.message(message)
.build();
}

public ClubLikeResponse toLikeResponse(ClubLike clubLike, Club club) {
return ClubLikeResponse.builder()
.id(clubLike.getId())
.clubId(clubLike.getClubId())
.memberId(clubLike.getMemberId())
.liked(true)
.message("좋아요가 등록되었습니다.")
.clubThumbnail(club.getClubThumbnail())
.clubName(club.getTitle())
.clubIntroduction(club.getIntroduction())
.clubMemberCount(club.getRecruitCount())
.build();
}

public ClubLikeResponse toLikeResponse(ClubLike clubLike, Club club, ClubMeeting clubMeeting) {
return ClubLikeResponse.builder()
.id(clubLike.getId())
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/core/linkup/club/club/entity/ClubLike.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ public class ClubLike extends BaseEntity {

private Long clubId;
private Long memberId;
private Boolean liked;
private String message;
}
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);
}

This file was deleted.

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

) {
}
31 changes: 24 additions & 7 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 Expand Up @@ -212,23 +230,22 @@ private Club validateClub(Long clubId) {
return club;
}

public ClubLikeResponse likeClub(Long memberId, Long clubId) {
public String likeClub(Long memberId, Long clubId) {
boolean duplicate = clubRepository.existsByMemberIdAndClubId(memberId, clubId);

if (duplicate) {
clubRepository.deleteByMemberIdAndClubId(memberId, clubId);
return clubConverter.toUnLikeResponse(false, "좋아요가 취소되었습니다.", memberId, clubId);
return "deleted";
} else {
ClubLike clubLike = clubConverter.toLikeEntity(memberId, clubId);
clubLikeRepository.save(clubLike);
Club club = clubRepository.findById(clubId)
.orElseThrow(() -> new BaseException(BaseResponseStatus.INVALID_CLUB_ID));
return clubConverter.toLikeResponse(clubLike, club);
return "liked";
}
}


public Page<ClubLikeResponse> findLikeClub(MemberDetails member, Pageable pageable, ClubLikeRequest request) {
public Page<ClubLikeResponse> findLikeClub(MemberDetails member, Pageable pageable) {
Long memberId = member.getId();

Page<ClubLike> clubLikes = clubRepository.findClubLikes(memberId, pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
@NoArgsConstructor
public abstract class BaseMembershipEntity extends BaseEntity{

private Long officeId;
private String location;
private Long price;
private Integer duration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public enum BaseResponseStatus {
SUCCESS(true, OK.value(), "요청이 성공적으로 처리되었습니다."),
CREATE_SUCCESS(true, CREATED.value(), "성공적으로 생성되었습니다."),
DELETE_SUCCESS(true, NO_CONTENT.value(), "성공적으로 삭제되었습니다."),
LIKE_SUCCESS(true, NO_CONTENT.value(), "좋아요가 생성되었습니다."),
LIKE_DELETED(true, NO_CONTENT.value(), "좋아요가 삭제되었습니다."),

EMAIL_ERROR(false, OK.value(), "메일이 도착하지 않았다면 다시 시도해주세요."),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ public CompanyResponse toCompanyResponse(Company company) {
.build();
}

public CompanyMembership toCompanyMembership(CompanyMembershipRequest request,
public CompanyMembership toCompanyMembership(Long officeId,
CompanyMembershipRequest request,
Long companyId){
return CompanyMembership.builder()
.location(request.getLocation())
.officeId(officeId)
.type(MembershipType.COMPANY_PASS)
.price(request.getPrice())
.duration(request.getDuration())
Expand All @@ -49,6 +51,7 @@ public CompanyMembership toCompanyMembership(CompanyMembershipRequest request,
public MembershipResponse toMembershipResponse(CompanyMembership companyMembership) {
return MembershipResponse.builder()
.id(companyMembership.getId())
.officeId(companyMembership.getOfficeId())
.type(companyMembership.getType().getName())
.location(companyMembership.getLocation())
.duration(companyMembership.getDuration())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ public class CompanyMembershipService {
// 기업 생성, 기업 멤버십 생성
@Transactional
public CompanyMembershipRegistrationResponse registerCompanyMembership(
Long officeId,
CompanyMembershipRegistrationRequest request) {
if (request.getCompany().isConsentContact()){
Company company = saveCompany(request.getCompany());
CompanyMembership companyMembership =
saveCompanyMembership(request.getCompanyMembership(), company.getId());
saveCompanyMembership(officeId, request.getCompanyMembership(), company.getId());

String authCode = authCodeUtils.createCompanyAuthCode();
sendCompanyAuthCode(company, authCode);
Expand Down Expand Up @@ -70,12 +71,13 @@ public void sendCompanyAuthCode(Company company, String authCode) {
}

// 기업 멤버십 생성
public CompanyMembership saveCompanyMembership(CompanyMembershipRequest request,
public CompanyMembership saveCompanyMembership(Long officeId,
CompanyMembershipRequest request,
Long companyId) {

return companyMembershipRepository.save(
companyMembershipConverter.toCompanyMembership(
request, companyId));
officeId, request, companyId));
}

public Company saveCompany(CompanyRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
@Converter
public class IndividualMembershipConverter {

public IndividualMembership toIndividualMembershipEntity(IndividualMembershipRequest request, Member member) {
public IndividualMembership toIndividualMembershipEntity(Long officeId,
IndividualMembershipRequest
request, Member member) {
MembershipType membershipType = MembershipType.fromKor(request.getType());
return IndividualMembership.builder()
.location(request.getLocation())
.officeId(officeId)
.type(membershipType)
.duration(request.getDuration())
.startDate(request.getStartDate().atStartOfDay())
Expand All @@ -28,6 +31,7 @@ public MembershipResponse toMembershipResponse(IndividualMembership individualMe
.type(individualMembership.getType().getName())
.id(individualMembership.getId())
.location(individualMembership.getLocation())
.officeId(individualMembership.getOfficeId())
.price(individualMembership.getPrice())
.duration(individualMembership.getDuration())
.startDate(individualMembership.getStartDate().toLocalDate())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ public class IndividualMembershipService {
private final IndividualMembershipRepository individualMembershipRepository;
private final IndividualMembershipConverter individualMembershipConverter;

public IndividualMembership saveIndividualMembership(IndividualMembershipRequest request,
public IndividualMembership saveIndividualMembership(Long officeId,
IndividualMembershipRequest request,
Member member) {
return individualMembershipRepository.save(
individualMembershipConverter.toIndividualMembershipEntity(request, member));
individualMembershipConverter.toIndividualMembershipEntity(officeId, request, member));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ public class CompanyMembershipReservationController {
private final CompanyMembershipReservationService companyMembershipReservationService;

// 기업 등록, 기업 멤버십 생성, 기업 인증번호 발송
@PostMapping
@PostMapping("/{officeId}")
public BaseResponse<CompanyMembershipRegistrationResponse> registerCompany(
@RequestBody CompanyMembershipRegistrationRequest request) {
@RequestBody CompanyMembershipRegistrationRequest request,
@PathVariable Long officeId
) {
CompanyMembershipRegistrationResponse response =
companyMembershipReservationService.registerCompanyMembership(request);
companyMembershipReservationService.registerCompanyMembership(officeId, request);
return BaseResponse.response(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class MembershipResponse {
private Long id;
private String location;
private Long officeId;
private Long price;
private String type;
private Integer duration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public class CompanyMembershipReservationService {
private final CompanyMembershipConverter companyMembershipConverter;

// (생성, 응답 변환) 기업 문의하기 -> 기업, 기업 멤버십 생성
public CompanyMembershipRegistrationResponse registerCompanyMembership(CompanyMembershipRegistrationRequest request) {
return companyMembershipService.registerCompanyMembership(request);
public CompanyMembershipRegistrationResponse registerCompanyMembership(
Long officeId,
CompanyMembershipRegistrationRequest request) {
return companyMembershipService.registerCompanyMembership(officeId, request);
}

// 사용자의 단일 기업 멤버십
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public MembershipReservationListResponse registerIndividualMembership(
IndividualMembershipRegistrationRequest requests, Member member, Long officeId) {
reservationValidationService.validateOfficeLocation(requests, officeId);
IndividualMembership membership =
individualMembershipService.saveIndividualMembership(requests.getMembership(), member);
individualMembershipService.saveIndividualMembership(officeId, requests.getMembership(), member);
List<ReservationResponse> reservationResponses =
reservationService.createReservationResponses(requests.getReservations(), membership);
return reservationConverter.toMembershipReservationListResponse(
Expand Down

0 comments on commit 0771734

Please sign in to comment.