java 배우기/TIL

Spring 익숙해지기 - 2

checkuu 2024. 8. 23. 14:17

이전에 만들었던 Member를 Board로 바꿔서 내용 입력까지 구현해봤다.

 

Entity

@Entity
@Getter
@NoArgsConstructor
public class Board {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String contents;

    public Board(String title, String contents){
        this.title = title;
        this.contents = contents;
    }

    public void updateTitle(String title){
        this.title = title;
    }

    public void updateContents(String contents) {
        this.contents = contents;
    }
}

 

Repository

public interface BoardRepository extends JpaRepository<Board, Long> {
}

 

Controller

@Controller
@RequiredArgsConstructor
public class BoardController {

    private final BoardService boardService;

    @PostMapping("/boards") //게시판 등록
    public ResponseEntity<BoardSaveResponseDto> saveBoard(@RequestBody BoardSaveRequestDto requestDto){
        return ResponseEntity.ok(boardService.saveBoard(requestDto));
    }

    @GetMapping("/boards") //게시판 목록 조회
    public ResponseEntity<List<BoardSimpleResponseDto>> getBoards(){
        return ResponseEntity.ok(boardService.getBoards());
    }

    @GetMapping("/boards/{Id}") //게시판 단건 조회
    public ResponseEntity<BoardDetailResponseDto> getBoard(@PathVariable Long Id){
        return ResponseEntity.ok(boardService.getBoard(Id));
    }

    @PutMapping("/boards/title/{Id}") //게시판 제목 수정
    public ResponseEntity<BoardUpdateResponseDto> updateBoardTitle(@PathVariable Long Id, @RequestBody BoardUpdateTitleRequestDto requestDto){
        return ResponseEntity.ok(boardService.updateBoardTitle(Id,requestDto));
    }

    @PutMapping("/boards/contents/{Id}") //게시판 내용 수정
    public ResponseEntity<BoardUpdateResponseDto> updateBoardContents(@PathVariable Long Id, @RequestBody BoardUpdateContentsRequestDto requestDto){
        return ResponseEntity.ok(boardService.updateBoardContents(Id,requestDto));
    }

    @DeleteMapping("/boards/{id}") //게시판 삭제
    public void deleteBoard(@PathVariable Long id){
        boardService.deleteBoard(id);
    }
}

게시글 조회의 경우 게시판 목록 처럼 id 와 title만 나오게 설정했다.

title과 contents 수정을 따로 할 수 있게 설정했다. responseDto 의 경우 둘다 보여주는것 같아서 하나로 했다.

 

Service

@Service
@RequiredArgsConstructor
public class BoardService {

    private final BoardRepository boardRepository;

    @Transactional
    public BoardSaveResponseDto saveBoard(BoardSaveRequestDto requestDto) {
        Board board = new Board(requestDto.getTitle(), requestDto.getContents());
        Board saveBoard = boardRepository.save(board);

        return new BoardSaveResponseDto(saveBoard.getId(), saveBoard.getTitle(), saveBoard.getContents());
    }

    public List<BoardSimpleResponseDto> getBoards() {
        List<Board> boards = boardRepository.findAll();
        List<BoardSimpleResponseDto> boardSimpleResponseDtos = new ArrayList<>();
        for (Board board : boards) {
            boardSimpleResponseDtos.add(new BoardSimpleResponseDto(board.getId(), board.getTitle()));
        }
        return boardSimpleResponseDtos;
    }

    public BoardDetailResponseDto getBoard(Long id) {
        Board board = boardRepository.findById(id).orElseThrow(()-> new NullPointerException("해당하는 게시글이 없습니다"));
        return new BoardDetailResponseDto(board.getId(), board.getTitle(), board.getContents());
    }

    @Transactional
    public BoardUpdateResponseDto updateBoardTitle(Long id, BoardUpdateTitleRequestDto requestDto) {
        Board board = boardRepository.findById(id).orElseThrow(() -> new NullPointerException("해당하는 게시글이 없습니다."));

        board.updateTitle(requestDto.getTitle());

        return new BoardUpdateResponseDto(board.getId(), board.getTitle(), board.getContents());
    }

    @Transactional
    public BoardUpdateResponseDto updateBoardContents(Long id, BoardUpdateContentsRequestDto requestDto) {
        Board board = boardRepository.findById(id).orElseThrow(() -> new NullPointerException("해당하는 게시글이 없습니다."));
        board.updateContents(requestDto.getContents());

        return new BoardUpdateResponseDto(board.getId(), board.getTitle(), board.getContents());
    }

    public void deleteBoard(Long id) {
        Board board = boardRepository.findById(id).orElseThrow(()-> new NullPointerException("해당하는 게시글이 없습니다."));
        boardRepository.delete(board);
    }
}

 

Dto

@Getter
public class BoardDetailResponseDto {
    private final Long id;
    private final String title;
    private final String contents;

    public BoardDetailResponseDto (Long id,String title, String contents){
        this.id = id;
        this.title = title;
        this.contents = contents;
    }
    
@Getter
public class BoardSaveRequestDto {
    private String title;
    private String contents;
}

@Getter
public class BoardSaveResponseDto {

    private final Long id;
    private final String title;
    private final String contents;

    public BoardSaveResponseDto(Long id,String title, String contents) {
        this.id = id;
        this.title = title;
        this.contents = contents;
    }
}

@Getter
public class BoardSimpleResponseDto {
    private final Long id;
    private final String title;


    public BoardSimpleResponseDto(Long id,String title){
        this.id = id;
        this.title = title;
    }
}

@Getter
public class BoardUpdateContentsRequestDto {
    private String contents;
}

@Getter
public class BoardUpdateResponseDto {
    private final Long id;
    private final String title;
    private final String contents;

    public BoardUpdateResponseDto(Long id, String title, String contents){
        this.id = id;
        this.title = title;
        this.contents = contents;
    }
}

@Getter
public class BoardUpdateTitleRequestDto {
    private String title;
}

 

Postman

 

게시글 등록

게시글 조회 

 

게시글 단건 조회

 

게시글 제목 수정

 

게시글 내용 수정

 

게시글 수정 후 조회

 

게시글 삭제 후 조회

 

member만들었을 때 보다 요소가 하나더 늘었으나 더 빠르게 만들었다.

'java 배우기 > TIL' 카테고리의 다른 글

REST API  (0) 2024.08.28
Spring 배우기 개인과제  (0) 2024.08.27
JPA , Entity  (0) 2024.08.23
Spring Boot 서버 포트 변경하기  (0) 2024.08.21
Spring 익숙해 지기  (0) 2024.08.20