-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoardService.java
More file actions
137 lines (108 loc) · 4.19 KB
/
BoardService.java
File metadata and controls
137 lines (108 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.web.SearchWeb.board.service;
import com.web.SearchWeb.board.dao.BoardDao;
import com.web.SearchWeb.board.domain.Board;
import com.web.SearchWeb.board.dto.BoardDto;
import com.web.SearchWeb.comment.service.CommentService;
import com.web.SearchWeb.likes.service.LikesService;
import com.web.SearchWeb.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class BoardService {
private final BoardDao boardDao;
private final MemberService memberService;
private final LikesService likesService;
private final CommentService commentService;
@Autowired
public BoardService(BoardDao boardDao, MemberService memberService, LikesService likesService, CommentService commentService) {
this.boardDao = boardDao;
this.memberService = memberService;
this.likesService = likesService;
this.commentService = commentService;
}
/**
* 게시글 생성
*/
public int insertBoard(int memberId, BoardDto boardDto) {
return boardDao.insertBoard(memberId, boardDto);
}
/**
* 페이징된 게시글 목록 조회
*/
public Map<String, Object> selectBoardPage(int page, int size, String sort, String query, String postType) {
int offset = page * size;
int totalCount = boardDao.countBoardList(query, postType);
List<Board> boards = boardDao.selectBoardPage(offset, size, sort, query, postType);
List<String[]> hashtagsList = new ArrayList<>();
for (Board board : boards) {
//해시태그 추가
String[] hashtagsArray = board.getHashtags() != null ? board.getHashtags().split(" ") : new String[0];
hashtagsList.add(hashtagsArray);
}
Map<String, Object> result = new HashMap<>();
result.put("boards", boards);
result.put("hashtagsList", hashtagsList);
result.put("hasNext", offset + size < totalCount);
return result;
}
/**
* 게시글 단일 조회
*/
public Map<String, Object> selectBoard(int boardId) {
// 조회수 증가
boardDao.incrementViewCount(boardId);
Board board = boardDao.selectBoard(boardId); // 단일 Board 객체를 가져옵니다.
// 해시태그를 분리하여 리스트에 추가합니다.
String[] hashtagsList = board.getHashtags() != null ? board.getHashtags().split(" ") : new String[0];
Map<String, Object> result = new HashMap<>();
result.put("board", board);
result.put("hashtagsList", hashtagsList);
return result;
}
/**
* 게시글 수정
*/
public int updateBoard(int boardId, BoardDto boardDto){
return boardDao.updateBoard(boardId, boardDto);
}
/**
* 게시글 삭제
*/
public int deleteBoard(int boardId) {
return boardDao.deleteBoard(boardId);
}
/**
* 게시글 북마크 수 증가
*/
public void incrementBookmarkCount(int boardId) {
Board board = boardDao.selectBoard(boardId);
if (board != null) {
board.setBookmarks_count(board.getBookmarks_count() + 1);
boardDao.updateBookmarkCount(boardId, board.getBookmarks_count());
} else {
throw new IllegalArgumentException("Invalid board ID");
}
}
/**
* 게시글 북마크 수 감소
*/
public void decrementBookmarkCount(int boardId) {
Board board = boardDao.selectBoard(boardId);
if (board != null) {
board.setBookmarks_count(board.getBookmarks_count() - 1);
boardDao.updateBookmarkCount(boardId, board.getBookmarks_count());
} else {
throw new IllegalArgumentException("Invalid board ID");
}
}
// 게시글 소유자(작성자) 조회
public int findMemberIdByBoardId(int boardId) {
Board board = boardDao.selectBoard(boardId);
if (board == null) throw new IllegalArgumentException("게시글이 존재하지 않습니다.");
return board.getMember_memberId();
}
}