Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.somemore.community.dto.response.CommunityBoardDetailResponseDto;
import com.somemore.community.dto.response.CommunityBoardResponseDto;
import com.somemore.community.usecase.board.CommunityBoardDocumentUseCase;
//import com.somemore.community.usecase.board.CommunityBoardDocumentUseCase;
import com.somemore.community.usecase.board.CommunityBoardQueryUseCase;
import com.somemore.global.common.response.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -21,7 +21,7 @@
public class CommunityBoardQueryApiController {

private final CommunityBoardQueryUseCase communityBoardQueryUseCase;
private final CommunityBoardDocumentUseCase communityBoardDocumentUseCase;
// private final CommunityBoardDocumentUseCase communityBoardDocumentUseCase;

@GetMapping("/community-boards")
@Operation(summary = "전체 커뮤니티 게시글 조회", description = "전체 커뮤니티 게시글 목록을 조회합니다.")
Expand All @@ -48,18 +48,18 @@ public ApiResponse<Page<CommunityBoardResponseDto>> getByWriterId(
);
}

@GetMapping("/community-boards/search")
@Operation(summary = "커뮤니티 게시글 키워드 검색", description = "키워드로 포함한 커뮤니티 게시글 목록을 조회합니다.")
public ApiResponse<Page<CommunityBoardResponseDto>> getCommunityBoardsBySearch(
String keyword,
Pageable pageable
) {
return ApiResponse.ok(
200,
communityBoardDocumentUseCase.getCommunityBoardBySearch(keyword, pageable.getPageNumber()),
"커뮤니티 게시글 검색 리스트 조회 성공"
);
}
// @GetMapping("/community-boards/search")
// @Operation(summary = "커뮤니티 게시글 키워드 검색", description = "키워드로 포함한 커뮤니티 게시글 목록을 조회합니다.")
// public ApiResponse<Page<CommunityBoardResponseDto>> getCommunityBoardsBySearch(
// String keyword,
// Pageable pageable
// ) {
// return ApiResponse.ok(
// 200,
// communityBoardDocumentUseCase.getCommunityBoardBySearch(keyword, pageable.getPageNumber()),
// "커뮤니티 게시글 검색 리스트 조회 성공"
// );
// }

@GetMapping("/community-board/{id}")
@Operation(summary = "커뮤니티 게시글 상세 조회", description = "커뮤니티 게시글의 상세 정보를 조회합니다.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package com.somemore.community.domain;

import jakarta.persistence.Id;
import lombok.Builder;
import lombok.Getter;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Getter
@Document(indexName = "community_board")
public class CommunityBoardDocument {

@Id
private Long id;

@Field(type = FieldType.Text, analyzer = "nori_analyzer")
private String title;

@Field(type = FieldType.Text, analyzer = "nori_analyzer")
private String content;

@Builder
public CommunityBoardDocument(Long id, String title, String content) {
this.id = id;
this.title = title;
this.content = content;
}
}
//package com.somemore.community.domain;
//
//import jakarta.persistence.Id;
//import lombok.Builder;
//import lombok.Getter;
//import org.springframework.data.elasticsearch.annotations.Document;
//import org.springframework.data.elasticsearch.annotations.Field;
//import org.springframework.data.elasticsearch.annotations.FieldType;
//
//@Getter
//@Document(indexName = "community_board")
//public class CommunityBoardDocument {
//
// @Id
// private Long id;
//
// @Field(type = FieldType.Text, analyzer = "nori_analyzer")
// private String title;
//
// @Field(type = FieldType.Text, analyzer = "nori_analyzer")
// private String content;
//
// @Builder
// public CommunityBoardDocument(Long id, String title, String content) {
// this.id = id;
// this.title = title;
// this.content = content;
// }
//}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.somemore.community.repository.board;

import com.somemore.community.domain.CommunityBoardDocument;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.List;

public interface CommunityBoardDocumentRepository extends ElasticsearchRepository<CommunityBoardDocument, Long> {
List<CommunityBoardDocument> findAll();
@Query("{\"multi_match\": {\"query\": \"?0\", \"fields\": [\"title\", \"content\"]}}")
List<CommunityBoardDocument> findIdsByTitleOrContentContaining(String keyword);
}
//package com.somemore.community.repository.board;
//
//import com.somemore.community.domain.CommunityBoardDocument;
//import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
//
//public interface CommunityBoardDocumentRepository extends ElasticsearchRepository<CommunityBoardDocument, Long> {
//// List<CommunityBoardDocument> findAll();
//// @Query("{\"multi_match\": {\"query\": \"?0\", \"fields\": [\"title\", \"content\"]}}")
//// List<CommunityBoardDocument> findIdsByTitleOrContentContaining(String keyword);
//}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ default boolean doesNotExistById(Long id) {
}
void deleteAllInBatch();

