-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBoardService.java
More file actions
57 lines (46 loc) · 1.91 KB
/
BoardService.java
File metadata and controls
57 lines (46 loc) · 1.91 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
package com.example.jpa.board.service;
import com.example.jpa.board.domain.Board;
import com.example.jpa.board.dto.request.BoardCreateRequest;
import com.example.jpa.board.dto.request.BoardUpdateRequest;
import com.example.jpa.board.dto.response.BoardResponse;
import com.example.jpa.board.repository.BoardRepository;
import com.example.jpa.member.domain.Member;
import com.example.jpa.member.service.MemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class BoardService {
private final BoardRepository boardRepository;
private final MemberService memberService;
@Transactional
public void createBoard(BoardCreateRequest boardCreateRequest) {
Member targetMember = memberService.checkExist(boardCreateRequest.getId());
boardRepository.save(boardCreateRequest.toEntity(targetMember));
}
public BoardResponse retrieveBoardById(Long id) {
return BoardResponse.from(checkExist(id));
}
public List<BoardResponse> retrieveBoards() {
return boardRepository.findAll().stream().map(BoardResponse::from).collect(Collectors.toList());
}
@Transactional
public void deleteBoard(Long id) {
boardRepository.delete(checkExist(id));
}
@Transactional
public void updateBoard(BoardUpdateRequest boardUpdateRequest) {
Board targetBoard = checkExist(boardUpdateRequest.getId());
targetBoard.update(boardUpdateRequest);
}
public Board checkExist(Long id) {
Board targetBoard = boardRepository.findById(id)
.orElseThrow(() -> new NoSuchElementException("게시판이 존재하지 않습니다."));
return targetBoard;
}
}