Skip to content

Commit

Permalink
Merge pull request #12 from kusitms-28th-Meetup-E/feat/search
Browse files Browse the repository at this point in the history
feat: Search API
  • Loading branch information
eojinny authored Nov 22, 2023
2 parents 91906c5 + bb21daf commit d4921cf
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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<CommunityRes> communityResList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,9 +35,12 @@ public List<CommunityRes> getCommunityTop5ByHearts(String memberId,String orderB
return communityQueryService.getCommunityTop5(memberId,CommunityOrderCondition.valueOf(orderBy), word);
}


public List<CommunityRes> getCommunityByMyHearts(String memberId) {

return communityQueryService.getCommunityByMyHearts(memberId);
}
public SearchRes getSearchList(String memberId,String orderBy,String keyword){
return communityQueryService.search(memberId,CommunityOrderCondition.valueOf(orderBy),keyword);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public interface CommunityCustomRepository {
Optional<List<CommunityRes>> findCommunityTop5ByHeartsAndTopic(String memberId,String topic);

Optional<List<CommunityRes>> findCommunityTop5(String memberId,CommunityOrderCondition orderCondition, String word);
Optional<List<CommunityRes>> getSearchCommunity(String memberId,CommunityOrderCondition orderCondition, String keyword);

Optional<List<CommunityRes>> findCommunityByMyHearts(String memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -220,6 +222,41 @@ private BooleanExpression orderByColumn(CommunityOrderCondition orderCondition,
}
}

public Optional<List<CommunityRes>> 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());

}






Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
);
Expand Down Expand Up @@ -83,4 +89,16 @@ public List<CommunityRes> getCommunityTop5(String memberId,CommunityOrderConditi
public List<CommunityRes> getCommunityByMyHearts(String memberId) {
return communityRepository.findCommunityByMyHearts(memberId).orElseThrow(NotFoundCommunityException::new);
}
public SearchRes search(String memberId, CommunityOrderCondition communityOrderCondition, String keyword) {
List<CommunityRes> 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;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,13 +77,15 @@ public ResponseEntity<SuccessResponse<List<CommunityRes>>> getCommunityTop5(@Req
return ResponseEntity.ok(SuccessResponse.create(CommunityResponseMessage.GET_COMMUNITY_SUCCESS.getMessage(), this.communityReadUseCase.getCommunityTop5ByHearts(socialId,sortBy,word)));
}


@GetMapping("/mypage")
public ResponseEntity<SuccessResponse<List<CommunityRes>>> 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<SuccessResponse<SearchRes>> 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)));
}

}

Expand Down

0 comments on commit d4921cf

Please sign in to comment.