diff --git a/src/main/java/gwangjang/server/domain/community/application/dto/res/SearchRes.java b/src/main/java/gwangjang/server/domain/community/application/dto/res/SearchRes.java new file mode 100644 index 0000000..ce486c3 --- /dev/null +++ b/src/main/java/gwangjang/server/domain/community/application/dto/res/SearchRes.java @@ -0,0 +1,17 @@ +package gwangjang.server.domain.community.application.dto.res; + +import lombok.*; + +import java.util.List; + +@Getter +@Builder +@Setter +@AllArgsConstructor +@NoArgsConstructor +public class SearchRes { + + String searchKeyword; + String searchCount; + List communityResList; +} diff --git a/src/main/java/gwangjang/server/domain/community/application/mapper/CommunityMapper.java b/src/main/java/gwangjang/server/domain/community/application/mapper/CommunityMapper.java index 4e9d2d1..9c32d65 100644 --- a/src/main/java/gwangjang/server/domain/community/application/mapper/CommunityMapper.java +++ b/src/main/java/gwangjang/server/domain/community/application/mapper/CommunityMapper.java @@ -43,4 +43,20 @@ public CommunityRes mapToCommunityResByMemberDto(MemberDto memberDto) { .build(); } + public CommunityRes mapToCommunityRes(Community community) { + return CommunityRes.builder() + .id(community.getId()) + .communityText(community.getTalk()) + .date(community.getCreatedAt().toString()) // 날짜 형식에 따라 수정 + .writerId(community.getWriterId()) + .area(community.getTopic()) + .subject(community.getIssue()) + .keyword(community.getKeyword()) + .likeCount((long) community.getHearts().size()) // 가정: 좋아요는 hearts 리스트에 저장되어 있다고 가정 + .commentCount((long) community.getComments().size()) // 가정: 댓글은 comments 리스트에 저장되어 있다고 가정 + .contentsId(community.getContentsId()) + .likeStatus(null) // 사용자의 좋아요 상태에 따라 설정 + .build(); + } + } diff --git a/src/main/java/gwangjang/server/domain/community/application/service/CommunityReadUseCase.java b/src/main/java/gwangjang/server/domain/community/application/service/CommunityReadUseCase.java index a7cebd2..539d56e 100644 --- a/src/main/java/gwangjang/server/domain/community/application/service/CommunityReadUseCase.java +++ b/src/main/java/gwangjang/server/domain/community/application/service/CommunityReadUseCase.java @@ -2,6 +2,7 @@ import gwangjang.server.domain.community.application.dto.res.CommunityRes; +import gwangjang.server.domain.community.application.dto.res.SearchRes; import gwangjang.server.domain.community.domain.entity.constant.CommunityOrderCondition; import gwangjang.server.domain.community.domain.service.CommunityQueryService; import gwangjang.server.global.annotation.DomainService; @@ -34,9 +35,12 @@ public List getCommunityTop5ByHearts(String memberId,String orderB return communityQueryService.getCommunityTop5(memberId,CommunityOrderCondition.valueOf(orderBy), word); } - public List getCommunityByMyHearts(String memberId) { return communityQueryService.getCommunityByMyHearts(memberId); } + public SearchRes getSearchList(String memberId,String orderBy,String keyword){ + return communityQueryService.search(memberId,CommunityOrderCondition.valueOf(orderBy),keyword); + + } } diff --git a/src/main/java/gwangjang/server/domain/community/domain/repository/CommunityCustomRepository.java b/src/main/java/gwangjang/server/domain/community/domain/repository/CommunityCustomRepository.java index 95c900d..9722384 100644 --- a/src/main/java/gwangjang/server/domain/community/domain/repository/CommunityCustomRepository.java +++ b/src/main/java/gwangjang/server/domain/community/domain/repository/CommunityCustomRepository.java @@ -16,6 +16,7 @@ public interface CommunityCustomRepository { Optional> findCommunityTop5ByHeartsAndTopic(String memberId,String topic); Optional> findCommunityTop5(String memberId,CommunityOrderCondition orderCondition, String word); + Optional> getSearchCommunity(String memberId,CommunityOrderCondition orderCondition, String keyword); Optional> findCommunityByMyHearts(String memberId); } diff --git a/src/main/java/gwangjang/server/domain/community/domain/repository/CommunityCustomRepositoryImpl.java b/src/main/java/gwangjang/server/domain/community/domain/repository/CommunityCustomRepositoryImpl.java index 530466b..9489352 100644 --- a/src/main/java/gwangjang/server/domain/community/domain/repository/CommunityCustomRepositoryImpl.java +++ b/src/main/java/gwangjang/server/domain/community/domain/repository/CommunityCustomRepositoryImpl.java @@ -2,11 +2,13 @@ import com.querydsl.core.types.Projections; import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.core.types.dsl.StringPath; import com.querydsl.jpa.JPAExpressions; import com.querydsl.jpa.JPQLQuery; import com.querydsl.jpa.impl.JPAQueryFactory; import gwangjang.server.domain.community.application.dto.res.CommunityRes; import gwangjang.server.domain.community.domain.entity.Community; +import gwangjang.server.domain.community.domain.entity.QCommunity; import gwangjang.server.domain.community.domain.entity.constant.CommunityOrderCondition; import jakarta.persistence.EntityManager; @@ -220,6 +222,41 @@ private BooleanExpression orderByColumn(CommunityOrderCondition orderCondition, } } + public Optional> getSearchCommunity(String memberId,CommunityOrderCondition orderCondition, String keyword) { + BooleanExpression memberHeartExists = JPAExpressions + .selectOne() + .from(heart) + .where( + heart.community.id.eq(community.id), + heart.pusherId.eq(memberId), + heart.status.eq(Boolean.TRUE)) + .exists(); + + return Optional.ofNullable(queryFactory + .select(Projections.constructor(CommunityRes.class, + community.id, + community.talk, + community.createdAt, + community.writerId, + community.topic, + community.issue, + community.keyword, + community.hearts.size().longValue(), + community.comments.size().longValue(), + community.contentsId, + memberHeartExists.stringValue() + )) + .from(community) + .where( + orderByColumn(orderCondition,keyword) + + ).orderBy(community.hearts.size().desc()) // hearts의 크기에 따라 내림차순 정렬 + .fetch()); + + } + + + diff --git a/src/main/java/gwangjang/server/domain/community/domain/service/CommunityQueryService.java b/src/main/java/gwangjang/server/domain/community/domain/service/CommunityQueryService.java index 8144ec2..baa1cf2 100644 --- a/src/main/java/gwangjang/server/domain/community/domain/service/CommunityQueryService.java +++ b/src/main/java/gwangjang/server/domain/community/domain/service/CommunityQueryService.java @@ -2,6 +2,7 @@ import gwangjang.server.domain.community.application.dto.res.CommunityRes; import gwangjang.server.domain.community.application.dto.res.MemberDto; +import gwangjang.server.domain.community.application.dto.res.SearchRes; import gwangjang.server.domain.community.application.mapper.CommunityMapper; import gwangjang.server.domain.community.domain.entity.Community; import gwangjang.server.domain.community.domain.entity.constant.CommunityOrderCondition; @@ -12,7 +13,10 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.Collections; import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; //@DomainService @Service @@ -22,6 +26,8 @@ public class CommunityQueryService { private final CommunityRepository communityRepository; private final FindMemberFeignClient findMemberFeignClient; + private final CommunityMapper communityMapper = new CommunityMapper(); + public Community getCommunityById(Long communityId) { return communityRepository.findById(communityId).orElseThrow(NotFoundCommunityException::new ); @@ -83,4 +89,16 @@ public List getCommunityTop5(String memberId,CommunityOrderConditi public List getCommunityByMyHearts(String memberId) { return communityRepository.findCommunityByMyHearts(memberId).orElseThrow(NotFoundCommunityException::new); } + public SearchRes search(String memberId, CommunityOrderCondition communityOrderCondition, String keyword) { + List searchResults = communityRepository.getSearchCommunity(memberId,communityOrderCondition,keyword).orElseThrow(NotFoundCommunityException::new);; + + + SearchRes searchRes = new SearchRes(); + searchRes.setSearchKeyword(keyword); + searchRes.setSearchCount(String.valueOf(searchResults.size())); + searchRes.setCommunityResList(searchResults); + + return searchRes; + + } } diff --git a/src/main/java/gwangjang/server/domain/community/presentation/CommunityController.java b/src/main/java/gwangjang/server/domain/community/presentation/CommunityController.java index 713b268..5397203 100644 --- a/src/main/java/gwangjang/server/domain/community/presentation/CommunityController.java +++ b/src/main/java/gwangjang/server/domain/community/presentation/CommunityController.java @@ -2,8 +2,10 @@ import gwangjang.server.domain.community.application.dto.req.CommunityReq; import gwangjang.server.domain.community.application.dto.res.CommunityRes; +import gwangjang.server.domain.community.application.dto.res.SearchRes; import gwangjang.server.domain.community.application.service.CommunityCreateUseCase; import gwangjang.server.domain.community.application.service.CommunityReadUseCase; +import gwangjang.server.domain.community.domain.entity.constant.CommunityOrderCondition; import gwangjang.server.domain.community.presentation.constant.CommunityResponseMessage; import gwangjang.server.global.response.SuccessResponse; import lombok.AllArgsConstructor; @@ -75,13 +77,15 @@ public ResponseEntity>> getCommunityTop5(@Req return ResponseEntity.ok(SuccessResponse.create(CommunityResponseMessage.GET_COMMUNITY_SUCCESS.getMessage(), this.communityReadUseCase.getCommunityTop5ByHearts(socialId,sortBy,word))); } - @GetMapping("/mypage") public ResponseEntity>> getMyHeartCommunity(@RequestHeader(value = "user-id") String socialId) { return ResponseEntity.ok(SuccessResponse.create(CommunityResponseMessage.GET_COMMUNITY_SUCCESS.getMessage(), this.communityReadUseCase.getCommunityByMyHearts(socialId))); } - + @GetMapping("/search/{sortBy}/{keyword}") + public ResponseEntity> search(@RequestHeader(value = "user-id") String socialId, @PathVariable("sortBy") String sortBy, @PathVariable String keyword) { + return ResponseEntity.ok(SuccessResponse.create(CommunityResponseMessage.GET_COMMUNITY_SUCCESS.getMessage(),this.communityReadUseCase.getSearchList(socialId ,sortBy,keyword))); + } }