Skip to content

Commit

Permalink
Merge pull request #76 from Likelion12/8-be-소셜링-검색-구현
Browse files Browse the repository at this point in the history
Feat : 소셜링 검색 구현
  • Loading branch information
SonMyeongJin authored Aug 5, 2024
2 parents 50ac143 + 19bf571 commit 7a12ce4
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import com.example.likelion12.util.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
Expand Down Expand Up @@ -82,7 +84,7 @@ public BaseResponse<List<GetSocialringSearchFilterResponse>> searchFilterSocialr
* 참가 예정인 소셜링
*/
@GetMapping("/join/before")
public BaseResponse<List<GetSocialringJoinStatusResponse>> joinBeforeSocialring(@RequestHeader("Authorization") String authorization){
public BaseResponse<List<GetSocialringJoinStatusResponse>> joinBeforeSocialring(@RequestHeader("Authorization") String authorization) {
log.info("[SocialringController.joinBeforeSocialring]");
Long memberId = jwtProvider.extractIdFromHeader(authorization);
return new BaseResponse<>(socialringService.joinBeforeSocialring(memberId));
Expand Down Expand Up @@ -110,6 +112,21 @@ public BaseResponse<Void> deleteSocialring(@RequestHeader("Authorization") Strin
return new BaseResponse<>(BaseExceptionResponseStatus.SUCCESS, null);
}

/**
* 소셜링 검색하기
*/
@GetMapping("/search")
public BaseResponse<Page<GetSocialringSearchResponse>> searchSocialrings(
@RequestHeader("Authorization") String authorization,
@RequestParam(required = false) String keyWord,
@RequestParam(required = false) LocalDate socialringDate,
@RequestParam(required = false) String activityRegionName,
@RequestParam(defaultValue = "0") int page
) {
Page<GetSocialringSearchResponse> responses = socialringService.searchSocialrings(keyWord, socialringDate, activityRegionName, page, 9);
return new BaseResponse<>(BaseExceptionResponseStatus.SUCCESS, responses);
}

/**
* 소셜링 취소하기
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.likelion12.dto.socialring;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.time.LocalDate;
import java.util.List;

@AllArgsConstructor
@Data
public class GetSocialringSearchResponse {

private long socialringId;
private String socialringName;
private String socialringImg;
private int totalRecruits;
private int currentMembers;
private LocalDate socialringDate;
private int socialringCost;
private String commentSimple;
private String ActivityRegionName;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.example.likelion12.domain.Socialring;
import com.example.likelion12.domain.base.BaseStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

Expand All @@ -23,5 +25,23 @@ public interface SocialringRepository extends JpaRepository<Socialring, Long> {
@Query("SELECT c FROM Socialring c WHERE c.activityRegion.id = :activityRegionId AND c.status = :status ORDER BY c.socialringId ASC")
List<Socialring> findTop3ByActivityRegionIdAndStatus(@Param("activityRegionId") Long activityRegionId, @Param("status") BaseStatus status, Pageable pageable);

/** 정적쿼리가 동적쿼리보다 빠르기때문에 정적쿼리
* JPA에서 제공하는 페이징 이용!*/
@Query("SELECT s, COUNT(ms) AS memberCount FROM Socialring s " +
"LEFT JOIN MemberSocialring ms ON s.id = ms.socialring.id " +
"WHERE (:keyWord IS NULL OR s.socialringName LIKE %:keyWord% " +
"OR s.commentSimple LIKE %:keyWord%) " +
"AND (:socialringDate IS NULL OR s.socialringDate = :socialringDate) " +
"AND (:activityRegionId IS NULL OR (s.activityRegion.id = :activityRegionId AND s.activityRegion.status = :status)) " +
"AND s.status = :status " +
"GROUP BY s.id " +
"ORDER BY s.socialringDate DESC")
Page<Object[]> searchSocialringsWithMemberCount(
@Param("keyWord") String keyWord,
@Param("socialringDate") LocalDate socialringDate,
@Param("activityRegionId") Long activityRegionId,
@Param("status") BaseStatus status,
Pageable pageable
);
List<Socialring> findAllByStatus(BaseStatus baseStatus);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
import com.example.likelion12.repository.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.example.likelion12.common.response.status.BaseExceptionResponseStatus.*;
Expand All @@ -26,6 +29,7 @@
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class SocialringService {
private final SocialringRepository socialringRepository;
private final MemberRepository memberRepository;
Expand Down Expand Up @@ -466,6 +470,45 @@ public void deleteSocialring(Long memberId, Long socialringId) {
}
}

/**
* 소셜링 검색
*/
public Page<GetSocialringSearchResponse> searchSocialrings(String keyWord, LocalDate socialringDate, String activityRegionName, int page, int size) {
Long activityRegionId = getActivityRegionIdByName(activityRegionName, BaseStatus.ACTIVE);

Pageable pageable = PageRequest.of(page, size);
Page<Object[]> results = socialringRepository.searchSocialringsWithMemberCount(
keyWord, socialringDate, activityRegionId, BaseStatus.ACTIVE, pageable
);
return results.map(result -> {
Socialring socialring = (Socialring) result[0];
long memberCount = (Long) result[1];
return new GetSocialringSearchResponse(
socialring.getSocialringId(),
socialring.getSocialringName(),
socialring.getSocialringImg(),
socialring.getTotalRecruits(),
(int) memberCount,
socialring.getSocialringDate(),
socialring.getSocialringCost(),
socialring.getCommentSimple(),
socialring.getActivityRegion().getActivityRegionName()
);
});
}

private Long getActivityRegionIdByName(String name, BaseStatus status){
Long activityRegionId = null;
/** 이름으로 지역아이디 얻기 */
if (name != null) {
Optional<ActivityRegion> activityRegionOpt = activityRegionRepository.findByActivityRegionNameAndStatus(name, status);
if (activityRegionOpt.isPresent()) {
activityRegionId = activityRegionOpt.get().getActivityRegionId();
}
}
return activityRegionId;
}

/**
* 소셜링 취소
*/
Expand Down

0 comments on commit 7a12ce4

Please sign in to comment.