게시판 검색 수정

This commit is contained in:
dyhj625 2025-03-25 15:38:43 +09:00
parent 4241859180
commit b0f77138cd
2 changed files with 78 additions and 24 deletions

View File

@ -55,13 +55,17 @@ public class localbordService {
private final PasswordEncoder passwordEncoder;
public List<MapDto> selectNotices(MapDto map) {
String keyword = map.getString("searchKeyword");
map.put("searchKeyword", null);
List<MapDto> posts = boardMapper.selectNotices(map);
enrichPostsWithAdditionalData(posts);
return posts;
return filterPostsByKeyword(posts, keyword);
}
public PageInfo<MapDto> selectGeneralPosts(MapDto map) {
System.out.println(map);
int page = map.getString("page") != null ? Integer.parseInt(map.getString("page")) : 1;
int size = map.getString("size") != null ? Integer.parseInt(map.getString("size")) : 10;
@ -70,12 +74,27 @@ public class localbordService {
map.put("orderBy", "date");
}
PageHelper.startPage(page, size);
String keyword = map.getString("searchKeyword");
map.put("searchKeyword", null); // MyBatis에 전달하지 않음
// 넉넉히 많이 가져온 Java에서 필터링
PageHelper.startPage(1, 1000); // 최대 1000개까지만 조회
List<MapDto> result = boardMapper.selectGeneralPosts(map);
enrichPostsWithAdditionalData(result);
return PageUtil.redefineNavigation(new PageInfo<>(result, size));
// 키워드 필터링 적용
List<MapDto> filtered = filterPostsByKeyword(result, keyword);
// 원하는 페이지만 잘라서 리턴
int fromIndex = (page - 1) * size;
int toIndex = Math.min(fromIndex + size, filtered.size());
List<MapDto> pageList = fromIndex >= filtered.size() ? new ArrayList<>() : filtered.subList(fromIndex, toIndex);
PageInfo<MapDto> pageInfo = new PageInfo<>(pageList);
pageInfo.setTotal(filtered.size());
return PageUtil.redefineNavigation(pageInfo);
}
public void updateIncrementViewCount(Long boardId) {
@ -314,17 +333,66 @@ public class localbordService {
return null; // 이미지가 없는 경우
}
private boolean procIsValidJson(String json) {
public String extractPlainText(String deltaJson) {
StringBuilder sb = new StringBuilder();
ObjectMapper mapper = new ObjectMapper();
try {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.readTree(json); // JSON 파싱 시도
return true; // JSON이 유효하면 true 반환
JsonNode root = mapper.readTree(deltaJson);
JsonNode ops = root.get("ops");
if (ops != null && ops.isArray()) {
for (JsonNode op : ops) {
JsonNode insertNode = op.get("insert");
// insert가 문자열일 경우만 추출
if (insertNode != null && insertNode.isTextual()) {
String text = insertNode.asText();
if (!"\n".equals(text.trim())) { // 줄바꿈 제외
sb.append(text).append(" ");
}
}
// insert가 객체면 image일 가능성 제외
}
}
} catch (Exception e) {
return false; // 유효하지 않은 경우 false 반환
e.printStackTrace(); // 실제 운영에서는 로깅 처리
}
return sb.toString().trim();
}
/**
* 검색어로 순수 텍스트 기준 게시글 필터링
*/
private List<MapDto> filterPostsByKeyword(List<MapDto> posts, String keyword) {
if (keyword == null || keyword.trim().isEmpty()) return posts;
keyword = keyword.trim();
List<MapDto> filtered = new ArrayList<>();
for (MapDto post : posts) {
String title = post.getString("title");
String author = post.getString("author");
String contentJson = BlobUtil.procBlobToString(post.get("content")); // Blob 처리
String plainText = extractPlainText(contentJson);
// 포함 여부 판단 (title, author, content 순수 텍스트)
if ((title != null && title.contains(keyword)) ||
(author != null && author.contains(keyword)) ||
plainText.contains(keyword)) {
post.put("content", contentJson); // content는 원래대로 복원
filtered.add(post);
}
}
return filtered;
}
private void enrichBoardDetail(MapDto boardDetail) {
if(boardDetail == null) return;

View File

@ -14,13 +14,6 @@
FROM localbord b
LEFT JOIN netmember m ON b.MEMBERSEQ = m.MEMBERSEQ
WHERE LOCBRDTYP = '300103'
<if test="searchKeyword != null and searchKeyword != ''">
AND (
LOCBRDTTL LIKE CONCAT('%', #{searchKeyword}, '%')
OR LOCBRDCON LIKE CONCAT('%', #{searchKeyword}, '%')
OR MEMBERNAM LIKE CONCAT('%', #{searchKeyword}, '%')
)
</if>
ORDER BY LOCBRDUDT DESC
</select>
@ -37,13 +30,6 @@
FROM localbord b
LEFT JOIN netmember m ON b.MEMBERSEQ = m.MEMBERSEQ
WHERE b.LOCBRDTYP IN ('300101', '300102')
<if test="searchKeyword != null and searchKeyword != ''">
AND (
LOCBRDTTL LIKE CONCAT('%', #{searchKeyword}, '%')
OR LOCBRDCON LIKE CONCAT('%', #{searchKeyword}, '%')
OR MEMBERNAM LIKE CONCAT('%', #{searchKeyword}, '%')
)
</if>
ORDER BY
<choose>
<when test="orderBy == 'date'"> b.LOCBRDUDT DESC </when>