|
1 | 1 | package codeview.main.controller;
|
2 | 2 |
|
| 3 | +import codeview.main.dto.ApiResponse; |
| 4 | +import codeview.main.dto.BoardRequest; |
| 5 | +import codeview.main.dto.BoardResponse; |
3 | 6 | import codeview.main.entity.Board;
|
4 | 7 | import codeview.main.service.BoardService;
|
5 | 8 | import lombok.RequiredArgsConstructor;
|
6 |
| -import org.springframework.stereotype.Controller; |
7 |
| -import org.springframework.web.bind.annotation.PostMapping; |
8 |
| -import org.springframework.web.bind.annotation.RequestBody; |
| 9 | +import org.springframework.http.HttpStatus; |
| 10 | +import org.springframework.http.ResponseEntity; |
| 11 | +import org.springframework.web.bind.annotation.*; |
9 | 12 |
|
10 |
| -@Controller |
| 13 | +import java.util.Optional; |
| 14 | + |
| 15 | + |
| 16 | +@RestController |
11 | 17 | @RequiredArgsConstructor
|
| 18 | +@RequestMapping("/board") |
12 | 19 | public class BoardController {
|
13 | 20 | private final BoardService boardService;
|
| 21 | + @GetMapping("/{id}") |
| 22 | + public ResponseEntity<BoardResponse> getBoardById(@PathVariable Long id) { |
| 23 | + Optional<Board> findBoardObj = boardService.findBoardById(id); |
| 24 | + if (findBoardObj.isPresent()) { |
| 25 | + BoardResponse response = new BoardResponse(findBoardObj.get()); |
| 26 | + return ResponseEntity.ok(response); |
| 27 | + } else { |
| 28 | + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); |
| 29 | + } |
| 30 | + } |
14 | 31 |
|
| 32 | + @PostMapping("/write") |
| 33 | + public ResponseEntity<ApiResponse<BoardResponse>> boardSave(@RequestBody Board board) { |
| 34 | + Board saveBoard = boardService.save(board); |
| 35 | + BoardResponse boardResponse = new BoardResponse(saveBoard); |
| 36 | + ApiResponse<BoardResponse> response = new ApiResponse<>(HttpStatus.CREATED, "Board Write Success", boardResponse); |
| 37 | + return ResponseEntity.status(HttpStatus.CREATED) |
| 38 | + .body(response); |
| 39 | + } |
| 40 | + @PutMapping("/{id}") |
| 41 | + public ResponseEntity<BoardResponse> updateBoard( |
| 42 | + @PathVariable Long id, |
| 43 | + @RequestBody BoardRequest boardRequest) { |
| 44 | + Board updatedBoard = new Board(); |
| 45 | + updatedBoard.setTitle(boardRequest.getTitle()); |
| 46 | + |
| 47 | + try { |
| 48 | + Board savedBoard = boardService.updateBoard(id, updatedBoard); |
| 49 | + BoardResponse responseDto = new BoardResponse(savedBoard); |
| 50 | + return ResponseEntity.ok(responseDto); |
| 51 | + } catch (RuntimeException e) { |
| 52 | + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); |
| 53 | + } |
| 54 | + } |
15 | 55 |
|
16 |
| - @PostMapping("/board/write") |
17 |
| - public void boardSave(@RequestBody Board board) { |
18 |
| - boardService.save(board); |
| 56 | + @DeleteMapping("/{id}") |
| 57 | + public ResponseEntity<BoardResponse> deleteBoard(@PathVariable Long id) { |
| 58 | + try { |
| 59 | + boardService.deleteBoard(id); |
| 60 | + return ResponseEntity.noContent().build(); |
| 61 | + }catch (RuntimeException e){ |
| 62 | + return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); |
| 63 | + } |
19 | 64 | }
|
20 | 65 | }
|
0 commit comments