리스트 첨부파일유무,댓글갯수,좋아요/싫어요 갯수 추가

This commit is contained in:
dyhj625 2025-01-17 15:25:42 +09:00
parent cd81956d7c
commit 77730be07f
4 changed files with 103 additions and 4 deletions

View File

@ -72,6 +72,12 @@ public class BoardController {
Blob blob = (Blob) content; Blob blob = (Blob) content;
post.put("content", safeBlobToString(blob)); post.put("content", safeBlobToString(blob));
} }
// "id" 값을 Number로 받고 longValue() 변환
Object idObject = post.get("id");
long postId = ((Number) idObject).longValue();
post.put("hasAttachment", boardService.hasAttachments(postId));
} }
return ApiResponse.ok(posts); return ApiResponse.ok(posts);
@ -93,6 +99,19 @@ public class BoardController {
Blob blob = (Blob) content; Blob blob = (Blob) content;
post.put("content", safeBlobToString(blob)); post.put("content", safeBlobToString(blob));
} }
// "id" 값을 Number로 받고 longValue() 변환
Object idObject = post.get("id");
if (idObject instanceof Number) {
long postId = ((Number) idObject).longValue();
post.put("commentCount", boardService.getCommentCount(postId));
post.put("hasAttachment", boardService.hasAttachments(postId));
MapDto reactions = boardService.getBoardReactions(postId);
post.put("likeCount", reactions.getOrDefault("likeCount", 0));
post.put("dislikeCount", reactions.getOrDefault("dislikeCount", 0));
} else {
post.put("commentCount", 0); // id가 없거나 잘못된 경우 기본값 설정
}
} }
return ApiResponse.ok(posts); return ApiResponse.ok(posts);
@ -182,16 +201,32 @@ public class BoardController {
throw new RuntimeException("Failed to process Blob content", e); throw new RuntimeException("Failed to process Blob content", e);
} }
} }
MapDto reactions = boardService.getBoardReactions(boardId);
boardDetail.put("likeCount", reactions.getOrDefault("likeCount", 0));
boardDetail.put("dislikeCount", reactions.getOrDefault("dislikeCount", 0));
// 첨부파일 댓글 조회
List<MapDto> attachments = boardService.getAttachments(boardId); List<MapDto> attachments = boardService.getAttachments(boardId);
List<MapDto> comments = boardService.getComments(boardId.intValue()); List<MapDto> comments = boardService.getComments(boardId.intValue());
List<MapDto> commentReactions = boardService.getCommentReactions(boardId);
for (MapDto comment : comments) {
Integer commentId = (Integer) comment.get("LOCCMTSEQ");
for (MapDto reaction : commentReactions) {
if (reaction.get("LOCCMTSEQ").equals(commentId)) {
comment.put("likeCount", reaction.getOrDefault("likeCount", 0));
comment.put("dislikeCount", reaction.getOrDefault("dislikeCount", 0));
}
}
}
// 결과 조합 // 결과 조합
MapDto result = new MapDto(); MapDto result = new MapDto();
result.put("boardDetail", boardDetail); result.put("boardDetail", boardDetail);
result.put("attachments", attachments); result.put("attachments", attachments);
result.put("comments", comments); result.put("comments", comments);
result.put("commentCount", boardService.getCommentCount(boardId));
result.put("hasAttachment", boardService.hasAttachments(boardId));
return ApiResponse.ok(result); return ApiResponse.ok(result);
} }

View File

@ -55,13 +55,24 @@ public interface localbordMapper {
// 게시물 상세보기 // 게시물 상세보기
MapDto selectBoardDetail(Long boardId); MapDto selectBoardDetail(Long boardId);
// 댓글 갯수
int countComments(Long boardId);
// 첨부파일 유무
int countAttachments(Long boardId);
// 게시물 좋아요/싫어요 개수
MapDto getBoardReactions(Long boardId);
// 댓글 좋아요/싫어요 개수
List<MapDto> getCommentReactions(Long boardId);
// 첨부파일 가져오기 // 첨부파일 가져오기
List<MapDto> selectAttachments(Long boardId); List<MapDto> selectAttachments(Long boardId);
//댓글 좋아요/싫어요추가
void reactToComment(MapDto map);
//댓글id 확인 //댓글id 확인
MapDto getCommentById(int commentId); MapDto getCommentById(int commentId);
} }

View File

@ -92,6 +92,24 @@ public class localbordService {
public MapDto getCommentById(int commentId) { public MapDto getCommentById(int commentId) {
return boardMapper.getCommentById(commentId); return boardMapper.getCommentById(commentId);
} }
public int getCommentCount(Long boardId) {
return boardMapper.countComments(boardId);
}
public boolean hasAttachments(Long boardId) {
int count = boardMapper.countAttachments(boardId);
return count > 0; // 첨부파일 개수가 0보다 크면 true 반환
}
public MapDto getBoardReactions(Long boardId) {
return boardMapper.getBoardReactions(boardId);
}
public List<MapDto> getCommentReactions(Long boardId) {
return boardMapper.getCommentReactions(boardId);
}
} }

View File

@ -146,5 +146,40 @@
WHERE LOCCMTSEQ = #{commentId} WHERE LOCCMTSEQ = #{commentId}
</select> </select>
<!-- 댓글 개수 조회 -->
<select id="countComments" parameterType="long" resultType="int">
SELECT COUNT(*)
FROM localcomt
WHERE LOCBRDSEQ = #{boardId}
</select>
<!-- 첨부파일 유무 -->
<select id="countAttachments" resultType="int">
SELECT COUNT(*)
FROM commonfil
WHERE CMNBRDSEQ = #{boardId}
</select>
<!-- 게시물 좋아요/싫어요 개수 조회 -->
<select id="getBoardReactions" resultType="io.company.localhost.common.dto.MapDto">
SELECT
COALESCE(SUM(CASE WHEN LOCGOBGOD = 'T' THEN 1 ELSE 0 END), 0) AS likeCount,
COALESCE(SUM(CASE WHEN LOCGOBBAD = 'T' THEN 1 ELSE 0 END), 0) AS dislikeCount
FROM localgorb
WHERE LOCBRDSEQ = #{boardId};
</select>
<!-- 댓글별 좋아요/싫어요 개수 조회 -->
<select id="getCommentReactions" resultType="io.company.localhost.common.dto.MapDto">
SELECT
LOCCMTSEQ,
SUM(CASE WHEN LOCGOBGOD = 'T' THEN 1 ELSE 0 END) AS likeCount,
SUM(CASE WHEN LOCGOBBAD = 'T' THEN 1 ELSE 0 END) AS dislikeCount
FROM localgorb
WHERE LOCBRDSEQ = #{boardId}
GROUP BY LOCCMTSEQ
</select>
</mapper> </mapper>