Page<CommunityBoardView> findByCommunityBoardsContaining(String keyword, Pageable pageable);
void saveDocuments(List<CommunityBoard> communityBoards);
// Page<CommunityBoardView> findByCommunityBoardsContaining(String keyword, Pageable pageable);
// void saveDocuments(List<CommunityBoard> communityBoards);
List<CommunityBoard> findAll();
void deleteDocument(Long id);
// void deleteDocument(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.somemore.community.domain.CommunityBoard;
import com.somemore.community.domain.CommunityBoardDocument;
import com.somemore.community.repository.mapper.CommunityBoardView;
import com.somemore.community.domain.QCommunityBoard;
import com.somemore.volunteer.domain.QVolunteer;
Expand All @@ -15,7 +14,6 @@
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand All @@ -26,7 +24,7 @@ public class CommunityBoardRepositoryImpl implements CommunityBoardRepository {

private final JPAQueryFactory queryFactory;
private final CommunityBoardJpaRepository communityBoardJpaRepository;
private final CommunityBoardDocumentRepository documentRepository;
// private final CommunityBoardDocumentRepository documentRepository;

private static final QCommunityBoard communityBoard = QCommunityBoard.communityBoard;
private static final QVolunteer volunteer = QVolunteer.volunteer;
Expand Down Expand Up @@ -86,41 +84,41 @@ public boolean existsById(Long id) {
return communityBoardJpaRepository.existsByIdAndDeletedFalse(id);
}

@Override
public Page<CommunityBoardView> findByCommunityBoardsContaining(String keyword, Pageable pageable) {
List<CommunityBoardDocument> boardDocuments = getBoardDocuments(keyword);

List<Long> boardIds = boardDocuments.stream()
.map(CommunityBoardDocument::getId)
.toList();

List<CommunityBoardView> content = getCommunityBoardsQuery()
.where(communityBoard.id.in(boardIds)
.and(isNotDeleted()))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

JPAQuery<Long> countQuery = queryFactory
.select(communityBoard.count())
.from(communityBoard)
.join(volunteer).on(communityBoard.writerId.eq(volunteer.id))
.where(communityBoard.id.in(boardIds)
.and(isNotDeleted()));

return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
}

@Override
public void saveDocuments(List<CommunityBoard> communityBoards) {
List<CommunityBoardDocument> communityBoardDocuments = convertEntityToDocuments(communityBoards);
documentRepository.saveAll(communityBoardDocuments);
}

@Override
public void deleteDocument(Long id) {
documentRepository.deleteById(id);
}
// @Override
// public Page<CommunityBoardView> findByCommunityBoardsContaining(String keyword, Pageable pageable) {
// List<CommunityBoardDocument> boardDocuments = getBoardDocuments(keyword);
//
// List<Long> boardIds = boardDocuments.stream()
// .map(CommunityBoardDocument::getId)
// .toList();
//
// List<CommunityBoardView> content = getCommunityBoardsQuery()
// .where(communityBoard.id.in(boardIds)
// .and(isNotDeleted()))
// .offset(pageable.getOffset())
// .limit(pageable.getPageSize())
// .fetch();
//
// JPAQuery<Long> countQuery = queryFactory
// .select(communityBoard.count())
// .from(communityBoard)
// .join(volunteer).on(communityBoard.writerId.eq(volunteer.id))
// .where(communityBoard.id.in(boardIds)
// .and(isNotDeleted()));
//
// return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
// }

// @Override
// public void saveDocuments(List<CommunityBoard> communityBoards) {
// List<CommunityBoardDocument> communityBoardDocuments = convertEntityToDocuments(communityBoards);
// documentRepository.saveAll(communityBoardDocuments);
// }

// @Override
// public void deleteDocument(Long id) {
// documentRepository.deleteById(id);
// }

@Override
public List<CommunityBoard> findAll() {
Expand All @@ -142,31 +140,31 @@ private JPAQuery<CommunityBoardView> getCommunityBoardsQuery() {
.orderBy(communityBoard.createdAt.desc());
}

private List<CommunityBoardDocument> convertEntityToDocuments(List<CommunityBoard> communityBoards) {
List<CommunityBoardDocument> communityBoardDocuments = new ArrayList<>();

for (CommunityBoard communityboard : communityBoards) {
CommunityBoardDocument document = CommunityBoardDocument.builder()
.id(communityboard.getId())
.title(communityboard.getTitle())
.content(communityboard.getContent())
.build();
communityBoardDocuments.add(document);
}
return communityBoardDocuments;
}
// private List<CommunityBoardDocument> convertEntityToDocuments(List<CommunityBoard> communityBoards) {
// List<CommunityBoardDocument> communityBoardDocuments = new ArrayList<>();
//
// for (CommunityBoard communityboard : communityBoards) {
// CommunityBoardDocument document = CommunityBoardDocument.builder()
// .id(communityboard.getId())
// .title(communityboard.getTitle())
// .content(communityboard.getContent())
// .build();
// communityBoardDocuments.add(document);
// }
// return communityBoardDocuments;
// }

private BooleanExpression isNotDeleted() {
return communityBoard.deleted.eq(false);
}

private BooleanExpression isWriter(UUID writerId) {return communityBoard.writerId.eq(writerId); }

private List<CommunityBoardDocument> getBoardDocuments(String keyword) {

if (keyword == null || keyword.isEmpty()) {
return documentRepository.findAll();
}
return documentRepository.findIdsByTitleOrContentContaining(keyword);
}
// private List<CommunityBoardDocument> getBoardDocuments(String keyword) {
//
// if (keyword == null || keyword.isEmpty()) {
// return documentRepository.findAll();
// }
// return documentRepository.findIdsByTitleOrContentContaining(keyword);
// }
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package com.somemore.community.scheduler;

import com.somemore.community.domain.CommunityBoard;
import com.somemore.community.usecase.board.CommunityBoardDocumentUseCase;
import com.somemore.community.usecase.board.CommunityBoardQueryUseCase;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class CommunityBoardUpdateScheduler {

private final CommunityBoardQueryUseCase communityBoardQueryUseCase;
private final CommunityBoardDocumentUseCase communityBoardDocumentUseCase;


@Scheduled(cron = "${spring.schedules.cron.updateCommunityBoardDocuments}")
public void updateCommunityBoardDocuments() {
List<CommunityBoard> communityBoards = communityBoardQueryUseCase.getAllCommunityBoards();
communityBoardDocumentUseCase.saveCommunityBoardDocuments(communityBoards);
}
}
//package com.somemore.community.scheduler;
//
//import com.somemore.community.domain.CommunityBoard;
//import com.somemore.community.usecase.board.CommunityBoardDocumentUseCase;
//import com.somemore.community.usecase.board.CommunityBoardQueryUseCase;
//import lombok.RequiredArgsConstructor;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import java.util.List;
//
//@Component
//@RequiredArgsConstructor
//public class CommunityBoardUpdateScheduler {
//
// private final CommunityBoardQueryUseCase communityBoardQueryUseCase;
// private final CommunityBoardDocumentUseCase communityBoardDocumentUseCase;
//
//
// @Scheduled(cron = "${spring.schedules.cron.updateCommunityBoardDocuments}")
// public void updateCommunityBoardDocuments() {
// List<CommunityBoard> communityBoards = communityBoardQueryUseCase.getAllCommunityBoards();
// communityBoardDocumentUseCase.saveCommunityBoardDocuments(communityBoards);
// }
//}
Loading
Loading